using AutoMapper;
using FLY.Thick.FilmCasting.Server.Model;
using FLY.Thick.FilmCasting.UI.DbViewer.Db;
using Misc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace FLY.Thick.FilmCasting.UI.DbViewer.Core
{
    //整个DbViewer软件的运行核心  
    public class DbViewerModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;


        public List<DbProfilePack> ProfilePacks { get; } = new List<DbProfilePack>();
        /// <summary>
        /// 数据准备好了
        /// </summary>
        public bool IsPackReady { get; private set; }

        public DateTime PackBeginDate { get; private set; }
        public DateTime PackEndDate { get; private set; }




        /// <summary>
        /// ProfilePacks.Count()
        /// </summary>
        public int PackCnt { get; private set; }

        /// <summary>
        /// 数据库根目录路径
        /// </summary>
        public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath;

        /// <summary>
        /// 直方图统计步距 1um
        /// </summary>
        public int Step { get; set; } = 1;

        /// <summary>
        /// 扫描图显示的范围比例 3倍
        /// </summary>
        public double YRangePercent { get; set; } = 3;

        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix { get; set; } = 4;



        public DbHelper mDbHelper;
        public DbViewerModel()
        {
        }
        public void Init()
        {
            if (!Load())
                Save();
            mDbHelper = new DbHelper();
            mDbHelper.Init(DbDirPath, null);
        }
        public bool GetProfilePacks(List<Db_Profile> profiles)
        {
            ProfilePacks.Clear();
            foreach (var db_profile in profiles)
            {
                var profilePack = mDbHelper.GetProfilePack(db_profile);
                profilePack.Mix = Mix;
                profilePack.YRangePercent = YRangePercent;
                profilePack.Step = Step;
                if (profilePack.Init())
                { 
                    ProfilePacks.Add(profilePack);
                }
            }
            if (ProfilePacks.Count() > 0)
            {
                IsPackReady = true;
                PackCnt = ProfilePacks.Count();
                PackBeginDate = ProfilePacks.First().Profile.StartTime;
                PackEndDate = ProfilePacks.Last().Profile.EndTime;
                return true;
            }
            else {
                IsPackReady = false;
                return false;
            }

        }

        private string filePath = "param.json";
        bool Load()
        {
            return DbViewerModelJsonDb.Load(this, filePath);
        }
        public bool Save()
        {
            return DbViewerModelJsonDb.Save(this, filePath);
        }

    }


    public class DbViewerModelJsonDb
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<DbViewerModel, DbViewerModelJsonDb>().ReverseMap();
        }));
        public static bool Save(DbViewerModel src, string filePath)
        {
            var p = Mapper.Map<DbViewerModelJsonDb>(src);
            try
            {
                File.WriteAllText(filePath, JsonConvert.SerializeObject(p, Formatting.Indented));
            }
            catch
            {
                //异常，没有json 编码失败
                return false;
            }
            return true;
        }
        public static bool Load(DbViewerModel src, string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    string json = File.ReadAllText(filePath);
                    var p = JsonConvert.DeserializeObject<DbViewerModelJsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常，没有json 解码失败
            }
            return false;
        }

        /// <summary>
        /// 数据库根目录路径
        /// </summary>
        public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath;

        /// <summary>
        /// 直方图统计步距 1um
        /// </summary>
        public int Step = 1;

        /// <summary>
        /// 扫描图显示的范围比例 3倍
        /// </summary>
        public double YRangePercent = 3;

        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix { get; set; } = 4;
    }
}
