using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MKVTest
{
    class MkvFileInfo
    {

        public MkvFile GetAttributes(string filePath)
        {
            var file = new MkvFile { Tracks = GetShit(filePath, out MkvSegmentInformation info), SegementInformation = info };
            return file;
        }

        private static List<MkvTrack> GetShit(string filePath, out MkvSegmentInformation information)
        {
            var tracks = new List<MkvTrack>();
            information = null;
            var command = "/C mkvinfo.exe \"" + filePath + "\" > info.txt";
            var process = new System.Diagnostics.Process();
            var startInfo = new System.Diagnostics.ProcessStartInfo
            {
                WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
                FileName = "cmd.exe",
                Arguments = command
            };
            process.StartInfo = startInfo;
            process.Start();
            while (!process.HasExited)
            {

            }
            var info = File.ReadAllLines("info.txt");
            var readingSegInfo = false;
            var readingTrack = false;
            var track = new MkvTrack();
            for (var i = 0; i < info.Length; i++)
            {
                if (info[i] == "|+ Segment information")
                {
                    readingSegInfo = true;
                    readingTrack = false;
                    continue;
                }

                if (info[i] == "| + A track")
                {
                    readingTrack = true;
                    readingSegInfo = false;
                    if (track.Name == null && track.Language == null) continue;
                    tracks.Add(track);
                    track = new MkvTrack();
                }

                if (readingSegInfo)
                {
                    if (info[i].Contains("Title:"))
                    {
                        var index = info[i].IndexOf(":", StringComparison.Ordinal) + 1;
                        var seg = new MkvSegmentInformation
                        {
                            Title = info[i].Substring(index, info[i].Length - index).Trim()
                        };
                        information = seg;
                    }
                }

                if (readingTrack)
                {
                    if (info[i].Contains("Name:"))
                    {
                        var index = info[i].IndexOf(":", StringComparison.Ordinal) + 1;
                        track.Name = info[i].Substring(index, (info[i].Length - index)).Trim();
                    }

                    if (info[i].Contains("Language:"))
                    {
                        var index = info[i].IndexOf(":", StringComparison.Ordinal) + 1;
                        track.Language = info[i].Substring(index, (info[i].Length - index)).Trim();
                    }

                    if (info[i].Contains("Video track"))
                    {
                        track.Type = MkvTrack.TrackType.Video;
                    }
                    if (info[i].Contains("Audio track"))
                    {
                        track.Type = MkvTrack.TrackType.Audio;
                    }
                    if (info[i].Contains("Track type: subtitles"))
                    {
                        track.Type = MkvTrack.TrackType.Subtitle;
                    }
                }
            }
            if (information == null)
            {
                information = new MkvSegmentInformation();  
            }

            return tracks;
        }
    }
}
