/* This file is part of AHD Subtitles Maker Professional
   A program can create and edit subtitles

   Copyright © Ala I Hadid 2009 - 2012

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;
using System.Resources;
using AHD.SM.ASMP;
using AHD.Forms;
using RavSoft;

namespace AHD.SM.Forms
{
    public partial class Frm_Translate : Form
    {
        public Frm_Translate(SubtitlesTrack subtitlesTrack)
        {
            InitializeComponent();
            this.subtitlesTrack = subtitlesTrack;
            //Clone
            foreach (Subtitle sub in subtitlesTrack.Subtitles)
            {
                Subtitle newSubtitle = new Subtitle();
            
                newSubtitle.EndTime = sub.EndTime;
                newSubtitle.StartTime = sub.StartTime;
                //text
                SubtitleText newText = new SubtitleText();
                newText.CustomPosition = sub.Text.CustomPosition;
                newText.IsCustomPosition = sub.Text.IsCustomPosition;
                newText.Position = sub.Text.Position;
                foreach (SubtitleLine line in sub.Text.TextLines)
                {
                    SubtitleLine newline = new SubtitleLine();
                    newline.Alignement = line.Alignement;
                    foreach (SubtitleChar chr in line.Chars)
                    {
                        newline.Chars.Add(new SubtitleChar(chr.TheChar, chr.Font, chr.Color));
                    }
                    newText.TextLines.Add(newline);
                }
                newSubtitle.Text.RighttoLeft = sub.Text.RighttoLeft;
                newSubtitle.Text = newText;
                TsubtitlesTrack.Subtitles.Add(newSubtitle);
            }
            //Refresh Track
            SubtitleTimingMode timingMode = SubtitleTimingMode.Timespan_Milli;
            listView1.Items.Clear();
            listView2.Items.Clear();
            int i = 1;
            foreach (Subtitle sub in subtitlesTrack.Subtitles)
            {
                //add to original
                ListViewItem_Subtitle item = new ListViewItem_Subtitle();
                item.Number = i;
                item.Subtitle = sub;
                item.ChangeTimingView(timingMode);
                listView1.Items.Add(item);
                //add to translation
                ListViewItem_Subtitle Titem = new ListViewItem_Subtitle();
                Titem.Number = i;
                Titem.Subtitle = TsubtitlesTrack.Subtitles[i - 1];
                Titem.RefreshText(true);
                listView2.Items.Add(Titem);
                i++;
            }
        }
        ResourceManager resources = new ResourceManager("AHD.SM.Forms.LanguageResources.Resource",
          Assembly.GetExecutingAssembly());
        SubtitlesTrack subtitlesTrack;
        SubtitlesTrack TsubtitlesTrack = new SubtitlesTrack();//translation subtitles track
        bool createNew = false;
        Thread mainThread;
        string targetLanguage = "English";
        string sourceLanguage = "English";
        delegate void SetIntValue(int value);
        delegate void EnableDisable();
        delegate string GetText(int index);
        delegate void TranslateItem(int index, string text);
        //properties
        public SubtitlesTrack TranslationSubtitlesTrack
        {
            get { return TsubtitlesTrack; }
        }
        public bool IsCreateNew
        { get { return createNew; } }

        void TranslateProgress()
        {
            //disable
            Disablecontrols();
            Translator translator = new Translator();
            translator.TargetLanguage = targetLanguage;
            translator.SourceLanguage = sourceLanguage;

            for (int i = 0; i < subtitlesTrack.Subtitles.Count; i++)
            {
                string translation = "";
                foreach (SubtitleLine line in subtitlesTrack.Subtitles[i].Text.TextLines)
                {
                    translator.SourceText = line.ToString();
                    translator.Translate();
                    translation += translator.Translation + "\n";
                }
                translation = translation.Substring(0, translation.Length - 1);
                TranslateSubItem(i, translation);

                int x = (i * 100) / listView1.Items.Count;
                SetProgress(x);
            }
            //finish
            Enablecontrols();
        }
        void Disablecontrols()
        {
            if (!this.InvokeRequired)
                Disablecontrols1();
            else
                Invoke(new EnableDisable(Disablecontrols1));
        }
        void Disablecontrols1()
        {
            subtitleTextEditor_original.Enabled = subtitleTextEditor_translate.Enabled =
                button1.Enabled = button2.Enabled = button5.Enabled = false;
            progressBar1.Visible = true;
            button3.Text = resources.GetString("Button_Stop");
            progressBar1.Value = 0;
        }
        void Enablecontrols()
        {
            if (!this.InvokeRequired)
                Enablecontrols1();
            else
                Invoke(new EnableDisable(Enablecontrols1));
        }
        void Enablecontrols1()
        {
            subtitleTextEditor_original.Enabled = subtitleTextEditor_translate.Enabled =
                 button1.Enabled = button2.Enabled = button5.Enabled = true;
            progressBar1.Visible = false;
            button3.Text = resources.GetString("Button_Cancel");
        }
        void SetProgress(int value)
        {
            if (!this.InvokeRequired)
                SetProgress1(value);
            else
                Invoke(new SetIntValue(SetProgress1), new object[] { value });
        }
        void SetProgress1(int value)
        {
            progressBar1.Value = value;
        }
        void TranslateSubItem(int index, string text)
        {
            if (!this.InvokeRequired)
                TranslateSubItem1(index, text);
            else
                Invoke(new TranslateItem(TranslateSubItem1), new object[] { index, text });
        }
        void TranslateSubItem1(int index, string text)
        {
            ((ListViewItem_Subtitle)listView2.Items[index]).Subtitle.Text = SubtitleText.FromString(text, new Font("Tahoma", 8, FontStyle.Regular), Color.White);
            ((ListViewItem_Subtitle)listView2.Items[index]).RefreshText(true);
            ((ListViewItem_Subtitle)listView2.Items[index]).EnsureVisible();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count != 1)
                return;
            //update selection
            listView2.Items[listView1.SelectedItems[0].Index].Selected = true;
            listView2.Items[listView1.SelectedItems[0].Index].EnsureVisible();
            //text
            subtitleTextEditor_original.SubtitleText = ((ListViewItem_Subtitle)listView1.SelectedItems[0]).Subtitle.Text;
        }
        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count != 1)
                return;
            //update selection
            listView1.Items[listView2.SelectedItems[0].Index].Selected = true;
            listView1.Items[listView2.SelectedItems[0].Index].EnsureVisible();
            //text
            subtitleTextEditor_translate.SubtitleText = ((ListViewItem_Subtitle)listView2.SelectedItems[0]).Subtitle.Text;
        }
        //Create New
        private void button1_Click(object sender, EventArgs e)
        {
            createNew = true;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        //Replace
        private void button2_Click(object sender, EventArgs e)
        {
            createNew = false;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (mainThread != null)
            {
                if (mainThread.IsAlive)
                {
                    if (MessageDialog.ShowMessage(resources.GetString("Message_AreYouSureYouWantToStopCurrentProgress"),
                        resources.GetString("MessageCaption_StopTranslationProgress"), MessageDialogButtons.OkNo, MessageDialogIcon.Question) == MessageDialogResult.Ok)
                    {
                        mainThread.Abort();
                        mainThread = null;
                        Enablecontrols();
                    }
                    return;
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }
        //Show Help
        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void subtitleTextEditor_translate_SubtitleTextChanged(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count != 1)
                return;
            //update selection
            ((ListViewItem_Subtitle)listView2.SelectedItems[0]).RefreshText(true);
        }
        //Google Translate
        private void button5_Click(object sender, EventArgs e)
        {
            Frm_TranslateLanguageFromTo frm = new Frm_TranslateLanguageFromTo();
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                sourceLanguage = frm.LanguageFrom;
                targetLanguage = frm.LanguageTo;
                mainThread = new Thread(new ThreadStart(TranslateProgress));
                mainThread.Start();
            }
        }
    }
}
