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>
        FilmCastingProfileJsonDb profileDb = new FilmCastingProfileJsonDb();
        /// <summary>
        /// 
        /// </summary>
        [PropertyPush]
        public FilmCastingProfileParam Param { get; } = new FilmCastingProfileParam();


        private string file_path = "profile.json";

        public event PropertyChangedEventHandler PropertyChanged;

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


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool Load()
        {
            try
            {
                if (File.Exists(file_path))
                {
                    string json = File.ReadAllText(file_path);
                    profileDb = JsonConvert.DeserializeObject<FilmCastingProfileJsonDb>(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 Save()
        {
            try
            {
                File.WriteAllText(file_path, JsonConvert.SerializeObject(profileDb, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常，没有json 编码失败

            }
            return false;
        }

        /// <summary>
        /// 应用 &amp; 保存
        /// </summary>
        public void Apply(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);
            }
            Save();
        }

        /// <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);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class FilmCastingProfileJsonDb
    {
        /// <summary>
        /// 当前正在使用的产品参数
        /// </summary>
        public string CurrentPName { get; set; }
        /// <summary>
        /// 参数列表！！！！！
        /// </summary>
        public List<FilmCastingProfileParam> ParamList { get; set; } = new List<FilmCastingProfileParam>();



    }
}
