using AutoMapper;
using FLY.Thick.FilmCasting.Common;
using FLY.Thick.FilmCasting.IService;
using FObjBase;
using FObjBase.Reflect;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

namespace FLY.Thick.FilmCasting.Server
{

    /// <summary>
    /// 
    /// </summary>
    public class FilmCastingProfile : IFilmCastingProfileService
    {
        /// <summary>
        /// 参数列表！！！！！
        /// </summary>
        FilmCastingProfileListJsonDb profileDb = new FilmCastingProfileListJsonDb();
        /// <summary>
        /// 
        /// </summary>
        [PropertyPush]
        public FilmCastingProfileParam Param { get; } = new FilmCastingProfileParam();


        /// <summary>
        /// 自动下辊条件 平均值变化 10um
        /// </summary>
        public double RollDown_AvgChanged { get; set; } = 10;
        /// <summary>
        /// 自动下辊条件 膜宽度变化 100mm
        /// </summary>
        public double RollDown_FilmWidthChanged { get; set; } = 100;
        /// <summary>
        /// 自动下辊条件 生产长度 大于 10000m
        /// </summary>
        public double RollDown_FilmLength { get; set; } = 10000;
        /// <summary>
        /// 自动下辊条件 生产时间 2小时
        /// </summary>
        public double RollDown_TimeoutHour { get; set; } = 2;
        /// <summary>
        /// 自动下辊条件 停止时间 大于 10min
        /// </summary>
        public double RollDown_StopTimeMin { get; set; } = 10;


        private string file_path = "profile.json";

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 
        /// </summary>
        public FilmCastingProfile()
        {
            LoadList();
            Load();
        }

        
        

        bool Load() 
        {
            return FilmCastingProfileJsonDb.Load(this, "profile2.json");
        }
        bool Save()
        {
            return FilmCastingProfileJsonDb.Save(this, "profile2.json");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        bool LoadList()
        {
            try
            {
                if (File.Exists(file_path))
                {
                    string json = File.ReadAllText(file_path);
                    profileDb = JsonConvert.DeserializeObject<FilmCastingProfileListJsonDb>(json);
                    var param = profileDb.ParamList.Find((p) => p.PName == profileDb.CurrentPName);
                    if (param != null)
                    {
                        Param.Copy(param);
                    }
                    return true;
                }
            }
            catch
            {
                //异常，没有json 解码失败

            }
            return false;
        }

        

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        bool SaveList()
        {
            try
            {
                File.WriteAllText(file_path, JsonConvert.SerializeObject(profileDb, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常，没有json 编码失败

            }
            return false;
        }



        /// <summary>
        /// 应用 &amp; 保存
        /// </summary>
        public void ApplyParam(FilmCastingProfileParam param)
        {
            Param.Copy(param);
            profileDb.CurrentPName = Param.PName;
            FilmCastingProfileParam param2 = profileDb.ParamList.Find((p) => p.PName == Param.PName);
            if (param2 == null)
            {
                profileDb.ParamList.Add((FilmCastingProfileParam)Param.Clone());
            }
            else
            {
                param2.Copy(Param);
            }
            SaveList();
        }

        /// <summary>
        /// 获取产品列表, 返回类型为 List&lt;string&gt;
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void GetList(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            IEnumerable<string> names = from p in profileDb.ParamList select p.PName;
            asyncDelegate(asyncContext, names);
        }

        /// <summary>
        /// 删除指定产品
        /// </summary>
        /// <param name="productname"></param>
        public void Del(string productname)
        {
            profileDb.ParamList.RemoveAll((p) => p.PName == productname);

        }

        /// <summary>
        /// 读取指定产品,返回类型为 ProfileParam
        /// </summary>
        /// <param name="productname"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void Read(string productname, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            var p = profileDb.ParamList.Find((_p) => _p.PName == productname);

            asyncDelegate(asyncContext, p);
        }

        public void Apply() {
            Save();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class FilmCastingProfileListJsonDb
    {
        /// <summary>
        /// 当前正在使用的产品参数
        /// </summary>
        public string CurrentPName { get; set; }
        /// <summary>
        /// 参数列表！！！！！
        /// </summary>
        public List<FilmCastingProfileParam> ParamList { get; set; } = new List<FilmCastingProfileParam>();
    }
    public class FilmCastingProfileJsonDb
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<FilmCastingProfile, FilmCastingProfileJsonDb>().ReverseMap();
        }));

        public static bool Load(FilmCastingProfile src, string file_path)
        {
            try
            {
                if (File.Exists(file_path))
                {
                    string json = File.ReadAllText(file_path);
                    var p = JsonConvert.DeserializeObject<FilmCastingProfileJsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常，没有json 解码失败
            }

            return false;
        }
        public static bool Save(FilmCastingProfile src, string file_path)
        {
            var p = Mapper.Map<FilmCastingProfileJsonDb>(src);
            try
            {
                File.WriteAllText(file_path, JsonConvert.SerializeObject(p, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常，没有json 编码失败

            }
            return false;

        }


        /// <summary>
        /// 自动下辊条件 平均值变化 10um
        /// </summary>
        public double RollDown_AvgChanged { get; set; } = 10;
        /// <summary>
        /// 自动下辊条件 膜宽度变化 100mm
        /// </summary>
        public double RollDown_FilmWidthChanged { get; set; } = 100;
        /// <summary>
        /// 自动下辊条件 生产长度 大于 10000m
        /// </summary>
        public double RollDown_FilmLength { get; set; } = 10000;
        /// <summary>
        /// 自动下辊条件 生产时间 2小时
        /// </summary>
        public double RollDown_TimeoutHour { get; set; } = 2;
        /// <summary>
        /// 自动下辊条件 停止时间 大于 10min
        /// </summary>
        public double RollDown_StopTimeMin { get; set; } = 10;

    }
}
