/* 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.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AHD.SM.ASMP;

namespace AHD.SM.Controls
{
    public partial class SubtitlesDataEditor : UserControl
    {
        public SubtitlesDataEditor()
        {
            InitializeComponent();
            settings.Reload();
            //load settings
            toolStripComboBox1.SelectedIndex = settings.SubtitlesData_ViewIndex;
            columnHeader_StartTime.Width = settings.SubtitlesData_startime;
            columnHeader_EndTime.Width = settings.SubtitlesData_endime;
            columnHeader_duration.Width = settings.SubtitlesData_duration;
            columnHeader_text.Width = settings.SubtitlesData_text;
            columnHeader_number.Width = settings.SubtitlesData_number;
        }

        SubtitlesTrack track;
        ControlsSettings settings = new ControlsSettings();
        int timer = 1;
        bool showViewed = true;

        public void SaveSettings()
        {
            settings.SubtitlesData_ViewIndex = toolStripComboBox1.SelectedIndex;
            settings.SubtitlesData_startime = columnHeader_StartTime.Width;
            settings.SubtitlesData_endime = columnHeader_EndTime.Width;
            settings.SubtitlesData_duration = columnHeader_duration.Width;
            settings.SubtitlesData_text = columnHeader_text.Width;
            settings.SubtitlesData_number = columnHeader_number.Width;
            settings.Save();
        }
        /// <summary>
        /// Get or set if the control should always show the subtitle that viewed
        /// </summary>
        public bool AlwayesEnsureViewed
        { get { return showViewed; } set { showViewed = value; } }
        public void SelectItem(int index)
        {
            try
            {
                listView1.Items[index].Selected = true;
            }
            catch { }
        }
        public void SelectItem(Subtitle sub)
        {
            foreach (ListViewItem_Subtitle item in listView1.Items)
            {
                if (item.Subtitle == sub)
                { item.Selected = true; break; }
            }
        }
        public void ClearDataIcons()
        {
            foreach (ListViewItem_Subtitle item in listView1.Items)
                item.ImageIndex = 0;
        }
        public void SetViewedSubtitle(int index, bool IsViewed)
        {
            try
            {
                ClearDataIcons();
                listView1.Items[index].ImageIndex = IsViewed ? 1 : 0;
                if (showViewed & IsViewed)
                    listView1.Items[index].EnsureVisible();
            }
            catch { }
        }
        /// <summary>
        /// Refresh the subtitles to review them
        /// </summary>
        public void RefreshSubtitles()
        {
            listView1.Visible = false;
            SubtitleTimingMode timingMode = SubtitleTimingMode.SecondMilli;
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0: timingMode = SubtitleTimingMode.SecondMilli; break;
                case 1: timingMode = SubtitleTimingMode.Timespan_Milli; break;
                case 2: timingMode = SubtitleTimingMode.Timespan_NTSC; break;
                case 3: timingMode = SubtitleTimingMode.Timespan_PAL; break;
                case 4: timingMode = SubtitleTimingMode.Frames_NTSC; break;
                case 5: timingMode = SubtitleTimingMode.Frames_Pal; break;
            }
            listView1.Items.Clear();
            int i = 1;
            foreach (Subtitle sub in track.Subtitles)
            {
                ListViewItem_Subtitle item = new ListViewItem_Subtitle();
                item.Number = i;
                item.Subtitle = sub;
                item.ChangeTimingView(timingMode);
                listView1.Items.Add(item);
                i++;
            }
            ViewSelectionText();
            listView1.Visible = true;
        }
        /// <summary>
        /// Clear subtitles from the list
        /// </summary>
        public void Clear()
        {
            listView1.Items.Clear();
        }
        public void SelectAll()
        {
            foreach (ListViewItem item in listView1.Items)
                item.Selected = true;
        }
        public void SelectNone()
        {
            foreach (ListViewItem item in listView1.Items)
                item.Selected = false;
        }
        public void SelectWithSameText()
        {
            if (listView1.SelectedItems.Count == 1)
            {
                ListViewItem_Subtitle selected = (ListViewItem_Subtitle)listView1.SelectedItems[0];
                foreach (ListViewItem_Subtitle sub in listView1.Items)
                {
                    sub.Selected = (sub.Subtitle.Text.ToString() == selected.Subtitle.Text.ToString());
                }
            }
        }
        public void ShowCurrentlyInView()
        {
            foreach (ListViewItem_Subtitle item in listView1.Items)
            {
                if (item.ImageIndex == 1)
                {
                    item.EnsureVisible();
                    break;
                }
            }
        }
        void ViewSelectionText()
        {
            toolStripLabel1.Text = listView1.SelectedItems.Count + " / " + listView1.Items.Count;
        }
        //Properties
        /// <summary>
        /// Get or set the subtitles track to show subtitles for
        /// </summary>
        public SubtitlesTrack SubtitlesTrack
        {
            get { return track; }
            set
            {
                track = value;
                if (track != null)
                {
                    RefreshSubtitles();
                }
                else
                {
                    listView1.Items.Clear();
                }
            }
        }
        /// <summary>
        /// Get selected subtitle items
        /// </summary>
        public ListViewItem_Subtitle[] SelectedSubtitleItems
        {
            get
            {
                List<ListViewItem_Subtitle> items = new List<ListViewItem_Subtitle>();
                foreach (ListViewItem_Subtitle item in listView1.SelectedItems)
                {
                    items.Add(item);
                }
                return items.ToArray();
            }
        }
        /// <summary>
        /// Get subtitle items
        /// </summary>
        public ListViewItem_Subtitle[] SubtitleItems
        {
            get
            {
                List<ListViewItem_Subtitle> items = new List<ListViewItem_Subtitle>();
                foreach (ListViewItem_Subtitle item in listView1.Items)
                {
                    items.Add(item);
                }
                return items.ToArray();
            }
        }
        /// <summary>
        /// Get Selected Subtitle Indices
        /// </summary>
        public int[] SelectedSubtitleIndices
        {
            get
            {
                List<int> items = new List<int>();
                foreach (int item in listView1.SelectedIndices)
                {
                    items.Add(item);
                }
                return items.ToArray();
            }
        }
        public int SubtitlesCount
        { get { return listView1.Items.Count; } }
        //Events
        public event EventHandler SubtitlesSelected;
        public event EventHandler<TimeChangeArgs> TimeChangeRequest;
        /// <summary>
        /// Rised up when the user double click on one subtitle
        /// </summary>
        public event EventHandler SubtitleEditRequest;
       
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SubtitleTimingMode timingMode = SubtitleTimingMode.SecondMilli;
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0: timingMode = SubtitleTimingMode.SecondMilli; break;
                case 1: timingMode = SubtitleTimingMode.Timespan_Milli; break;
                case 2: timingMode = SubtitleTimingMode.Timespan_NTSC; break;
                case 3: timingMode = SubtitleTimingMode.Timespan_PAL; break;
                case 4: timingMode = SubtitleTimingMode.Frames_NTSC; break;
                case 5: timingMode = SubtitleTimingMode.Frames_Pal; break;
            }
            foreach (ListViewItem_Subtitle sub in listView1.Items)
            {
                sub.ChangeTimingView(timingMode);
            }
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SelectAll();
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SelectNone();
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SelectWithSameText();
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            timer = 1;
            timer1.Start();
        }
        //Selection Timer
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timer > 0)
                timer--;
            else
            {
                timer1.Stop();
                //rise event
                if (SubtitlesSelected != null)
                    SubtitlesSelected(this, new EventArgs());
                if (listView1.SelectedItems.Count == 1)
                {
                    if (TimeChangeRequest != null)
                        TimeChangeRequest(this, new TimeChangeArgs(((ListViewItem_Subtitle)listView1.SelectedItems[0]).Subtitle.StartTime, 0));
                }
                ViewSelectionText();
            }
        }
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                if (SubtitleEditRequest != null)
                    SubtitleEditRequest(this, new EventArgs());
            }
        }
        //focus on viewd
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            ShowCurrentlyInView();
        }
        //select next
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                int index = listView1.SelectedItems[0].Index;
                index++;
                try
                {
                    listView1.SelectedItems[0].Selected = false;
                    listView1.Items[index].Selected = true;
                }
                catch 
                {
                
                }
            }
        }
        //select previous
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                int index = listView1.SelectedItems[0].Index;
                index--;
                try
                {
                    listView1.SelectedItems[0].Selected = false;
                    listView1.Items[index].Selected = true;
                }
                catch
                {

                }
            }
        }
    }
}
