using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Data;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;

namespace Cosmos.Win
{
    public class WinOpenClose
    {
        /// <summary>
        /// FORM 화면 표시
        /// </summary>
        /// <param name="aPara"></param>
        /// <returns></returns>
        public static string ShowForm(string[] aPara)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                if (aPara[0] == FormManager.FORM_SOD_SALE)
                {
                    // 개점 화면
                    frmSOD fForm = (frmSOD)FormManager.GetForm(FormManager.FORM_SOD_SALE);
                    if (fForm == null)
                    {
                        fForm = new frmSOD();
                        FormManager.AddForm(FormManager.FORM_SOD_SALE, fForm);
                    }
                    DialogResult dReturn = fForm.ShowDialog();

                    if (dReturn == DialogResult.OK)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    else if (dReturn == DialogResult.Ignore)
                    {
                        sRet = UserCom.RST_IGNORE;
                    }
                    else
                    {
                        sRet = UserCom.RST_ERR;
                    }
                }
                else if (aPara[0] == FormManager.FORM_ACC_REMONEY)
                {
                    frmAccRemoney fForm = (frmAccRemoney)FormManager.GetForm(FormManager.FORM_ACC_REMONEY);
                    if (fForm == null)
                    {
                        fForm = new frmAccRemoney();
                        FormManager.AddForm(FormManager.FORM_ACC_REMONEY, fForm);
                    }

                    if (aPara.Length <= 1)
                    {
                        fForm.SearchInfo = "";
                        fForm.SetAutoClose = "";
                    }
                    else
                    {
                        // 준비금 입금내역 조회일자 전달
                        fForm.SearchInfo = aPara[1];
                        fForm.SetAutoClose = aPara[2];
                    }
                    DialogResult dReturn = fForm.ShowDialog();
                    sRet = (dReturn == DialogResult.OK ? UserCom.RST_OK : UserCom.RST_ERR);
                }
                else if (aPara[0] == FormManager.FORM_ACC_ETC_INOUT)
                {
                    frmAccEtcInOut fForm = (frmAccEtcInOut)FormManager.GetForm(FormManager.FORM_ACC_ETC_INOUT);
                    if (fForm == null)
                    {
                        fForm = new frmAccEtcInOut();
                        FormManager.AddForm(FormManager.FORM_ACC_ETC_INOUT, fForm);
                    }
                    DialogResult dReturn = fForm.ShowDialog();
                    sRet = (dReturn == DialogResult.OK ? UserCom.RST_OK : UserCom.RST_ERR);
                }
                else if (aPara[0] == FormManager.FORM_SCH_VENDOR)
                {
                    // 거래처 조회 화면
                    frmShopPayType fForm = (frmShopPayType)FormManager.GetForm(FormManager.FORM_SCH_VENDOR);
                    if (fForm == null)
                    {
                        fForm = new frmShopPayType();
                        FormManager.AddForm(FormManager.FORM_SCH_VENDOR, fForm);
                    }

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_ACC_SALE_END)
                {
                    sRet = UserCom.RST_ERR;
                    sRet = EndStoreCheck(PosConst.POS_CHECK_ALARM_TYPE.CHECK_TYPE_EOD, aPara[1]);
                     
                    frmAccADMT fForm = (frmAccADMT)FormManager.GetForm(FormManager.FORM_ACC_SALE_END);
                    if (fForm == null)
                    {
                        fForm = new frmAccADMT();
                        FormManager.AddForm(FormManager.FORM_ACC_SALE_END, fForm);
                    }

                    fForm.SetPosMenuKey  = aPara[1];
                    fForm.SetTranKind = aPara[2];
                    fForm.SetTradeDiv = aPara[3];

                    //if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                    var ret = fForm.ShowDialog();
                    if (ret == DialogResult.OK)
                    {
                        sRet = UserCom.RST_OK;
                    }
                    else if (ret == DialogResult.Ignore)
                    {
                        sRet = UserCom.RST_IGNORE;                    
                    }
                }
                else if (aPara[0] == FormManager.FORM_ACC_SALE_END_CAN)
                {
                    frmAccADMTCan fForm = (frmAccADMTCan)FormManager.GetForm(FormManager.FORM_ACC_SALE_END_CAN);
                    if (fForm == null)
                    {
                        fForm = new frmAccADMTCan();
                        FormManager.AddForm(FormManager.FORM_ACC_SALE_END_CAN, fForm);
                    }

                    fForm.SetPosMenuKey = aPara[1];
                    fForm.SetTranKind = aPara[2];
                    fForm.SetTradeDiv = aPara[3];

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_SCH_PREP_STOR_NOTICE)
                {
                    frmPrepStorNotice fForm = (frmPrepStorNotice)FormManager.GetForm(FormManager.FORM_SCH_PREP_STOR_NOTICE);
                    if (fForm == null)
                    {
                        fForm = new frmPrepStorNotice();
                        FormManager.AddForm(FormManager.FORM_SCH_PREP_STOR_NOTICE, fForm);
                    }

                    fForm.CheckCode = aPara[2];
                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_SOD_CHECK)
                {
                    frmSaleCheckList fForm = (frmSaleCheckList)FormManager.GetForm(FormManager.FORM_SOD_CHECK);
                    if (fForm == null)
                    {
                        fForm = new frmSaleCheckList();
                        FormManager.AddForm(FormManager.FORM_SOD_CHECK, fForm);
                    }

                    fForm.SearchInfo = aPara[2];
                    fForm.DateInfo = aPara[3];
                    fForm.CheckCode = aPara[4];
                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_FOOD_SAFETY_DAY_POPUP)
                {
                    frmSafetyDay fForm = (frmSafetyDay)FormManager.GetForm(FormManager.FORM_FOOD_SAFETY_DAY_POPUP);
                    if (fForm == null)
                    {
                        fForm = new frmSafetyDay();
                        FormManager.AddForm(FormManager.FORM_FOOD_SAFETY_DAY_POPUP, fForm);
                    }

                    fForm.SearchInfo = aPara[2];
                    fForm.DateInfo = aPara[3];
                    fForm.CheckCode = aPara[4];

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_FOOD_SAFETY_DAY)
                {
                    frmSafetyCheckList fForm = (frmSafetyCheckList)FormManager.GetForm(FormManager.FORM_FOOD_SAFETY_DAY);
                    if (fForm == null)
                    {
                        fForm = new frmSafetyCheckList();
                        FormManager.AddForm(FormManager.FORM_FOOD_SAFETY_DAY, fForm);
                    }

                    fForm.SearchInfo = aPara[2];
                    fForm.DateInfo = aPara[3];
                    fForm.CheckCode = aPara[4];

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_EXPIRATION_DATE_CHECK)
                {
                    frmExpiryDateCheckList fForm = (frmExpiryDateCheckList)FormManager.GetForm(FormManager.FORM_EXPIRATION_DATE_CHECK);
                    if (fForm == null)
                    {
                        fForm = new frmExpiryDateCheckList();
                        FormManager.AddForm(FormManager.FORM_EXPIRATION_DATE_CHECK, fForm);
                    }

                    fForm.SearchInfo = aPara[2];
                    fForm.DateInfo = aPara[3];
                    fForm.CheckCode = aPara[4];

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
                else if (aPara[0] == FormManager.FORM_STORE_EQUIP_CHECK_LIST)
                {
                    frmStoreEquipCheckList fForm = (frmStoreEquipCheckList)FormManager.GetForm(FormManager.FORM_STORE_EQUIP_CHECK_LIST);
                    if (fForm == null)
                    {
                        fForm = new frmStoreEquipCheckList();
                        FormManager.AddForm(FormManager.FORM_STORE_EQUIP_CHECK_LIST, fForm);
                    }

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }

                else if (aPara[0] == FormManager.FORM_STORE_EQUIP_CHECK)
                {
                    frmStoreEquipCheck fForm = (frmStoreEquipCheck)FormManager.GetForm(FormManager.FORM_STORE_EQUIP_CHECK);
                    if (fForm == null)
                    {
                        fForm = new frmStoreEquipCheck();
                        FormManager.AddForm(FormManager.FORM_STORE_EQUIP_CHECK, fForm);
                    }

                    if (fForm.ShowDialog() == DialogResult.OK) sRet = UserCom.RST_OK;
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }

            return sRet;
        }

        /// <summary>
        /// 마감시 준비점검체크 / 공지사항 확인
        /// </summary>
        /// <returns></returns>
        public static string EndStoreCheck(string sAlarmType, string sType)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                IStoreCheck m_cStoreCheck = null;                                // 점포점검 서비스
                SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
                PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
                StateServer StateObject = (StateServer)StateServer.GetInstance();

                m_cPosStatus = (PosStatus)StateObject.POS;              //POS 기본정보
                m_cStoreCheck = (IStoreCheck)sManager.InitServiceInstance(ServiceLists.BSV_OPEN_CLOSE.DLL, ServiceLists.BSV_OPEN_CLOSE.STORE_CHECK);

                DataTable dtCheckInfo = new DataTable();
                string sCheckCode = string.Empty;

                // 웹 업무 화면 호출(준비점검, 공지사항, 판매준비점검)
                if (sAlarmType == PosConst.POS_CHECK_ALARM_TYPE.CHECK_TYPE_NONE)
                {
                    dtCheckInfo = new DataTable();

                    // 1 : 로그인, 2 : 개점, 3 : 마감
                    // 개점 요청 일자로 스케줄을 체크한다.
                    dtCheckInfo = (DataTable)m_cStoreCheck.StoreCheckSchedule(sAlarmType, sType);

                    //if (dtCheckInfo.Rows.Count > 0)
                    //{
                        // 공지사항 호출
                        sRet = WinOpenClose.ShowForm(new string[] { FormManager.FORM_SCH_PREP_STOR_NOTICE, PosKey.MENU_KEY.PREP_STOR_NOTICE, sCheckCode });
                        if (sRet != UserCom.RST_OK)
                        {

                        }
                    //}
                }

                if (sAlarmType != PosConst.POS_CHECK_ALARM_TYPE.CHECK_TYPE_NONE)
                {

                    dtCheckInfo = new DataTable();
                    // 1 : 로그인, 2 : 개점, 3 : 마감
                    // 개점 요청 일자로 스케줄을 체크한다.
                    dtCheckInfo = (DataTable)m_cStoreCheck.StoreCheckSchedule(sAlarmType, sType);

                    if (dtCheckInfo.Rows.Count > 0)
                    {
                        for (int nLoop = 0; nLoop < dtCheckInfo.Rows.Count; nLoop++)
                        {
                            sCheckCode = CmUtil.GetDataRowStr(dtCheckInfo.Rows[nLoop], "CHECK_CD");

                            if (sCheckCode != "" && sCheckCode != null)
                            {
                                sRet = m_cStoreCheck.StoreCheckSaveIrt(sCheckCode, m_cPosStatus.Base.SaleDate);
                                if (sRet == UserCom.RST_OK)
                                {
                                    //continue;
                                }
                                else
                                {
                                    if (sType != PosKey.MENU_KEY.PREP_STOR_NOTICE)
                                    {
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                continue;
                                //return sRet;
                            }


                            if (sCheckCode == "12")
                            {
                                // 유통기한 점검 체크리스트
                                sRet = WinOpenClose.ShowForm(new string[] { FormManager.FORM_EXPIRATION_DATE_CHECK, PosKey.MENU_KEY.EXPIRATION_DATE_CHECK_LIST, PosConst.STORE_CHECK_FLAG.STORE_OPEN_CHECK, m_cPosStatus.Base.SaleDate, sCheckCode });
                                if (sRet != UserCom.RST_OK)
                                {

                                }
                            }
                            else if (sCheckCode == "11")
                            {
                                // 식품안전의 날
                                sRet = WinOpenClose.ShowForm(new string[] { FormManager.FORM_FOOD_SAFETY_DAY_POPUP, PosKey.MENU_KEY.FOOD_SAFETY_DAY_POPUP, PosConst.STORE_CHECK_FLAG.STORE_OPEN_CHECK, m_cPosStatus.Base.SaleDate, sCheckCode });
                                if (sRet != UserCom.RST_OK)
                                {

                                }
                            }
                            else
                            {
                                if (sAlarmType == PosConst.POS_CHECK_ALARM_TYPE.CHECK_TYPE_SOD)
                                {
                                    // 위에 설정된 점검코드 외 점검내용은 기본 점검화면을 사용한다.
                                    sRet = WinOpenClose.ShowForm(new string[] { FormManager.FORM_SOD_CHECK, PosKey.MENU_KEY.STORE_CHECK_LIST, PosConst.STORE_CHECK_FLAG.STORE_OPEN_CHECK, m_cPosStatus.Base.SaleDate, sCheckCode });
                                    if (sRet != UserCom.RST_OK)
                                    {

                                    }
                                }
                                else if (sAlarmType == PosConst.POS_CHECK_ALARM_TYPE.CHECK_TYPE_EOD)
                                {
                                    //판매준비점검(개점/마감)
                                    sRet = WinOpenClose.ShowForm(new string[] { FormManager.FORM_SOD_CHECK, PosKey.MENU_KEY.STORE_CHECK_LIST, PosConst.STORE_CHECK_FLAG.STORE_CLOSE_CHECK, m_cPosStatus.Base.SaleDate, sCheckCode });
                                    if (sRet != UserCom.RST_OK)
                                    {
                                        //return sRet;
                                    }
                                }
                                else if (sAlarmType == PosConst.POS_CHECK_ALARM_TYPE.CHECK_TYPE_LOGIN)
                                {
                                    //판매준비점검(개점/마감)
                                    sRet = WinOpenClose.ShowForm(new string[] { FormManager.FORM_SOD_CHECK, PosKey.MENU_KEY.STORE_CHECK_LIST, PosConst.STORE_CHECK_FLAG.STORE_LOGIN_CHECK, m_cPosStatus.Base.SaleDate, sCheckCode });
                                    if (sRet != UserCom.RST_OK)
                                    {
                                        //return sRet;
                                    }
                                }

                            }
                            
                        }
                    }
                }
                sRet = UserCom.RST_OK;

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
    }
}
