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 属性，成员变量的代理
        private IPEndPoint m_IPEP_server = Misc.StringConverter.ToIPEndPoint("127.0.0.1:20006");
        public IPEndPoint LocalEP 
        {
            get 
            {
                return m_IPEP_server;
            }
            set
            {
                m_IPEP_server = value;
                NotifyPropertyChanged("LocalEP");
            }
        }

        private IPEndPoint m_IPEP_flyad7_device = Misc.StringConverter.ToIPEndPoint("192.168.250.10:20006");
        public IPEndPoint FLYAD7EP
        {
            get
            {
                return m_IPEP_flyad7_device;
            }
            set
            {
                if (value == null)
                    return;
                
                if (!m_IPEP_flyad7_device.Equals(value))
                {
                    m_IPEP_flyad7_device = new IPEndPoint(value.Address, value.Port);
                    NotifyPropertyChanged("FLYAD7EP");
                    //Save();
                }
            }
        }

        private bool flyad7_hascrc = false;
        public bool FLYAD7_HasCRC 
        {
            get { return flyad7_hascrc; }
            set {
                if (flyad7_hascrc != value) 
                {
                    flyad7_hascrc = value;
                    NotifyPropertyChanged("FLYAD7_HasCRC");
                }
            }
        }

        private int flyad7_grid_smooth = 0;
        /// <summary>
        /// grid 平滑
        /// </summary>
        public int FLYAD7_GridSmooth
        {
            get { return flyad7_grid_smooth; }
            set
            {
                if (flyad7_grid_smooth != value)
                {
                    flyad7_grid_smooth = value;
                    NotifyPropertyChanged("FLYAD7_GridSmooth");
                }
            }
        }
        #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;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

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