/* 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.Windows.Forms;
using System.Reflection;
using System.Resources;
using AHD.ID3Tag;
using AHD.SM.ASMP;
using AHD.Forms;
namespace AHD.SM.Forms
{
    public partial class Frm_ID3v2 : Form
    {
        public Frm_ID3v2(Project project, bool dropUncompatibleFrames, bool writeFooter, byte saveVersion)
        {
            InitializeComponent();
            this.project = project;
            this.dropUncompatibleFrames = dropUncompatibleFrames;
            this.writeFooter = writeFooter;
            this.saveVersion = saveVersion;
            //refresh tracks
            foreach (SubtitlesTrack track in project.SubtitleTracks)
                listBox_subtitleTracks.Items.Add(track);
            //id3 tag items
            id3v2 = new ID3v2();
            try { id3v2.Load(project.MediaPath); }
            catch (NotID3v2Exception ex)
            {
                MessageDialog.ShowMessage(resources.GetString("Message_ThisMp3FileDoesntHaveID3v2"),
                    resources.GetString("MessageCaption_NoID3Tagv2Found"));
            }
            catch (Exception ex)
            {
                MessageDialog.ShowMessage(ex.Message + "\n\n" + ex.ToString(),
                 resources.GetString("Error"), MessageDialogButtons.Ok, MessageDialogIcon.Error);
                this.Close();
                return;
            }
            ID3v2Frame[] frames = id3v2.GetFramesLoaded("SYLT");
            foreach (AHD.ID3Tag.SynchronisedLyricsFrames.SYLT frame in frames)
                listBox_id3Items.Items.Add(frame);
            //others
            foreach (string lang in Consts.Languages)
                comboBox1.Items.Add(lang);
        }
        ID3v2 id3v2;
        Project project;
        bool dropUncompatibleFrames = false;
        bool writeFooter = false;
        byte saveVersion = 3;
        ResourceManager resources = new ResourceManager("AHD.SM.Forms.LanguageResources.Resource",
        Assembly.GetExecutingAssembly());

        private void listBox_subtitleTracks_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            if (listBox_subtitleTracks.SelectedIndex < 0)
                return;
            groupBox3.Text = resources.GetString("Title_DataFor") + " '" + ((SubtitlesTrack)listBox_subtitleTracks.SelectedItem).Name +
                "' " + resources.GetString("SubtitlesTrack");
            int i = 1;
            foreach (Subtitle sub in ((SubtitlesTrack)listBox_subtitleTracks.SelectedItem).Subtitles)
            {
                ListViewItem_Subtitle item = new ListViewItem_Subtitle();
                item.Number = i;
                item.Subtitle = sub;
                item.ChangeTimingView(SubtitleTimingMode.Timespan_Milli);
                listView1.Items.Add(item);
                i++;
            }
        }

        private void listBox_id3Items_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            if (listBox_id3Items.SelectedIndex < 0)
                return;
            groupBox3.Text = resources.GetString("Title_DataFor") + " '" + ((SynchronisedLyricsFrame)listBox_id3Items.SelectedItem).ToString() +
                "' " + resources.GetString("ID3TagSynchronisedLyricItem");
            int i = 1;
            foreach (SynchronisedLyricsItem id3item in
                ((SynchronisedLyricsFrame)listBox_id3Items.SelectedItem).Items)
            {
                ListViewItem item = new ListViewItem();
                item.Text = i.ToString();
                item.SubItems.Add(((double)id3item.Time / 1000).ToString("F3"));
                item.SubItems.Add("N/A");
                item.SubItems.Add("N/A");
                item.SubItems.Add(id3item.Text);
                listView1.Items.Add(item);
                i++;
            }

            textBox1.Text = ((SynchronisedLyricsFrame)listBox_id3Items.SelectedItem).Description;
            comboBox1.SelectedItem = ((SynchronisedLyricsFrame)listBox_id3Items.SelectedItem).Language;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (listBox_id3Items.SelectedIndex >= 0)
            {
                SynchronisedLyricsFrame frame = (SynchronisedLyricsFrame)listBox_id3Items.SelectedItem;
                frame.Description = textBox1.Text;
                int index = listBox_id3Items.SelectedIndex;
                listBox_id3Items.Items.RemoveAt(index);
                listBox_id3Items.Items.Insert(index, frame);
                listBox_id3Items.SelectedIndex = index;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox_id3Items.SelectedIndex >= 0)
            {
                SynchronisedLyricsFrame frame = (SynchronisedLyricsFrame)listBox_id3Items.SelectedItem;
                frame.Language = comboBox1.SelectedItem.ToString();
                int index = listBox_id3Items.SelectedIndex;
                listBox_id3Items.Items.RemoveAt(index);
                listBox_id3Items.Items.Insert(index, frame);
                listBox_id3Items.SelectedIndex = index;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (listBox_id3Items.SelectedIndex < 0)
                return;
            MessageDialogResult result = MessageDialog.ShowMessage(resources.GetString("Message_AreYouSure"),
               resources.GetString("MessageCaption_DeleteItem"), MessageDialogButtons.OkNo, MessageDialogIcon.Question,
                false, resources.GetString("Button_Yes"), resources.GetString("Button_No"));
            if (result == MessageDialogResult.Ok)
            {
                listBox_id3Items.Items.RemoveAt(listBox_id3Items.SelectedIndex);
                listView1.Items.Clear();
                textBox1.Text = "";
                comboBox1.SelectedIndex = -1;
            }
        }
        //Add selected ID3v2 selected item as a Subtitles Track to the project
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (listBox_id3Items.SelectedIndex < 0)
                return;
            EnterNameForm frm = new EnterNameForm(resources.GetString("Title_EnterProjectName"), resources.GetString("NewSubtitlesTrack"),
                true, false);
            frm.OkPressed += new EventHandler<EnterNameFormOkPressedArgs>(frm_OkPressed);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                AHD.ID3Tag.SynchronisedLyricsFrames.SYLT frame = (AHD.ID3Tag.SynchronisedLyricsFrames.SYLT)listBox_id3Items.SelectedItem;
                SubtitlesTrack track = new SubtitlesTrack(frm.EnteredName);
                for (int i = 0; i < frame.Items.Count; i++)
                {
                    Subtitle sub = new Subtitle();
                    sub.StartTime = ((double)frame.Items[i].Time / 1000);
                    if (i != frame.Items.Count - 1)
                    {
                        sub.EndTime = ((double)frame.Items[i + 1].Time / 1000) - 0.001;
                    }
                    else
                        sub.EndTime = sub.StartTime + 2;
                    sub.Text = SubtitleText.FromString(frame.Items[i].Text);
                    track.Subtitles.Add(sub);
                }
                listBox_subtitleTracks.Items.Add(track);
            }
        }
        //Add selected Subtitles Track as ID3v2 item
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listBox_subtitleTracks.SelectedIndex < 0)
                return;
            SubtitlesTrack track = ((SubtitlesTrack)listBox_subtitleTracks.SelectedItem);
            AHD.ID3Tag.SynchronisedLyricsFrames.SYLT frame = new AHD.ID3Tag.SynchronisedLyricsFrames.SYLT();
            frame.Description = track.Name;
            frame.Language = "English [eng]";
            frame.TimeStampFormat = TimeStampFormat.AbsoluteMilliseconds;
            frame.ContentType = "Lyrics";
            foreach (Subtitle sub in track.Subtitles)
            {
                SynchronisedLyricsItem item = new SynchronisedLyricsItem((int)(sub.StartTime * 1000), sub.Text.ToString());
                frame.Items.Add(item);
            }
            listBox_id3Items.Items.Add(frame);
            listBox_id3Items.SelectedIndex = listBox_id3Items.Items.Count - 1;
        }
        //ok
        private void button1_Click(object sender, EventArgs e)
        {
            //refresh tracks
            project.SubtitleTracks.Clear();
            foreach (SubtitlesTrack track in listBox_subtitleTracks.Items)
                project.SubtitleTracks.Add(track);
            //save ID3 tag
            id3v2.TagVersion = saveVersion;
            id3v2.IgnoreUncompatibleFrames = dropUncompatibleFrames;
            id3v2.Flag_FooterPresent = writeFooter;

            id3v2.RemoveFrame("SYLT");
            foreach (AHD.ID3Tag.SynchronisedLyricsFrames.SYLT frame in listBox_id3Items.Items)
                id3v2.Frames.Add(frame);
            id3v2.Save(project.MediaPath);
            //close
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        void frm_OkPressed(object sender, EnterNameFormOkPressedArgs e)
        {
            if (project.IsSubtitlesTrackExist(e.NameEntered))
            {
                MessageDialog.ShowMessage(resources.GetString("Message_ThisNameAlreadyTaken"),
                  resources.GetString("MessageCaption_NameAlreadyTaken"));
                e.Cancel = true;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, ".\\" + System.Threading.Thread.CurrentThread.CurrentUICulture.Name + "\\Help.chm", HelpNavigator.KeywordIndex, "How to, Save subtitle track(s) as ID3 Tag Synchronised Lyrics element to mp3 file");
        }
    }
}
