using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using INT_PT002.Part11_UserManager;
using INT_PT002.DataStore;
using INT_PT002.DialogForms;

namespace INT_PT002.Forms
{
    public partial class FormMainDisplay : Form
    {
        #region Field
        private FormMain m_ParentForm; 
        #endregion

        #region Constructor
        public FormMainDisplay(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
        } 
        #endregion

        #region Property
        public FormMain ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        } 
        #endregion

        #region Method
        private bool UI_Invoke(ThreadStart invoker)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    if (this.IsDisposed)
                        return true;

                    this.Invoke(invoker);
                }
                else
                {
                    invoker();
                }

                return true;

            }
            catch (Exception e)
            {
                return false;
            }

        }

        private void Login()
        {
            DialogFormLogOn logOn = new DialogFormLogOn(this.ParentForm);
            if (logOn.ShowDialog() == DialogResult.OK)
            {
                this.DisplayRefresh();
                this.buttonUser.ButtonDown();
                //this.ParentForm.TimerAutomaticLogout(true, this.ParentForm.SystemConfig.AutomaticLogout);

                // Part 11
                this.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Login, "");
            }
            else
            {
                this.buttonUser.ButtonUp();
            }
        }
        public void Logout()
        {
            this.ParentForm.CurrentSystemStatus.CurrentUser.Status = Define.E_UserStatus.LogOff;
            this.buttonUser.ButtonUp();
            this.DisplayRefresh();

            // Part 11
            this.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Logout, "");
        }
        public void Part11AutomaticLogoutReset()
        {
            UserManager.UserManager_AutoLogoutTimeoutReset();
        }

        public void CallBackUserListLoginTimeoutDataEvent(UserManager.UserMgr_login_timeout_t user)
        {
            string code = "", message1 = "", message2 = "";
            int warningTime = 0;

            if (user.remainder_time != 0)
            {
                switch (this.ParentForm.SystemConfig.LANGUAGE)
                {
                    case Define.E_LanguageID.Korean:
                        code = "로그인";
                        message1 = "잠시 후에 로그아웃 됩니다";
                        message2 = "";
                        break;
                    case Define.E_LanguageID.English:
                        code = "Login";
                        message1 = "You will be logged out after a while";
                        message2 = "";
                        break;
                    default:
                        break;
                }

                warningTime = this.ParentForm.FlagAutomaticLogoutWarningTime - 2;

                this.UI_Invoke(delegate
                {
                    DialogFormMessage msg = new DialogFormMessage(Define.E_MessageBoxIcon.Asterisk, code, message1, message2, warningTime);
                    msg.ShowDialog();
                });
            }
            else
            {
                switch (this.ParentForm.SystemConfig.LANGUAGE)
                {
                    case Define.E_LanguageID.Korean:
                        code = "Login";
                        message1 = "자동 로그아웃 되었습니다";
                        message2 = "";
                        break;
                    case Define.E_LanguageID.English:
                        code = "Login";
                        message1 = "automatically logged out";
                        message2 = "";
                        break;
                    default:
                        break;
                }

                // User Logout
                UserManager.UserManager_UserLogout();

                this.UI_Invoke(delegate
                {
                    this.Logout();
                });

                this.UI_Invoke(delegate
                {
                    DialogFormMessage msg = new DialogFormMessage(Define.E_MessageBoxIcon.Asterisk, code, message1, message2, 0);
                    msg.ShowDialog();
                });
            }
        }

        public void DisplayRefresh()
        {

        }
        #endregion

        #region Event Handler
        private void smartButton1_Click(object sender, EventArgs e)
        {
            this.ParentForm.smartForm1.Show(2);
        }

        private void smartButton2_Click(object sender, EventArgs e)
        {
            this.ParentForm.smartForm1.Show(1);
        }
        #endregion
    }
}