using JWH;
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;
using System.Xml.Linq;

namespace DDUtilityApp.ETC
{

    public partial class FrmTIbcoConfig : Form
    {

        public FrmTIbcoConfig()
        {
            InitializeComponent();
            this.SetLayout();
            this.SetEventHandler();
        }

        private void SetLayout()
        {
            this.textBox1.Font = new Font("돋움체", 9.0F);
            this.textBox1.ScrollBars = ScrollBars.Both;
            this.textBox2.Font = new Font("돋움체", 9.0F);
            this.textBox2.ScrollBars = ScrollBars.Both;
        }

        private void SetEventHandler()
        {
            // DragDrop
            this.textBox1.AllowDrop = true;
            this.textBox1.DragDrop += Control_DragDrop;
            this.textBox1.DragEnter += Control_DragEnter;
            this.textBox2.AllowDrop = true;
            this.textBox2.DragDrop += Control_DragDrop;
            this.textBox2.DragEnter += Control_DragEnter;

            this.btnSave.Click += Button1_Click;
        }

        private void Control_DragDrop(object sender, DragEventArgs e)
        {
            XLogger.Instance.Info(e);

            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files == null || files.Length < 1) return;

            string fileName = files[0];
            this.SetText(this.textBox1, fileName);
            this.SetTextSort(this.textBox2, fileName);
        }

        private void Control_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            this.SaveDocument(this.textBox2, this.tboxFilename.Text);
        }

        private void SetText(object sender, string fileName)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;
            textBox.Clear();
            this.tboxFilename.Text = fileName;

            XmlDocument document = new XmlDocument();
            document.Load(fileName);

            using (MemoryStream stream = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Encoding = new UTF8Encoding(false),
                    Indent = true,
                    IndentChars = "    ",
                    NewLineChars = Environment.NewLine,
                    NewLineHandling = NewLineHandling.Replace
                };
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    document.Save(writer);
                }
                textBox.Text = Encoding.UTF8.GetString(stream.ToArray());
            }
        }

        private void SetTextSort(object sender, string fileName)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;
            this.tboxFilename.Text = fileName;

            XmlDocument document = new XmlDocument();
            document.Load(fileName);

            XmlNodeList messageNameList = document.GetElementsByTagName("messageNameList");
            List<XmlNode> list = new List<XmlNode>();
            foreach (XmlNode nodeList in messageNameList)
            {
                foreach (XmlNode node in nodeList.ChildNodes)
                    list.Add(node);

                nodeList.RemoveAll();
                foreach (XmlNode node in list.OrderBy(e => e.Name))
                    nodeList.AppendChild(node);
            }

            using (MemoryStream stream = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Encoding = new UTF8Encoding(false),
                    Indent = true,
                    IndentChars = "    ",
                    NewLineChars = Environment.NewLine,
                    NewLineHandling = NewLineHandling.Replace
                };
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    document.Save(writer);
                }
                textBox.Text = Encoding.UTF8.GetString(stream.ToArray());
            }
        }

        private void SetText1(object sender, string fileName)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;
            this.tboxFilename.Text = fileName;

            XmlDocument document = new XmlDocument();
            document.Load(fileName);
            textBox.Text = document.GetBeautify();
        }

        private void SaveDocument(object sender, string filename)
        {
            TextBox textBox = sender as TextBox;
            if (textBox == null) return;

            using (StreamWriter writer = new StreamWriter(filename))
            {
                writer.Write(textBox.Text);
            }

            MessageBox.Show($"저장하였습니다.");
        }

    }

}
