using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Newtonsoft.Json;
using static LanguageExtractTool.Form1;
using System.Xml.Linq;

namespace LanguageExtractTool
{
    public partial class FleetInspection : Form
    {
        public FleetInspection()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txt_src.Text = ofd.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txt_destdir.Text = fbd.SelectedPath;
            }
        }


        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txt_src.Text))
            {
                MessageBox.Show("请选择源文件");
                return;
            }
            if (string.IsNullOrWhiteSpace(txt_destdir.Text))
            {
                MessageBox.Show("请选择要存放资源文件的文件夹");
                return;
            }
            string destpath = txt_destdir.Text.Trim();
            XmlDocument doc = new XmlDocument();
            doc.Load(txt_src.Text);
            Dictionary<string, ResourceObject> dics = new Dictionary<string, ResourceObject>(StringComparer.OrdinalIgnoreCase);
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                string code = node.Name;
                foreach (XmlNode node1 in node.ChildNodes)
                {
                    if (string.IsNullOrWhiteSpace(node1.InnerText))
                    {
                        continue;
                    }
                    string lgid = node1.Name;
                    ResourceObject res = null;
                    if (dics.TryGetValue(lgid, out res))
                    {
                        res.Values[code] = node1.InnerText;
                    }
                    else
                    {
                        res = new ResourceObject();
                        res.LanguageId = lgid;
                        dics.Add(lgid, res);
                        res.Values[code] = node1.InnerText;
                    }
                }
            }

            foreach (var dic in dics)
            {
                string filename = dic.Key;
                if (dic.Key == "en-us")
                    filename = "en";
                else if (dic.Key == "fr-fr")
                    filename = "fr";
                else if (dic.Key == "es-es")
                    filename = "es";

                string fn = Path.Combine(txt_destdir.Text.Trim(), filename + ".xml");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
                XmlElement root = xmlDoc.CreateElement("root");
                xmlDoc.AppendChild(root);
                foreach (var item in dic.Value.Values)
                {
                    XmlElement el = xmlDoc.CreateElement(item.Key);
                    el.InnerText = item.Value;
                    root.AppendChild(el);
                }

                xmlDoc.Save(fn);
            }

            MessageBox.Show("分解完成。");
        }



    }
}