using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Misc;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Xml.Serialization;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Server
{
    //代替TDInitParam
    public class SysParam : INotifyPropertyChanged, ISaveToXml
    {
        #region 属性,成员变量的代理

        /// <summary>
        /// 本机obj服务地址
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public IPEndPoint LocalEP { get; set; } = Misc.StringConverter.ToIPEndPoint("127.0.0.1:20006");

        /// <summary>
        /// AD盒地址
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public IPEndPoint FLYAD7EP { get; set; } = Misc.StringConverter.ToIPEndPoint("192.168.250.10:20006");

        /// <summary>
        /// AD盒通讯使用 CRC校验
        /// </summary>
        public bool FLYAD7_HasCRC { get; set; } = false;
        
        /// <summary>
        /// grid 平滑
        /// </summary>
        public int FLYAD7_GridSmooth { get; set; } = 0;

        /// <summary>
        /// 数据库地址,只读,界面没法修改
        /// </summary>
        public string DBPath { get; set; } = @"D:\flydata\thick_history.sqlite3";
        #endregion

        private string param_path = "sysparam.xml";
        public SysParam()
        {
            this.PropertyChanged += new PropertyChangedEventHandler(SysParam_PropertyChanged);
        }
        public SysParam(string param_path) 
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;
            Load();
            this.PropertyChanged += new PropertyChangedEventHandler(SysParam_PropertyChanged);
        }
        void SysParam_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (isLoading)
                return;
            if ((e.PropertyName == "FLYAD7EP")||
                (e.PropertyName == "FLYAD7_HasCRC")||
                (e.PropertyName == "FLYAD7_GridSmooth")
            )
            {
                Save();
            }
        }
        bool isLoading = false;
        public bool Load()
        {
            isLoading = true;
            bool ret = Misc.SaveToXmlHepler.Load(param_path, this);
            isLoading = false;
            return ret;
        }

        public void Save()
        {
            Misc.SaveToXmlHepler.Save(param_path, this);

        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public string[] GetSavePropertyNames()
        {
            return new string[]{
            "LocalEP",
            "FLYAD7EP",
            "FLYAD7_HasCRC",
            "FLYAD7_GridSmooth",
            "DBPath"
            };
        }
    }
}