using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net; 
using System.IO;

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

namespace Cosmos.Win
{
    public partial class frmWebBiz : Form
    {
        #region 변수 선언
        [DllImport("Ole32.dll", SetLastError = true)]
        private static extern void CoFreeUnusedLibraries();

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// 파라메터
        /// </summary>
        private string m_sParaArgs = "";
        
        
        #endregion 변수 선언

        #region 생성자 & 소멸자
        public frmWebBiz(string sArgs)
        {
            InitializeComponent();

            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            //this.UpdateStyles();

            // 메뉴키(0),메뉴명(1),URL(2),메뉴ID(3),파라메터(4),폰트(5),브랜드구분(6),브랜드코드(7),캐셔번호(8),브라우져키(9)
            m_sParaArgs = sArgs;
        }

        private void frmWebBiz_Load(object sender, EventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", m_sParaArgs);

            InitControl();  // 콘트롤 초기화 처리
            this.Tag = "START";
        }

        private void frmWebPos2_Activated(object sender, EventArgs e)
        {
            if (this.Tag.ToString() == "START")
            {
                this.Tag = "";

                // 메뉴키(0),메뉴명(1),URL(2),메뉴ID(3),파라메터(4),폰트(5),브랜드구분(6),브랜드코드(7),캐셔번호(8),브라우져키(9)
                string[] saParaArgs = m_sParaArgs.Split('|');
                
                lblTitle.Text = saParaArgs[1];
                this.Update();

                SetForegroundWindow(this.Handle);
                CoFreeUnusedLibraries();
                System.Threading.Thread.Sleep(500);

                LoadWebPos();
                SetForegroundWindow(this.Handle);
            }
        }

        private void frmWebBiz_FormClosing(object sender, FormClosingEventArgs e)
        {
            //UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
            //        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");
        }

        private void frmWebBiz_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (this.axNexacroAXCtrl != null)
                {
                    // v8.dll Unload
                    ((IDisposable)this.axNexacroAXCtrl).Dispose();
                }
                UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", lblTitle.Text);
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        private void InitControl()
        {
            this.Location = new Point(0, 0);
            // 이미지 로딩 처리
            picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_FULL_SIZE);

            FormManager.SetControlSize(this, 0, 0, 1024, 768);
            FormManager.SetControlSize(this.picBack, 0, 0, 1024, 35);
            FormManager.SetControlSize(this.lblTitle, 10, 2, 964, 33);
            FormManager.SetControlSize(this.btnExit, 976, 2, 46, 32);
            FormManager.SetControlSize(this.axNexacroAXCtrl, 0, 35, 1024, 768 - 35);

            btnExit.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BTN_CLOSE);
            if (btnExit.Image != null) btnExit.Text = "";

            // 메뉴키(0),메뉴명(1),URL(2),메뉴ID(3),파라메터(4),폰트(5),브랜드구분(6),브랜드코드(7),캐셔번호(8),브라우져키(9)
            string[] saParaArgs = m_sParaArgs.Split('|');

            FormManager.SetFormAllControlFont(this, saParaArgs[5]);

            axNexacroAXCtrl.Visible = false;
        }
        #endregion

        #region Control Event
        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                //this.Close();
                Application.Exit();
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }
        #endregion Control Event

        #region User Method
        /// <summary>
        /// WEB POS 화면 로딩
        /// </summary>
        public bool LoadWebPos()
        {
            try
            {
                //UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                //    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");

                // 메뉴키(0),메뉴명(1),URL(2),메뉴ID(3),파라메터(4),폰트(5),브랜드구분(6),브랜드코드(7),캐셔번호(8),브라우져키(9)
                string[] saParaArgs = m_sParaArgs.Split('|');

                // 브라우져 키 설정
                string sBrowerKey = saParaArgs[9];
                if (saParaArgs[6] == ItemConst.CORP_DIV.BR) sBrowerKey += saParaArgs[7];
                
                axNexacroAXCtrl.key = sBrowerKey;
                axNexacroAXCtrl.bjson = saParaArgs[2];      // "http://10.0.40.98:7003/spcwps/start.json";
                axNexacroAXCtrl.run();

                return true;
            }
            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 false;
            }
        }

        private void axNexacroAXCtrl_loadingglobalvariables(object sender, AxNexacroAXLib._INexacroAXCtrlEvents_loadingglobalvariablesEvent e)
        {
            try
            {
                UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");

                // 메뉴키(0),메뉴명(1),URL(2),메뉴ID(3),파라메터(4),폰트(5),브랜드구분(6),브랜드코드(7),캐셔번호(8),브라우져키(9)
                string[] saParaArgs = m_sParaArgs.Split('|');

                string sUserID = UserLog.UserAES.StaticKeyEncrypt(saParaArgs[8]);
                // ID/화면코드
                if (!axNexacroAXCtrl.setvariablevalue("gv_param", "null!@#$null!@#$null!@#$USER_ID=" + sUserID + "!@#$SCRN=" + saParaArgs[3] + saParaArgs[4]))
                {
                    string sgv = (axNexacroAXCtrl.getvariablevalue("gv_param") == null ? "null" : axNexacroAXCtrl.getvariablevalue("gv_param").ToString());

                    UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", sgv);
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        private void axNexacroAXCtrl_beforeexit(object sender, AxNexacroAXLib._INexacroAXCtrlEvents_beforeexitEvent e)
        {
            try
            {
                e.pbcloseflag = true;
                e.pbhandledflag = true;
            }
            catch { }
        }

        private void axNexacroAXCtrl_exit(object sender, EventArgs e)
        {
            try
            {
                this.axNexacroAXCtrl.quit();
            }
            catch { }
        }
        #endregion 

        private void axNexacroAXCtrl_usernotify(object sender, AxNexacroAXLib._INexacroAXCtrlEvents_usernotifyEvent e)
        { 
            try
            {
                axNexacroAXCtrl.Visible = true;
            }
            catch { }
        }        
    }
}
