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
{
    public class DbViewerModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath;

        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 bool IsJustRunning { get; set; } = false;
        


        public double Step { get; set; } = 1;

        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.Init(IsJustRunning);
                if (profilePack.ScanDatas.Count > 0) 
                {
                    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;
        }
        public string DbDirPath { get; set; } = DbHelper.DefaultDbDirPath;

        public double Step = 1;

        public double YRangePercent = 2;

        public bool IsJustRunning = false;
    }
}
