using SinoChemCommonUtilities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wayne.Lib.Log;

namespace SinochemInternetPlusApp
{
    // the sql command should macth the db schema in the target fusion
    class MultiFusionsSupport
    {
        private static SqlCommandUtility sourceSqlCmd;
        private static SqlCommandUtility targetSqlCmd;

        private static readonly bool supportMultiFusions = ConfigurationValues.SupportMultiFusions;

        static MultiFusionsSupport()
        {
            try
            {
                if (supportMultiFusions)
                {
                    sourceSqlCmd = new SqlCommandUtility(GenericSinochemEpsApp.AppSettings["AnotherPosDatabaseConnStr"]); // default to the conn string to current machine

                    string anotherConnStr = GenericSinochemEpsApp.AppSettings["AnotherPosDatabaseConnStr"];
                    targetSqlCmd = new SqlCommandUtility(anotherConnStr); // conn string to another fusion
                }
            }
            catch(Exception ex)
            {
            }
        }

        public static bool CopyXiaofei2ToTargetFusion(int jihao, string liushuihao, DebugLogger debugLogger)
        {
            if(!supportMultiFusions)
            {
                return false;
            }

            try
            {
                string querySql = string.Format(" select * from xiaofei2 " +
                                                " where jihao={0} and liushuino='{1}' ",
                                                jihao, liushuihao);
                Xiaofei2Model xiaofei2Model = sourceSqlCmd.ReadData<Xiaofei2Model>(querySql, DbModelConstructors.ConstructXiaofei2Model);
                if (xiaofei2Model != null)
                {
                    string insertSql = string.Format(" insert into xiaofei2 (jihao, youpin, qty, danjia, amount, xf_date, xf_time, liushuino, fzqty, fzamount) " +
                                                " values({0}, '{1}', {2}, {3}, {4}, '{5}', '{6}', '{7}', {8}, {9}, '{10}') ",
                                                xiaofei2Model.jihao, xiaofei2Model.youpin, xiaofei2Model.qty, xiaofei2Model.danjia, xiaofei2Model.amount, xiaofei2Model.xf_date, xiaofei2Model.xf_time,
                                                xiaofei2Model.liushuino, xiaofei2Model.fzqty, xiaofei2Model.fzamount, xiaofei2Model.plate_number);
                    targetSqlCmd.Insertdata(insertSql, false);

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                debugLogger.Add(ex);
                return false;
            }
        }

        public static bool DeleteXiaofei2FromTargetFusion(int jihao, string liushuihao, DebugLogger debugLogger)
        {
            if (!supportMultiFusions)
            {
                return false;
            }

            try
            {
                string querySql = string.Format(" delete from xiaofei2 " +
                                                " where jihao={0} and liushuino='{1}' ",
                                                jihao, liushuihao);

                targetSqlCmd.DeleteData(querySql);
                return true;
            }
            catch (Exception ex)
            {
                debugLogger.Add(ex);
                return false;
            }
        }

        public static bool UpdateJyInfoToTargetFusionWhenFuelingStarted(int jihao, DebugLogger debugLogger)
        {
            if (!supportMultiFusions)
            {
                return false;
            }

            try
            {
                string updateSql = string.Format(" update jy_info set [status]='{0}', qty=0, amount=0 " +
                                                " where jihao='{1}' ",
                                                'B', jihao);
                targetSqlCmd.UpdateData(updateSql);

                return true;
            }
            catch(Exception ex)
            {
                debugLogger.Add(ex);
                return false;
            }
        }

        public static bool CopyJyInfoToTargetFusion(int jihao, DebugLogger debugLogger)
        {
            if (!supportMultiFusions)
            {
                return false;
            }

            try
            {
                string querySql = string.Format(" select * from jy_info " +
                                    " where jihao={0} ",
                                    jihao);
                JyInfoModel jyInfoModel = sourceSqlCmd.ReadData<JyInfoModel>(querySql, DbModelConstructors.ConstructJyInfoModel);
                if (jyInfoModel != null)
                {
                    string updateSql = string.Format(" update jy_info set [status]='{0}', youpin=N'{1}', qty={2}, amount={3}, fzqty={4}, fzamount={5} " +
                                                " where jihao='{6}' ",
                                                jyInfoModel.status, jyInfoModel.youpin, jyInfoModel.qty, jyInfoModel.amount, jyInfoModel.fzqty, jyInfoModel.fzamount,
                                                jyInfoModel.jihao);
                    targetSqlCmd.UpdateData(updateSql);

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                debugLogger.Add(ex);
                return false;
            }
        }

        
    }

    
}