using DevExpress.Pdf.Native;
using DevExpress.XtraEditors;
using LFP_Manager.DataStructure;
using LFP_Manager.Function;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DevExpress.XtraPrinting.Export.Pdf.PdfImageCache;

namespace LFP_Manager.Forms
{
    public partial class FmxPasswordChange : DevExpress.XtraEditors.XtraForm
    {
        #region DELEGATE
        public delegate void PasswordResut(int result);
        #endregion

        #region VARIABLES
        private CommConfig Config;
        private int result = -1;

        public event PasswordResut OnResult = null;

        #endregion

        #region CONSTRUCTORS
        public FmxPasswordChange(CommConfig aConfig)
        {
            InitializeComponent();

            Config = aConfig;
        }
        #endregion

        #region FORM EVENT
        private void FmxPassword_FormClosed(object sender, FormClosedEventArgs e)
        {
            OnResult?.Invoke(result);
        }
        #endregion

        #region BUTTON EVENT
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            string sPW;
            string currPW, newPW, cornfirmPW;

            currPW = TeCurrentPW.Text;
            newPW = TeNewPW.Text;
            cornfirmPW = TeCornfirmPW.Text;

            if (newPW.Length < 4)
            {
                MessageBox.Show("Too short new password (Min. Length 4)", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (RbMaster.Checked)
            {
                sPW = Config.MasterPw;
            }
            else if (RbEngineer.Checked)
            {
                sPW = Config.EngineerPw;
            }
            else if (RbTechnician.Checked)
            {
                sPW = Config.TechnicianPw;
            }
            else
            {
                sPW = "";
            }

            if (sPW != currPW)
            {
                MessageBox.Show("The current password is incorrect.", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (newPW != cornfirmPW)
            {
                MessageBox.Show("The new password and its confirmation do not match.", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (currPW == newPW)
            {
                MessageBox.Show("Your new password cannot be the same as your current password.", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (RbMaster.Checked)
            {
                Config.MasterPw = newPW;
            }
            else if (RbEngineer.Checked)
            {
                Config.EngineerPw = newPW;
            }
            else if (RbTechnician.Checked)
            {
                Config.TechnicianPw = newPW;
            }
            else
            {
                return;
            }
            csIniControlFunction.IniSave(Application.ExecutablePath, Config);

            MessageBox.Show("Password changed successfully.", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnResetPW_Click(object sender, EventArgs e)
        {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == "FmxPassword")
                {
                    frm.Activate();
                    return;
                }
            }
            FmxPassword PasswordForm = new FmxPassword(Config);
            if (PasswordForm.ShowDialog() == DialogResult.OK)
            {
                Config.MasterPw = "8003";
                Config.EngineerPw = "7003";
                Config.TechnicianPw = "6003";
                csIniControlFunction.IniSave(Application.ExecutablePath, Config);

                MessageBox.Show("Password reset successful.", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {

            }
        }
        #endregion
    }
}