using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace MKVTest
{
    public partial class FrmProperties : Form
    {
        public FrmProperties()
        {
            InitializeComponent();
            GeneratePropertiesFile(@"C:\Users\glmcc\Desktop\Death Note\Black Lagoon - 1x02 - Mangrove Heaven.mkv");
            ReadPropertiesFile();
        }

        private void GeneratePropertiesFile(string filePath)
        {
            var command = "/C mkvinfo.exe \"" + filePath + "\" > properties.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)
            {

            }
        }

        private void ReadPropertiesFile()
        {
            var serializer = new SerializeMkvInfo();
            var xml = serializer.SerializeFile("properties.txt");
            var text = new StringBuilder();
            foreach (var line in xml)
            {
                text.Append(line.Trim());
            }
            ReadXml(text.ToString());
        }

        private void ReadXml(string s)
        {
            var xReader = new XmlTextReader(new StringReader(s));
            var r = new StringBuilder();
            ClearForm();
            var tags = new List<XmlTag>();
            var subGroup = new GroupBox();
            while (xReader.Read())
            {
                switch (xReader.NodeType)
                {
                    case XmlNodeType.Element:
                        var t = new XmlTag
                        {
                            Name = xReader.Name
                        };
                        tags.Add(t);
                        break;
                    case XmlNodeType.Text:
                        tags[tags.Count - 1].Value = xReader.Value;
                        break;
                    case XmlNodeType.EndElement:

                        tags.Clear();
                        break;
                }
            }

            //foreach (var tag in tags)
            //{
            //    //Foreach tag create a new row in the table layout panel.
            //    mainLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
            //    var label = new Label
            //    {
            //        Text = tag.Name //Static property name.
                    
            //    };
            //    var textBox = new TextBox
            //    {
            //        Text = tag.Value    //Allows the user to 
            //    };
            //    mainLayoutPanel.Controls.Add(label, 0, mainLayoutPanel.RowCount - 1);
            //    mainLayoutPanel.Controls.Add(textBox, 0, mainLayoutPanel.RowCount - 1);
            //}
        }

        private void BuildForm(List<XmlTag> tags)
        {
            
            foreach (var tag in tags)
            {
                
            }
        }

        private void ClearForm()
        {
            
        }
    }
}
