using FLY.OBJComponents.Server;
using FLY.OBJComponents.Server.Model;
using FLY.Thick.FilmCasting.IService;
using FObjBase;
using FObjBase.Reflect;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.FilmCasting.Server.Model
{
    /// <summary>
    /// 数据库 写操作
    /// </summary>
    public class HistoryDb : IShareDbService
    {
        /// <summary>
        /// 本地数据库
        /// </summary>
        public LocalDb localDb;
        DbModel dbModel;
        public BufferError ErrorBuffer;

        #region IShareDbService
        [Push(typeof(ProfileChangedEventArgs))]
        public event EventHandler ProfileChanged;

        [Push(typeof(ScanDataAddedEventArgs))]
        public event EventHandler ScanDataAdded;
        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dBModel"></param>
        /// <param name="orgDBModel"></param>
        /// <param name="localDb"></param>
        public void Init(DbModel dBModel, LocalDb localDb)
        {
            this.dbModel = dBModel;
            this.localDb = localDb;
            ErrorBuffer = new BufferError();
            ErrorBuffer.Init(dBModel.TbError);
        }




        /// <summary>
        /// 保存产品参数
        /// </summary>
        public void AddProfile(
            Db_Profile profile
            )
        {
            //把上一次结束掉
            if ((localDb.CurrProfile != null) && localDb.IsProfileFinished == false)
            {
                FinishProfile();
            }

            //添加 数据库必要的 field
            localDb.ProfileStartTime = DateTime.Now;
            localDb.ProfileEndTime = DateTime.Now;
            localDb.IsProfileFinished = false;

            profile.ID = dbModel.TbProfile.FreeID;
            profile.StartTime = localDb.ProfileStartTime;
            profile.EndTime = localDb.ProfileEndTime;


            //SQL
            List<string> sqls = new List<string>();
            sqls.Add(SQLiteHelper.GetInsertCommandText(profile));

            dbModel.sqliteHelper.QueryTranAsync(sqls);


            //本地数据备份
            localDb.CurrProfile = profile;

            #region IDBShareService
            ProfileChanged?.Invoke(this, new ProfileChangedEventArgs()
            {
                isInsert = true,
                profile = profile
            });
            #endregion
        }

        /// <summary>
        /// 修改产品参数
        /// </summary>
        /// <param name="profile"></param>
        public void UpdateProfile(
                Db_Profile profile
            )
        {
            //添加 数据库必要的 field
            profile.ID = localDb.CurrProfile.ID;
            profile.StartTime = localDb.ProfileStartTime;
            profile.EndTime = localDb.ProfileEndTime;

            //SQL
            List<string> sqls = new List<string>();
            sqls.Add(SQLiteHelper.GetUpdateCommandText(profile, $"WHERE {nameof(Db_Profile.ID)} = {profile.ID}"));

            dbModel.sqliteHelper.QueryTranAsync(sqls);


            //本地数据备份
            localDb.CurrProfile = profile;

            #region IDBShareService
            ProfileChanged?.Invoke(this, new ProfileChangedEventArgs()
            {
                isInsert = false,
                profile = profile
            });
            #endregion
        }

        /// <summary>
        /// 下辊，结束当前产品生产
        /// </summary>
        public void FinishProfile()
        {

            if (localDb.CurrProfile == null)
                return;

            //SQL
            dbModel.sqliteHelper.ExecuteNonQuery(
                $"UPDATE {dbModel.TbProfile.TableName} SET IsFinished = {true} WHERE ID = {localDb.CurrProfile.ID}");

            localDb.IsProfileFinished = true;

            localDb.CurrProfile.IsFinished = true;

            #region IDBShareService
            ProfileChanged?.Invoke(this, new ProfileChangedEventArgs()
            {
                isInsert = false,
                profile = localDb.CurrProfile
            });
            #endregion
        }

        /// <summary>
        /// 不需设置 DB_ScanData 内 ProfileID, 自动设置！
        /// </summary>
        public void AddScanData(
            Lc_ScanData scanData)
        {

            bool updateEnd = false;
            //添加 数据库必要的 field
            scanData.ID = dbModel.TbScanData.FreeID;


            if (localDb.ProfileStartTime.Month != scanData.EndTime.Month)
            {
                // 跨月必须换产品
                AddProfile(localDb.CurrProfile);
            }

            if (localDb.ProfileEndTime < scanData.Time)
            {
                localDb.ProfileEndTime = scanData.Time;
                updateEnd = true;
            }

            //SQLs
            List<string> sqls = new List<string>();
            var db_scandata = Lc_AutoMapperProfile.Mapper.Map<Db_ScanData>(scanData);
            sqls.Add(SQLiteHelper.GetInsertCommandText(db_scandata));

            if (updateEnd)
            {
                sqls.Add(
                    $"UPDATE {dbModel.TbProfile.TableName}" +
                    $" SET {nameof(Db_Profile.EndTime)}={localDb.ProfileEndTime.ToStringOfSQLiteFieldType()}" +
                    $" WHERE {nameof(Db_Profile.ID)}={localDb.CurrProfile.ID}");
            }
            dbModel.sqliteHelper.QueryTranAsync(sqls);

            //本地数据备份
            localDb.CurrProfile.EndTime = localDb.ProfileEndTime;


            #region IDBShareService
            ScanDataAdded?.Invoke(this, new ScanDataAddedEventArgs()
            {
                scandata = scanData
            });
            if (updateEnd)
            {
                ProfileChanged?.Invoke(this, new ProfileChangedEventArgs()
                {
                    isInsert = false,
                    profile = localDb.CurrProfile
                });
            }
            #endregion
        }


        /// <summary>
        /// 不需设置 DB_ScanData 内 ProfileID, 自动设置！
        /// </summary>
        public void AddSample(
            Lc_Sample sampleData)
        {
            //添加 数据库必要的 field
            sampleData.ID = dbModel.TbSample.FreeID;

            //SQLs
            List<string> sqls = new List<string>();
            var db_sampledata = Lc_AutoMapperProfile.Mapper.Map<Db_Sample>(sampleData);
            sqls.Add(SQLiteHelper.GetInsertCommandText(db_sampledata));

            dbModel.sqliteHelper.QueryTranAsync(sqls);

            //本地数据备份
            localDb.CurrProfile.EndTime = localDb.ProfileEndTime;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db_errorData"></param>
        public void AddErrorData(
            Db_Error db_errorData
            )
        {
            //添加 数据库必要的 field
            db_errorData.ID = dbModel.TbError.FreeID;

            //SQLs
            List<string> sqls = new List<string>();
            sqls.Add(SQLiteHelper.GetInsertCommandText(db_errorData));

            dbModel.sqliteHelper.QueryTranAsync(sqls);
        }

        #region IDBShareService
        /// <summary>
        /// 
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void GetProfile(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            if (localDb.CurrProfile != null)
            {
                asyncDelegate.Invoke(asyncContext, localDb.CurrProfile);
            }
            else
            {
                asyncDelegate.Invoke(asyncContext, null);
            }
        }
        #endregion
    }
}
