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;
using Newtonsoft.Json;

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

        /// <summary>
        /// 本机obj服务地址
        /// </summary>
        public string Addr { get; set; } = "0.0.0.0:20006";

        #endregion

        private string param_path = "sysparam.json";
        public SysParam()
        {

        }
        public SysParam(string param_path) 
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;
            if (!Load()) {
                Save();
            }
        }


        bool Load()
        {
            try
            {
                if (File.Exists(param_path))
                {
                    string json = File.ReadAllText(param_path);
                    JsonConvert.PopulateObject(json, this);
                    return true;
                }
            }
            catch
            {
                //异常，没有json 解码失败
            }

            return false;
        }
        public bool Save()
        {
            try
            {
                File.WriteAllText(param_path, JsonConvert.SerializeObject(this, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常，没有json 编码失败

            }
            return false;

        }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


    }
}
