using System;
using System.Data;
using System.Collections;

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

namespace Cosmos.Service
{
    class WebPos : IDataServiceUs
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        
        private IMasterUs m_cMstService = null;                                     // 마스터 인터페이스

        private string STATIC_KEY_FOR_BIZEUN_WEB = "dkaghghkdlstnspc";              // 빚은WEB 암호화 고정키

        public WebPos()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cMstService = (IMasterUs)sManager.InitServiceInstance(ServiceLists.ASV_MASTER.DLL, ServiceLists.ASV_MASTER.POS_MASTER);
        }

        /// <summary>
        /// 웹 업무 URL 가져오기
        /// </summary>
        /// <param name="aParam[0]">FUNC_ID</param>
        /// <param name="aParam[1]">점포코드 암호화 여부</param>
        /// <returns></returns>
        public object GetData(string[] aParam)
        {
            string sQuery = "";
            string sRet = "";
            bool bIsStorCdEncoded = false;

            try
            {
                sQuery += " SELECT * FROM POSMST..MST_WEB_LNKAGE ";
                sQuery += " WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "   AND USE_YN  = '" + PosConst.MST_USE_YN.YES + "'";
                sQuery += "   AND FUNC_ID = '" + aParam[0] + "'";

                DataTable dtDataInfo = m_cMstService.Select(new string[] { sQuery });

                if(aParam.Length > 1)
                {
                    bIsStorCdEncoded = aParam[1] == "1" ? true : false;
                }

                if (dtDataInfo != null && dtDataInfo.Rows.Count > 0)
                {
                    DataRow dr = dtDataInfo.Rows[0];

                    string sDiv = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.DIV);            // 1: WEB POS, 2:WEB LINK
                    string sName = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.MENU_NM).Replace("\n", " ").Replace("\\n", " "); 
                    string sUrl = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.WEB_MENU_ID);
                    string sParam = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.PARAM);
                    string sCaption = CmUtil.GetDataRowStr(dr, PosMst.MST_WEB_LINKAGE.DATA.FILLER1);

                    if (sParam != "") sParam = ParamReplace(sParam, bIsStorCdEncoded);

                    // 링크구분(1) + 메뉴명(30) + 메뉴ID(100) + 파라미터(100) + 캡션(50)
                    sRet = CmUtil.RPadH(sDiv, 1) + CmUtil.RPadH(sName, 30) + CmUtil.RPadH(sUrl, 100) + CmUtil.RPadH(sParam, 100) + CmUtil.RPadH(sCaption, 50);
                }
                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;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sParamInfo"></param>
        /// <returns></returns>
        private string ParamReplace(string sParamInfo, bool bIsStorCdEncoded)
        {
            string sRetParam = sParamInfo;
            try
            {
                if (sParamInfo != "")
                {
                    string sStoreNo = "";
                    if (bIsStorCdEncoded == true)
                    {
                        // 빚은몰 URL로 GET방식으로 전달 시 아래 문자들에 대하여 치환하여 전송 함
                        // "&" -> "%26", "+" -> "%2B"
                        string sEncodedStoreNo = UserLog.UserAES.DynamicKeyEncrypt(STATIC_KEY_FOR_BIZEUN_WEB, m_cPosStatus.Base.StoreNo);
                        sEncodedStoreNo = sEncodedStoreNo.Replace("&", "%26");
                        sEncodedStoreNo = sEncodedStoreNo.Replace("+", "%2B");
                        sStoreNo = sEncodedStoreNo;
                    }
                    else
                    {
                        sStoreNo = m_cPosStatus.Base.StoreNo;
                    }
                    sRetParam = sRetParam.Replace("<STOR_CD>", sStoreNo);
                    sRetParam = sRetParam.Replace("<USER_ID>", m_cPosStatus.Base.CashierNo);
                    sRetParam = sRetParam.Replace("<USER_PWD>", m_cPosStatus.Base.CashierWebPosPwd);
                    sRetParam = sRetParam.Replace("<BRAND_CD>", m_cPosStatus.Base.BrandCd);
                    sRetParam = sRetParam.Replace("<SALE_DT>", m_cPosStatus.Base.SaleDate);
                    sRetParam = sRetParam.Replace("<COMP_CD>", m_cPosStatus.Base.CmpCd);
                }

                return sRetParam;
            }
            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 sRetParam;
        }

        public string Execute(string[] aParam)
        {
            throw new NotImplementedException();
        }

        public string SetData(object oParam)
        {
            throw new NotImplementedException();
        }
    }
}
