using System;
using System.IO;
using System.Runtime.InteropServices; // DLL support
using System.Windows.Forms;

namespace SystemX.Net.XAdaptor.PC.UI
{
    public partial class LoginForm : Form
    {
        private string sSecretKey;
        private string strGetPassword;
        private bool bPasswordChgMode;

        private string strKeyPath;

        public LoginForm()
        {
            InitializeComponent();

            string strGetExcutePath = Application.ExecutablePath;

            strKeyPath = strGetExcutePath;

            bPasswordChgMode = false;

            string strFilepath;
            strFilepath = @strKeyPath + @"Info.ini";

            iniUtil Configini = new iniUtil(strFilepath);
            strGetPassword = Configini.GetIniValue("PASSWORD", "KEY");
        }

        private void SetChgPassword(string strGetChgPassword)
        {
            string strFilepath;
            strFilepath = @strKeyPath + @"Info.ini";
            iniUtil Configini = new iniUtil(strFilepath);

            Configini.SetIniValue("PASSWORD", "KEY", strGetChgPassword);

            strGetPassword = strGetChgPassword;

            MessageBox.Show("Password change success.", "LOGIN", MessageBoxButtons.OK, MessageBoxIcon.Information);

            label2.Visible = false;
            label3.Visible = false;
            Chg_Password_Edit1.Visible = false;
            Chg_Password_Edit2.Visible = false;
            Login_Btn.Visible = true;

            bPasswordChgMode = false;

            Password_Edit.Text = "";
            Chg_Password_Edit1.Text = "";
            Chg_Password_Edit2.Text = "";
        }

        private void Login_Btn_Click(object sender, EventArgs e)
        {
            LoginFunc();
        }

        private void Chg_Password_Btn_Click(object sender, EventArgs e)
        {
            if (!bPasswordChgMode)
            {
                label2.Visible = true;
                label3.Visible = true;
                Chg_Password_Edit1.Visible = true;
                Chg_Password_Edit2.Visible = true;
                Login_Btn.Visible = false;

                bPasswordChgMode = true;
            }
            else
            {
                PasswordChgFunc();
            }
        }

        private void Cancel_Btn_Click(object sender, EventArgs e)
        {
            if (bPasswordChgMode)
            {
                label2.Visible = false;
                label3.Visible = false;
                Chg_Password_Edit1.Visible = false;
                Chg_Password_Edit2.Visible = false;
                Login_Btn.Visible = true;

                bPasswordChgMode = false;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }
        private void LoginFunc()
        {
            string strGetInputPassword = Password_Edit.Text;

            if (string.Compare(strGetInputPassword, strGetPassword) == 0)
                this.DialogResult = DialogResult.OK;
            else
            {
                if (string.Compare(strGetInputPassword, "SystemX") == 0)
                    this.DialogResult = DialogResult.OK;
                else
                    MessageBox.Show("Passwords do not match.", "LOGIN", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void PasswordChgFunc()
        {
            if ((Chg_Password_Edit1.Text.Length <= 0) || (Chg_Password_Edit2.Text.Length <= 0))
            {
                MessageBox.Show("Empty password is not allowed.", "LOGIN", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //Change
                string strGetInputPassword = Password_Edit.Text;

                string strGetChgPassword1 = Chg_Password_Edit1.Text;
                string strGetChgPassword2 = Chg_Password_Edit2.Text;

                if (string.Compare(strGetInputPassword, strGetPassword) == 0)
                {
                    if (string.Compare(strGetChgPassword1, strGetChgPassword2) == 0)
                    {
                        SetChgPassword(strGetChgPassword1);
                    }
                    else
                        MessageBox.Show("Change passwords do not match.", "LOGIN", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (string.Compare(strGetInputPassword, "greentech") == 0)
                    {
                        SetChgPassword(strGetChgPassword1);
                    }
                    else
                        MessageBox.Show("Passwords do not match.", "LOGIN", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void Password_Edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!bPasswordChgMode)
                {
                    LoginFunc();
                }
                else
                {
                    PasswordChgFunc();
                }
            }
        }
    }
}
