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

using INT69DB_2A_ImageDll;
using INT69DB_2A.DialogForms;
using INT69DB_2A.Forms;
using INT69DB_2A.Part11_UserManager;

namespace INT69DB_2A.Controls
{
    public partial class ControlUserSetting : UserControl
    {
        #region Field
        private FormUserEditor m_ParentForm;
        #endregion

        #region Constructor
        public ControlUserSetting(FormUserEditor parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.InitializeDesign();
            this.DefaultSetting();
        }
        #endregion

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

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitlePassword.Text = "パスワㅡド";

                this.labelTitleExpireAccount.Text = "有効期限(ID)";
                this.labelTitleExpiryPassword.Text = "有効期限(パスワード)";
                this.labelStatusExpiryAccount.Text = "有効期限";
                this.labelStatusExpiryPassword.Text = "有効期限";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {

            }
            else
            {

            }
        }
        private void DefaultSetting()
        {
        }
        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 UpdateSelectUserDisplay(UserItem item)
        {
            // 화면 Enable 처리 추가할것
            this.labelID.Enabled = false;
            this.labelID.BackColor = Color.LightGray;

            this.labelID.Text = item.ID;
            this.labelPassword.Text = item.Password;
            this.labelExpireAccountDate.Text = string.Format("{0:yyyy/MM/dd} ~ {1:yyyy/MM/dd}", item.DateIdLogin, item.DateIdExpire);
            this.labelExpirePasswordDate.Text = string.Format("{0:yyyy/MM/dd} ~ {1:yyyy/MM/dd}", item.DatePasswordRegister, item.DatePasswordExpire);
            this.labelStatusExpiryAccount.Visible = item.IsLockAccount;
            this.labelStatusExpiryPassword.Visible = item.IsLockPassword;
        }

        public void CallBackUserListModifyInfoDataEvent(UserManager.UserMgr_user_info_t user)
        {
            string code = "", message1 = "", message2 = "";

            if (user.status == 0)
            {
                switch (this.ParentForm.ParentForm.SystemConfig.Language)
                {
                    case DataStore.LanguageID.Korean:
                        code = "유저설정";
                        message1 = "비밀번호 변경 완료!";
                        message2 = "";
                        break;
                    case DataStore.LanguageID.English:
                        code = "user settings";
                        message1 = "Password change complete!";
                        message2 = "";
                        break;
                    case DataStore.LanguageID.Chinese:
                        code = "用户设置";
                        message1 = "密码修改完成!";
                        message2 = "";
                        break;
                    case DataStore.LanguageID.Japanese:
                        code = "ユㅡザㅡ設定";
                        message1 = "パスワㅡドが変更されました。!";
                        message2 = "";
                        break;
                    case DataStore.LanguageID.Czech:
                        code = "uživatelské nastavení";
                        message1 = "Změna hesla dokončena!";
                        message2 = "";
                        break;
                    case DataStore.LanguageID.German:
                        code = "Benutzereinstellungen";
                        message1 = "Passwortänderung abgeschlossen!";
                        message2 = "";
                        break;
                    default:
                        break;
                }

                this.UI_Invoke(delegate
                {
                    this.labelPassword.Text = user.user_pw;
                    this.ParentForm.ParentForm.SystemConfig.CurrentUser.Password = user.user_pw;

                    DialogFormMessage dlg = new DialogFormMessage(DataStore.MessageBoxIcon.Asterisk, code, message1, message2, 0);
                    dlg.ShowDialog();
                });
            }
            else
            {
                this.UI_Invoke(delegate
                {
                    this.labelPassword.Text = this.ParentForm.ParentForm.SystemConfig.CurrentUser.Password;
                    DialogFormMessage msg = new DialogFormMessage(null, user.status, this.ParentForm.ParentForm.SystemConfig.Language);
                    msg.ShowDialog();
                });
            }
        }

        public void DisplayRefresh()
        {
            this.UpdateSelectUserDisplay(this.ParentForm.ParentForm.SystemConfig.CurrentUser);
        }
        #endregion

        #region Event Handler
        private void labelPassword_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormUserEditorKeyboard keyboard = new DialogFormUserEditorKeyboard(this.labelPassword.Text, this.ParentForm.ParentForm.SystemConfig.Language);

            if (keyboard.ShowDialog() == DialogResult.OK)
            {
                value = keyboard.RetStringValue;

                this.labelPassword.Text = "-";

                UserManager.UserManager_UserModifyPW(this.ParentForm.ParentForm.SystemConfig.CurrentUser.ID, value);
            }
        }
        #endregion
    }
}
