using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.Service;
using Cosmos.CommonManager;
using System.Timers;
//using System.Threading;
using SPC.Kiosk.Common;
using SPC.Kiosk.PB.Model;
namespace SPC.Kiosk.DataHelper
{
    public class BackDataService : IDisposable
    {
        #region [ Members ]
        private SManager sManager = null;                                   // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = null;                             // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = null;                              // 기본정보 참조
        private TranStatus m_cTrnStatus = null;                             // 거래정보 참조
        protected IDataCommonUs m_cDataCommon = null;
        private Timer objServiceTimer;
        private DateTime lastTime;

        private IDataProcessUs m_cDataService = null;                               // 거래데이터 합계금액 계산 및 관리
        private ICampaignUs m_cCampaingMain = null;                                 // 캠페인 관리
        private String m_sMstBatchDownDate = "";
        /// <summary>
        /// POS 초기화 필요 유무
        /// </summary>
        public bool NeedInitModule { get; set; }
        #endregion Members

        #region [ Ctor & Dispose ]
        /// <summary>
        /// Ctor
        /// </summary>
        public BackDataService()
        {
            sManager = new SManager();
            StateObject = (StateServer)StateServer.GetInstance();
            if (!NeedInitModule)
            {
                m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
                m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보 
                m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);

                m_cDataService = (IDataProcessUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_SERVICE);
                m_cCampaingMain = (ICampaignUs)sManager.InitServiceInstance(ServiceLists.ASV_CAMPAIGN.DLL, ServiceLists.ASV_CAMPAIGN.CAMPAIGN_MAIN);

                // 캠페인 마스터 로딩
                m_cDataService.InitTranHeader();
                m_sMstBatchDownDate = m_cPosStatus.Base.MstBatchDownDate;
                m_cCampaingMain.LoadCampaignCondition(new string[] { });

             
                objServiceTimer = new Timer(1000);
                lastTime = DateTime.Now;
                objServiceTimer.Elapsed += ObjServiceTimer_Elapsed;
                objServiceTimer.Start();
            }
        }


        private void ObjServiceTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            DateTime _dateTime = DateTime.Now;
            //if ((DateTime.Now - lastTime) >= (TimeSpan.FromMinutes(0.5d)))
            if ((_dateTime - lastTime) >= (TimeSpan.FromMinutes(0.5d)))
            {
                //TODO:5분마다 처리할 로직 추가
                lastTime = DateTime.Now;

                //string[] _list = m_cDataCommon.Select_NHS_CUST_BUY_HIST(string.Format("{0:yyyyMMddHHmmss}", DateTime.Now), @"1011175065");

                //  2019-03-21 - 1997fx11 : 아이템 변경 수신
                int iRet01 = m_cDataCommon.Get_NEW_PRODUCT_BY_IRT();

                //  2019-03-21 - 1997fx11 : 시스템정보 전송
                int iRet02 = m_cDataCommon.Send_SYSTEM_INFO();
            }

            //  2019-04-25 - 1997fx11 : 자동 마감/개점 ver 0.2
            //  PosStart/frmPosStart.cs/tmrStart_Tick 내에 메인포스 개점여부에 따라 진행. (추가예정?? 부분)
            string sCurrentTime = System.DateTime.Now.ToString("HHmmss");
            if (CmUtil.LongParse(sCurrentTime) == CmUtil.LongParse("050000"))
            {
                m_cDataCommon.SetSaleEndOpen();
            }
        }


        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            if (objServiceTimer != null)
            {
                objServiceTimer.Stop();
                objServiceTimer.Elapsed -= ObjServiceTimer_Elapsed;
                objServiceTimer.Dispose();
                objServiceTimer = null;
            }
            sManager = null;                                  // 이 객체를 통해 업무 Service 호출
            StateObject = null;                               // StateObject : StateServer Object (객체)
            m_cPosStatus = null;                              // 기본정보 참조
            m_cTrnStatus = null;                              // 거래정보 참조
            m_cDataCommon = null;
        }
        #endregion Ctor & Dispose

        #region [ Methods ]

        /// <summary>
        /// 옵션값 획득
        /// </summary>
        /// <param name="sOptCode"></param>
        /// <returns></returns>
        public string GetPosOption(string sOptCode)
        {
            return PosMstManager.GetPosOption(sOptCode);
        }

        #endregion Methods
    }
}
