using System;
using System.Collections;
using System.Text;

using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;
using System.Data;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 선불카드 판매 - 중국
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    class SalePreCard : IPaymentUs
    {
        protected SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        protected StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        protected PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        protected IDataCommonUs m_cDataCommon = null;                                 // POS 공통함수 인터페이스

        public SalePreCard()
        {
            m_cPosStatus = (PosStatus)StateObject.POS; 
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
        }

        #region SearchPayment 결제 조회
        /// <summary>
        /// 결제 조회
        /// </summary>
        /// <param name="aParam"></param>
        /// <param name="aRet"></param>
        /// <returns></returns>
        public string SearchPayment(string[] aParam, ref string[] aRet)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                string sInqFlag = aParam[0];                                        // 처리구분 (거래구분 0(0:전체, 1:카드번호), 1(0:승인, 1:취소))
                string sCardNo = aParam[1];                                         // 카드번호
                string sTranType = ItemConst.PRIPAIDCARD_SALE_TYPE.SELECT;          // 거래구분 (0:조회, 1:승인, 2:취소)
                bool bViewMode = (aParam[2] == "1" ? true : false);                 // 조회중 표시 여부

                sRet = ExecuteHostIrt(sTranType, sInqFlag, sCardNo, bViewMode, ref aRet);
                if (sRet != UserCom.RST_OK) return sRet;
            }
            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;
        }
        #endregion

        #region GetPayment 결제 정보 획득
        /// <summary>
        /// 결제 정보 획득
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetPayment(string[] aParam)
        {
            return null; // m_cPayItem;
        }
        #endregion

        #region SetPayment 결제 등록
        /// <summary>
        /// 결제 등록
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string SetPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            string[] aRet = null;
             
            try
            {
                string sInqFlag = aParam[0];                                        // 처리구분 (거래구분 0(0:전체, 1:카드번호), 1(0:승인, 1:취소))
                string sCardNo = aParam[1];                                         // 카드번호
                string sTranType = ItemConst.PRIPAIDCARD_SALE_TYPE.APPROVE;         // 거래구분 (0:조회, 1:승인, 2:취소)
                bool bViewMode = (aParam[2] == "1" ? true : false);                 // 조회중 표시 여부

                sRet = ExecuteHostIrt(sTranType, sInqFlag, sCardNo, bViewMode, ref aRet);
                if (sRet != UserCom.RST_OK) return sRet;
            }
            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;
        }
        #endregion

        #region 결제 취소
        /// <summary>
        /// 결제 취소
        /// </summary>
        /// <returns></returns>
        public string CancelPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            string[] aRet = null;

            try
            {
                string sInqFlag = aParam[0];                                        // 처리구분 (거래구분 0(0:전체, 1:카드번호), 1(0:승인, 1:취소))
                string sCardNo = aParam[1];                                         // 카드번호
                string sTranType = ItemConst.PRIPAIDCARD_SALE_TYPE.APPROVE_CAN;     // 거래구분 (0:조회, 1:승인, 2:취소)
                bool bViewMode = (aParam[2] == "1" ? true : false);                 // 조회중 표시 여부

                sRet = ExecuteHostIrt(sTranType, sInqFlag, sCardNo, bViewMode, ref aRet);
                if (sRet != UserCom.RST_OK) return sRet;
            }
            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;
        }
        #endregion

        #region 결제 반품
        /// <summary>
        /// 결제 반품
        /// </summary>
        /// <returns></returns>
        public string RefundPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            string[] aRet = null;

            try
            {
                string sInqFlag = aParam[0];                                        // 처리구분 (거래구분 0(0:전체, 1:카드번호), 1(0:승인, 1:취소))
                string sCardNo = aParam[1];                                         // 카드번호
                string sTranType = ItemConst.PRIPAIDCARD_SALE_TYPE.APPROVE_CAN;     // 거래구분 (0:조회, 1:승인, 2:취소)
                bool bViewMode = (aParam[2] == "1" ? true : false);                 // 조회중 표시 여부

                sRet = ExecuteHostIrt(sTranType, sInqFlag, sCardNo, bViewMode, ref aRet);
                if (sRet != UserCom.RST_OK) return sRet;

            }
            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;
        }
        #endregion
         
        #region SetMenualPayment
        public string SetMenualPayment(string[] aParam)
        {
            throw new NotImplementedException();
        }
        #endregion
          
        #region Host 조회 실행
        /// <summary>
        /// Host 조회 실행
        /// </summary>
        public string ExecuteHostIrt(string sTranType, string sInqFlag, string sCardNo, bool bViewMode, ref string[] aRet)
        {
            int iRecvTimeOut = 10000;
            string sRet = UserCom.RST_ERR;

            try
            {
                Hashtable htSendData = new Hashtable();
                Hashtable htRecvData = new Hashtable();

                if (sTranType == ItemConst.PRIPAIDCARD_SALE_TYPE.SELECT)
                {
                    // 목록 조회
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.INQ_TYPE, ItemConst.IRT_INQ_TYPE.PREPAIDCARD_SEL_INQ);
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.STOR_CD, m_cPosStatus.Base.StoreNo);
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.INQ_FLAG, sInqFlag);
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.INQ_GIFT_NO, sCardNo);
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.VAULES, "");
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.RES_CD, "");
                    htSendData.Add(Column.POS_SEL_PREPAIDCARD.DATA.RES_MSG, "");

                    sRet = m_cDataCommon.ExecutePosIrt(ItemConst.COMM_MSG_TYPE.POSIRT, m_cPosStatus.Base.CommSvrIp, (int)m_cPosStatus.Base.PayInqPort, iRecvTimeOut, htSendData, ref htRecvData, bViewMode);
                    if (sRet != UserCom.RST_OK)
                    {
                        WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0005));

                        sRet = UserCom.RST_ERR;
                        return sRet;
                    }

                    if (htRecvData[Column.POS_SEL_PREPAIDCARD.DATA.RES_CD].ToString() != "00")
                    {
                        //WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0294) + "\n (" + htRecvData[Column.POS_SEL_PREPAIDCARD.DATA.RES_CD].ToString() + ")" + "\n\r" + htRecvData[Column.POS_SEL_PREPAIDCARD.DATA.RES_MSG]);
                        if (sInqFlag == "1")
                            WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0603));
                        sRet = UserCom.RST_ERR;
                        return sRet;
                    }
                    else
                    {
                        // 데이터
                        aRet = htRecvData[Column.POS_SEL_PREPAIDCARD.DATA.VAULES].ToString().Split(new string[] { "#~" }, StringSplitOptions.None);
                    }
                }
                else
                {
                    // 승인/취소
                    htSendData.Add(Column.POS_APPR_PREPAIDCARD.DATA.INQ_TYPE, ItemConst.IRT_INQ_TYPE.PREPAIDCARD_APPR_INQ);
                    htSendData.Add(Column.POS_APPR_PREPAIDCARD.DATA.STOR_CD, m_cPosStatus.Base.StoreNo);
                    htSendData.Add(Column.POS_APPR_PREPAIDCARD.DATA.INQ_FLAG, sInqFlag);
                    htSendData.Add(Column.POS_APPR_PREPAIDCARD.DATA.INQ_GIFT_NO, sCardNo);
                    htSendData.Add(Column.POS_APPR_PREPAIDCARD.DATA.RES_CD, "");
                    htSendData.Add(Column.POS_APPR_PREPAIDCARD.DATA.RES_MSG, "");

                    sRet = m_cDataCommon.ExecutePosIrt(ItemConst.COMM_MSG_TYPE.POSIRT, m_cPosStatus.Base.CommSvrIp, (int)m_cPosStatus.Base.PayInqPort, iRecvTimeOut, htSendData, ref htRecvData, bViewMode);
                    if (sRet != UserCom.RST_OK)
                    {
                        WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0005));

                        sRet = UserCom.RST_ERR;
                        return sRet;
                    }

                    if (htRecvData[Column.POS_APPR_PREPAIDCARD.DATA.RES_CD].ToString() != "00" && bViewMode == true)
                    {
                        if (sInqFlag == "0")
                        {
                            // 승인요청 실패
                            WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0598) + "\n (" + htRecvData[Column.POS_APPR_PREPAIDCARD.DATA.RES_CD].ToString() + ")" + "\n\r" + htRecvData[Column.POS_APPR_PREPAIDCARD.DATA.RES_MSG]);
                        }
                        else
                        {
                            // 취소요청 실패
                            WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0600) + "\n (" + htRecvData[Column.POS_APPR_PREPAIDCARD.DATA.RES_CD].ToString() + ")" + "\n\r" + htRecvData[Column.POS_APPR_PREPAIDCARD.DATA.RES_MSG]);
                        }

                        sRet = UserCom.RST_ERR;
                        return sRet;
                    }
                }
                sRet = UserCom.RST_OK;
                return sRet;
            }
            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;
        }
        #endregion
    }
}
