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 INT69DC_ImageDll;
using INT69DC_7C.Forms;
using INT69DC_7C.DialogForms;
using INT69DC_7C.Part11_UserManager;

namespace INT69DC_7C.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)
            {
                this.labelTitlePassword.Text = "Password";
                this.labelTitleExpiryAccount.Text = "ExpireAccount";
                this.labelTitleExpiryPassword.Text = "ExpirePassword";

                this.labelStatusExpireAccount.Text = "Expired";
                this.labelStatusExpirePassword.Text = "Expired";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitlePassword.Text = "パスワㅡド";
                this.labelTitleExpiryAccount.Text = "アカウントの有効期限";
                this.labelTitleExpiryPassword.Text = "パスワㅡドの有効期限";

                this.labelStatusExpireAccount.Text = "満了";
                this.labelStatusExpirePassword.Text = "満了";
            }
            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.labelStatusExpireAccount.Visible = item.IsLockAccount;
            this.labelStatusExpirePassword.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.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;
                    case DataStore.LanguageID.Japanese:
                        code = "ユㅡザㅡ設定";
                        message1 = "パスワㅡドの変更が完了しました。";
                        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);

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

                this.labelPassword.Text = "-";

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

        private void button1_Click(object sender, EventArgs e)
        {
            UserManager.UserManager_UserModifyPW("test01", "test01%");
        }
    }
}
