using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using FLY.Thick.Base.IService;
using System.IO;
using FLY.Thick.Base.Common;
using FObjBase;

namespace FLY.Thick.Base.Server
{
    public class Profile : IProfileService, INotifyPropertyChanged, Misc.ISaveToXml
    {

        #region 属性,成员变量的代理
        string pname = "test_pname";
        public string PName
        {
            set
            {
                if (pname != value)
                {
                    pname = value;
                    NotifyPropertyChanged("PName");
                }
            }
            get { return pname; }
        }
        int target = 3000;
        public int Target
        {
            get { return target; }
            set
            {
                if (target != value)
                {
                    target = value;
                    NotifyPropertyChanged("Target");
                }
            }
        }
        int alarm = 100;
        public int Alarm
        {
            get { return alarm; }
            set
            {
                if (alarm != value)
                {
                    alarm = value;
                    NotifyPropertyChanged("Alarm");
                }
            }
        }

        double comp = 1;
        public double Comp
        {
            get { return comp; }
            set
            {
                comp = value;
                NotifyPropertyChanged("Comp");
            }
        }

        int shift = 0;
        public int Shift
        {
            get { return shift; }
            set
            {
                if (shift != value)
                {
                    shift = value;
                    NotifyPropertyChanged("Shift");
                }
            }
        }

        int beginNo = 0;
        public int BeginNo
        {
            get { return beginNo; }
            set
            {
                if (beginNo != value)
                {
                    beginNo = value;
                    NotifyPropertyChanged("BeginNo");
                }
            }
        }

        int endNo = 40;
        public int EndNo
        {
            get { return endNo; }
            set
            {
                if (endNo != value)
                {
                    endNo = value;
                    NotifyPropertyChanged("EndNo");
                }
            }
        }

        int dataBeginNo = 2;
        public int DataBeginNo
        {
            get { return dataBeginNo; }
            set
            {
                if (dataBeginNo != value)
                {
                    dataBeginNo = value;
                    NotifyPropertyChanged("DataBeginNo");
                }
            }
        }

        int dataEndNo = 30;
        public int DataEndNo
        {
            get { return dataEndNo; }
            set
            {
                if (dataEndNo != value)
                {
                    dataEndNo = value;
                    NotifyPropertyChanged("DataEndNo");
                }
            }
        }


        #endregion


        public Profile()
        {

        }
        public Profile(string path)
        {
            if (path == null)
                Load();
            else
                Load(path);
        }
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region ISaveToXml 成员

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "PName",
                "Target",
                "Alarm",
                "YRange",
                "Comp",
                "Shift",
                "DataBeginNo",
                "DataEndNo",
                "BeginNo",
                "EndNo"};
        }

        #endregion

        public bool Load() 
        {
            return Misc.SaveToXmlHepler.Load("profile.xml", this);
        }
        bool Load(string path)
        {
            return Misc.SaveToXmlHepler.Load(path, this);
        }

        bool Save()
        {
            bool ret2 = Misc.SaveToXmlHepler.Save(@"profile.xml", this);
            bool ret1 = Misc.SaveToXmlHepler.Save(@"profile\"+PName+".xml", this);
            return (ret1 && ret2);
        }

        /// <summary>
        /// 应用 &amp; 保存
        /// </summary>
        public void Apply() 
        {
            Save();
        }



        /// <summary>
        /// 获取产品列表, 返回类型为 List&lt;string&gt;
        /// </summary>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState) 
        {
            DirectoryInfo dinfo = new DirectoryInfo(@"profile");
            List<string> filenames = new List<string>();
            if (dinfo.Exists)
            {
                
                foreach (FileInfo info in dinfo.GetFiles())
                {
                    if (Path.GetExtension(info.Name) == ".xml")
                    {
                        filenames.Add(Path.GetFileNameWithoutExtension(info.Name));
                    }
                }
            }
            AsyncDelegate(AsyncState, filenames);
        }

        /// <summary>
        /// 删除指定产品
        /// </summary>
        /// <param name="productname"></param>
        public void Del(string productname) 
        {
            string path = @"profile\" + productname + ".xml";
            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);
            
        }

        /// <summary>
        /// 读取指定产品,返回类型为 ProfileParam
        /// </summary>
        /// <param name="productname"></param>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState) 
        {
            Profile p = new Profile();
            p.Load(@"profile\" + productname + ".xml");

            ProfileParam pp = new ProfileParam()
            {
                pname = p.PName,
                target = p.Target,
                alarm = p.Alarm,
                comp = p.Comp,
                shift = p.Shift,
                beginNo = p.BeginNo,
                endNo = p.EndNo,
                dataBeginNo = p.DataBeginNo,
                dataEndNo = p.DataEndNo
            };
            AsyncDelegate(AsyncState, pp);
        }
    }
}