using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Xml;
using System.IO;
using FileManager;

namespace MeasureData
{
    public class ConfigFile : ISlo
    {
        #region 内容
        //文件名
        private string m_fileName;
        public string FileName
        {
            get { return this.m_fileName; }
            set { this.m_fileName = value; }
        }

        //文件路径
        private string m_filepath;
        public string FilePath
        {
            get { return this.m_filepath; }
            set { this.m_filepath = value; }
        }

        public MeasureParam m_Config;
        #endregion

        public ConfigFile(MeasureParam mp)
        {
            this.m_Config = mp;
        }


        /// <summary>
        /// 创建XML文件
        /// </summary>
        /// <param name="filename">创建XML文件的全路径</param>
        /// <returns>0：失败；1：成功；2：文件已存在</returns>
        public int CreateXml(String filename)
        {
            if (!File.Exists(filename))
            {
                if (XmlManager.CreateXmlFile(filename))
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                XmlManager.CreateXmlFile(filename);
                return 2;
            }
        }

        //保存
        public bool Save(String filename)
        {
            CreateXml(filename);
            XmlDocument doc = new XmlDocument();
            if (File.Exists(filename))
            {
                doc.Load(filename);//载入xml文件               
            }
            else
            {
                return false; //当前路径不存在               
            }

            XmlNode root = doc.SelectSingleNode("XMLData");
            Serialize(true, doc, root);
            doc.Save(filename);

            return true;
        }

        //读取
        public bool Read(String filename)
        {
            XmlDocument doc = new XmlDocument();
            if (File.Exists(filename))
            {
                doc.Load(filename);//载入xml文件               
            }
            else
            {
                return false; //当前路径不存在               
            }

            XmlNode root = doc.SelectSingleNode("XMLData");
            Serialize(false, doc, root);
            //doc.Save(filename);
            return true;
        }

        //XML文件保存
        //样品孔存储xml文档
        public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
        {
            Slo sFile = new Slo();

            xString filename = new xString();
            xString filepath = new xString();
            filename.AssignValue(this.m_fileName);
            filepath.AssignValue(this.m_filepath);
            sFile.Register("FileName", filename);
            sFile.Register("FilePath", filepath);

            sFile.Register("Param", this.m_Config);

            if (isStoring)
            {
                sFile.Serialize(true, xml, rootNode);
            }
            else
            {
                sFile.Serialize(false, xml, rootNode);
                this.m_fileName = filename.value();
                this.m_filepath = filepath.value();
            }
        }
    }
}
