/* 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.Linq;
using System.Text;
using System.Drawing;
using System.Globalization;

namespace AHD.MKV
{
    public class IDX
    {
        public List<IDXItem> IdxItems = new List<IDXItem>();
        public List<Color> Palette = new List<Color>();
        public List<string> Languages = new List<string>();

        public IDX(string[] lines)
        {
            foreach (string line in lines)
            {
                if (line.StartsWith("timestamp:") && line.Contains("filepos:"))
                {
                    string[] parts = line.Split(new[] { ',', ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 7)
                    {
                        int hours;
                        int minutes;
                        int seconds;
                        int milliseconds;
                        if (int.TryParse(parts[1], out hours) &&
                            int.TryParse(parts[2], out minutes) &&
                            int.TryParse(parts[3], out seconds) &&
                            int.TryParse(parts[4], out milliseconds))
                        {
                            IdxItems.Add(new IDXItem(new TimeSpan(0, hours, minutes, seconds, milliseconds),
                                Convert.ToInt64(parts[6].Trim(), 16)));
                        }
                    }
                }
                else if (line.StartsWith("palette:"))
                {
                    string lineCode = line.Replace("palette: ", "");
                    string[] colors = lineCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string clr in colors)
                    {
                        Palette.Add(ColorFromString(clr.Replace(" ", "")));
                    }
                }
                else if (line.StartsWith("id:"))
                {
                    string lineCode = line.Replace("id:", "");
                    string[] langs = lineCode.Split(":, ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (langs.Length > 0)
                    {
                        try
                        {
                            string twoLetterLanguageId = langs[0];
                            CultureInfo info = CultureInfo.GetCultureInfoByIetfLanguageTag(twoLetterLanguageId);
                            Languages.Add(string.Format("{0} (0x{1:x})", info.NativeName, Languages.Count + 32));
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
        Color ColorFromString(string hex)
        {
            int r = Convert.ToInt32(hex.Substring(0, 2), 16);
            int g = Convert.ToInt32(hex.Substring(2, 2), 16);
            int b = Convert.ToInt32(hex.Substring(4, 2), 16);
            return Color.FromArgb(r, g, b);
        }
    }
}
