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

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 점착정보 조회
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    class SchStorArrvInfo : IStorArrvList
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보 참조
        private IMasterUs m_cMstService = null;                                     // 마스터 인터페이스

        private INetworkHttp m_cNetworkHttp = null;
        private IPosPrinterUs m_cPrinterUs = null;

        private IDataServiceUs m_cWebBiz = null;                                    // WEB 정보 서비스

        /// <summary>
        /// 생성자
        /// </summary>
        public SchStorArrvInfo()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보

            m_cNetworkHttp = (INetworkHttp)sManager.InitServiceInstance(ServiceLists.AGENT_NETWORK.DLL, ServiceLists.AGENT_NETWORK.NETWORK_HTTP);
            m_cPrinterUs = (IPosPrinterUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_POSPRINTER);
            m_cMstService = (IMasterUs)sManager.InitServiceInstance(ServiceLists.ASV_MASTER.DLL, ServiceLists.ASV_MASTER.POS_MASTER);

            m_cWebBiz = (IDataServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_BASIC.DLL, ServiceLists.BSV_BASIC.WEB_POS);
        }

        /// <summary>
        /// 점착정보 Http 통신
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string SendReceiveForStorArrInfo(object[] aParam, ref DataTable htResponse, ref int iReturn)
        {
            string sUrl;
            string sWorkType;
            Hashtable htRequest;
            Hashtable[] htReturn = null;
            int iRet = BaseCom.NG;
            string sArrInfo = string.Empty;
            string sArrInfoURL = string.Empty;
            string sArrInfoParm = string.Empty;

            string[] sCol = null;

            iReturn = -99;

            try
            {
                sArrInfo = (string)m_cWebBiz.GetData(new string[] { PosKey.MENU_KEY.WEB_STOR_ARRV_INFO });

                if (sArrInfo != null && sArrInfo != "")
                {
                    sArrInfoURL = CmUtil.MidH(sArrInfo, 31, 100).Trim();
                    sArrInfoParm = CmUtil.MidH(sArrInfo, 131, 100).Trim();
                    if (sArrInfoParm.StartsWith("?") == true)
                    {
                        sArrInfoParm = CmUtil.MidH(sArrInfo, 132, 99).Trim();
                    }
                }
                string sResp = string.Empty;
                //if ((iRet = m_cNetworkHttp.HttpGET_SendReceive("http://upos.paris.co.kr", "/EXEC_WPOS/ORDR/ORDR2000L0.jsp", "gubun=2&store_cd=0000013666", PosConst.ENCODING_FLAG.KSC, ref sResp)) == BaseCom.OK)
                if ((iRet = m_cNetworkHttp.HttpGET_SendReceive(sArrInfoURL, "", sArrInfoParm, PosConst.ENCODING_FLAG.KSC, ref sResp)) == BaseCom.OK)
                {
                    //htReturn = JsonConvert.DeserializeObject<Hashtable[]>(sResp);
                    sCol = sResp.Split(new string[] { "</body>" }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < sCol.Count() - 1 ; i++)
                    {

                        DataRow dr = htResponse.NewRow();
                        dr["cnt"] = i + 1;

                        int nStartIndex = sCol[i].IndexOf("<d_day>") + 7;
                        int nEndIndex = sCol[i].IndexOf("</d_day>");
                        dr["d_day"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<cust_name>") + "<cust_name>".Length;
                        nEndIndex = sCol[i].IndexOf("</cust_name>");
                        dr["cust_name"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<car_code>") + "<car_code>".Length;
                        nEndIndex = sCol[i].IndexOf("</car_code>");
                        dr["car_code"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<car_number>") + "<car_number>".Length;
                        nEndIndex = sCol[i].IndexOf("</car_number>");
                        dr["car_number"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<driver_name>") + "<driver_name>".Length;
                        nEndIndex = sCol[i].IndexOf("</driver_name>");
                        dr["driver_name"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<hand_phone>") + "<hand_phone>".Length;
                        nEndIndex = sCol[i].IndexOf("</hand_phone>");
                        dr["hand_phone"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<center_name>") + "<center_name>".Length;
                        nEndIndex = sCol[i].IndexOf("</center_name>");
                        dr["center_name"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<center_departure_time>") + "<center_departure_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</center_departure_time>");
                        dr["center_departure_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<center_arrival_time>") + "<center_arrival_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</center_arrival_time>");
                        dr["center_arrival_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<course_name>") + "<course_name>".Length;
                        nEndIndex = sCol[i].IndexOf("</course_name>");
                        dr["course_name"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<course_type>") + "<course_type>".Length;
                        nEndIndex = sCol[i].IndexOf("</course_type>");
                        dr["course_type"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<last_location>") + "<last_location>".Length;
                        nEndIndex = sCol[i].IndexOf("</last_location>");
                        dr["last_location"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<last_location_time>") + "<last_location_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</last_location_time>");
                        dr["last_location_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<cust_pre_arrival_time>") + "<cust_pre_arrival_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</cust_pre_arrival_time>");
                        dr["cust_pre_arrival_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<cust_arrival_time>") + "<cust_arrival_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</cust_arrival_time>");
                        dr["cust_arrival_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<cust_departure_time>") + "<cust_departure_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</cust_departure_time>");
                        dr["cust_departure_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<last_via_cust_name>") + "<last_via_cust_name>".Length;
                        nEndIndex = sCol[i].IndexOf("</last_via_cust_name>");
                        dr["last_via_cust_name"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<last_via_cust_arrival_time>") + "<last_via_cust_arrival_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</last_via_cust_arrival_time>");
                        dr["last_via_cust_arrival_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<last_via_cust_departure_time>") + "<last_via_cust_departure_time>".Length;
                        nEndIndex = sCol[i].IndexOf("</last_via_cust_departure_time>");
                        dr["last_via_cust_departure_time"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<current_car_status>") + "<current_car_status>".Length;
                        nEndIndex = sCol[i].IndexOf("</current_car_status>");
                        dr["current_car_status"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        nStartIndex = sCol[i].IndexOf("<current_cust_status>") + "<current_cust_status>".Length;
                        nEndIndex = sCol[i].IndexOf("</current_cust_status>");
                        dr["current_cust_status"] = sCol[i].Substring(nStartIndex, nEndIndex - nStartIndex);

                        htResponse.Rows.Add(dr);
                        
                    }
                    //htResponse.Rows.Add(dr);

                }

                iReturn = iRet;
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }

            return "";
        }

        /// <summary>
        /// 웹 업무 URL 가져오기
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetData(string[] aParam)
        {
            string sQuery = "";
            try
            {
                sQuery += " SELECT CMP_CD";
                sQuery += "      , STOR_CD";
                sQuery += "      , SEQ_NO";
                sQuery += "      , DIV";
                sQuery += "      , DISP_SEQ";
                sQuery += "      , DISP_YN";
                sQuery += "      , MENU_NM";
                sQuery += "      , WEB_MENU_ID";
                sQuery += "      , FUNC_ID";
                sQuery += "      , PARAM";
                sQuery += "      , FILLER1";
                sQuery += "      , FILLER2";
                sQuery += "      , FILLER3";
                sQuery += "      , FILLER4";
                sQuery += "      , FILLER5";
                sQuery += " FROM POSMST..MST_WEB_LNKAGE ";
                sQuery += " WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "   AND FUNC_ID = '" + aParam[0] + "'";

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

                return dtDataInfo;
            }
            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 null;
        }

    }
}
