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

using SmartX;
using ITC81DB_2H.Forms;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H.Datastore;
using ITC81DB_2H.Part11_UserManager;

namespace ITC81DB_2H.Controls
{
    public partial class ControlBottomEquipment : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private Define.E_MenuBottomEquipment CurrentMenu;
        private Collection<SmartButton> CollectionButtonMenu;
        #endregion

        #region Constructor
        public ControlBottomEquipment(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #endregion

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

        #region Method
        public void InitializeDesign()
        {
        }
        private void DefaultSetting()
        {
            this.CurrentMenu = Define.E_MenuBottomEquipment.Help;

            this.CollectionButtonMenu = new Collection<SmartButton>();
            this.CollectionButtonMenu.Clear();
            this.CollectionButtonMenu.Add(this.buttonHelp);
            this.CollectionButtonMenu.Add(this.buttonInitialize);
            this.CollectionButtonMenu.Add(this.buttonUpdate);
            this.CollectionButtonMenu.Add(this.buttonFunction);
            this.CollectionButtonMenu.Add(this.buttonUser);

            if (this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
                this.buttonUser.Visible = true;
            else
                this.buttonUser.Visible = false;
        }

        public void CurrentControlEnable(bool enable)
        {
            foreach (SmartButton button in this.CollectionButtonMenu)
                button.Enabled = enable;
        }

        public void UpdateDisplayMenuButton(Define.E_MenuBottomEquipment menu)
        {
            switch (menu)
            {
                case Define.E_MenuBottomEquipment.FunctionSetting:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonFunction.ButtonDown();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonInitialize.ButtonUp();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUpdate.ButtonUp();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonManufacturerSetting.ButtonUp();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUser.ButtonUp();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonEngineerSetting.ButtonUp();
                    break;
                case Define.E_MenuBottomEquipment.Help:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonFunction.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonHelp.ButtonDown();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonInitialize.ButtonUp();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUpdate.ButtonUp();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonManufacturerSetting.ButtonUp();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUser.ButtonUp();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonEngineerSetting.ButtonUp();
                    break;
                case Define.E_MenuBottomEquipment.Initialize:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonFunction.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonInitialize.ButtonDown();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUpdate.ButtonUp();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonManufacturerSetting.ButtonUp();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUser.ButtonUp();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonEngineerSetting.ButtonUp();
                    break;
                case Define.E_MenuBottomEquipment.Update:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonFunction.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonInitialize.ButtonUp();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonUpdate.ButtonDown();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonManufacturerSetting.ButtonUp();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUser.ButtonUp();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonEngineerSetting.ButtonUp();
                    break;
                case Define.E_MenuBottomEquipment.Manufacturer:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonFunction.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonInitialize.ButtonUp();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUpdate.ButtonUp();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonManufacturerSetting.ButtonDown();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUser.ButtonUp();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonEngineerSetting.ButtonUp();
                    break;
                case Define.E_MenuBottomEquipment.User:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonFunction.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonInitialize.ButtonUp();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUpdate.ButtonUp();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonManufacturerSetting.ButtonUp();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonUser.ButtonDown();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonEngineerSetting.ButtonUp();
                    break;
                case Define.E_MenuBottomEquipment.Engineer:
                    if (this.buttonFunction.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonFunction.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonInitialize.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonInitialize.ButtonUp();
                    if (this.buttonUpdate.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUpdate.ButtonUp();
                    if (this.buttonManufacturerSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonManufacturerSetting.ButtonUp();
                    if (this.buttonUser.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonUser.ButtonUp();
                    if (this.buttonEngineerSetting.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonEngineerSetting.ButtonDown();
                    break;
                default:
                    break;
            }
        }

        private void UpdateDisplayUser(UserItem user)
        {
            if (user.Group == Define.E_UserGroup.Developer && this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
            {
                this.DisplayHiddenEngineer(true);
                this.ParentForm.ParentForm.IsManufacturerVisible = true;
                this.DisplayHiddenManufacturer(true);
            }
            else
            {
                this.DisplayHiddenEngineer(false);
                this.ParentForm.ParentForm.IsManufacturerVisible = false;
                this.DisplayHiddenManufacturer(false);
            }
        }
        public void UpdateMenuDisplay(SystemStatus status)
        {
            switch (status.CurrentUser.Group)
            {
                case Define.E_UserGroup.Level1:
                    this.buttonUpdate.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsEquipUpdate;
                    this.buttonInitialize.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsEquipInitialize;
                    this.buttonFunction.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsEquipFunctionSetting;
                    this.buttonManufacturerSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsEquipManufacturerSetting;
                    this.buttonUser.Enabled = true;
                    this.buttonEngineerSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsEquipEngineerSetting;
                    break;
                case Define.E_UserGroup.Level2:
                    this.buttonUpdate.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsEquipUpdate;
                    this.buttonInitialize.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsEquipInitialize;
                    this.buttonFunction.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsEquipFunctionSetting;
                    this.buttonManufacturerSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsEquipManufacturerSetting;
                    this.buttonUser.Enabled = true;
                    this.buttonEngineerSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsEquipEngineerSetting;
                    break;
                case Define.E_UserGroup.Level3:
                    this.buttonUpdate.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsEquipUpdate;
                    this.buttonInitialize.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsEquipInitialize;
                    this.buttonFunction.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsEquipFunctionSetting;
                    this.buttonManufacturerSetting.Enabled = true;
                    this.buttonUser.Enabled = true;
                    this.buttonEngineerSetting.Enabled = true;
                    break;
                case Define.E_UserGroup.Admin:
                case Define.E_UserGroup.Developer:
                    this.buttonUpdate.Enabled = true;
                    this.buttonInitialize.Enabled = true;
                    this.buttonFunction.Enabled = true;
                    this.buttonManufacturerSetting.Enabled = true;
                    this.buttonUser.Enabled = true;
                    this.buttonEngineerSetting.Enabled = true;
                    break;
                case Define.E_UserGroup.NotLogin:
                    //this.buttonUpdate.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsEquipUpdate;
                    //this.buttonInitialize.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsEquipInitialize;
                    //this.buttonFunction.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsEquipFunctionSetting;
                    //this.buttonUser.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsEquipUserSetting;
                    //this.buttonEngineerSetting.Enabled = true;
                    break;
                default:
                    break;
            }
        }
        public void DisplayHiddenEngineer(bool bValue)
        {
            if (this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
                this.buttonEngineerSetting.Location = new Point(385, 0);
            else
                this.buttonEngineerSetting.Location = new Point(314, 0);
            this.buttonEngineerSetting.Visible = bValue;
        }
        public void DisplayHiddenManufacturer(bool bValue)
        {
            if (bValue == true)
            {
                if (this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
                    this.buttonManufacturerSetting.Location = new Point(456, 0);
                else
                    this.buttonManufacturerSetting.Location = new Point(385, 0);

                if (this.ParentForm.ParentForm.IsManufacturerVisible == true)
                    this.buttonManufacturerSetting.Visible = true;
            }
            else
                this.buttonManufacturerSetting.Visible = false;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.buttonHelp.ButtonDown();
            this.buttonUpdate.ButtonUp();
            this.buttonInitialize.ButtonUp();
            this.buttonFunction.ButtonUp();
            this.buttonUser.ButtonUp();
            this.buttonEngineerSetting.ButtonUp();
            this.buttonManufacturerSetting.ButtonUp();

            this.CurrentMenu = Define.E_MenuBottomEquipment.Help;
            this.ParentForm.DisplayBottomEquipment(this.CurrentMenu);

            this.UpdateDisplayUser(status.CurrentUser);
        }
        #endregion

        #region Event Handler
        private void buttonMenu_Click(object sender, EventArgs e)
        {
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            if (button == this.buttonHelp)
                this.CurrentMenu = Define.E_MenuBottomEquipment.Help;
            else if (button == this.buttonUpdate)
                this.CurrentMenu = Define.E_MenuBottomEquipment.Update;
            else if (button == this.buttonInitialize)
                this.CurrentMenu = Define.E_MenuBottomEquipment.Initialize;
            else if (button == this.buttonFunction)
                this.CurrentMenu = Define.E_MenuBottomEquipment.FunctionSetting;
            else if (button == this.buttonManufacturerSetting)
                this.CurrentMenu = Define.E_MenuBottomEquipment.Manufacturer;
            else if (button == this.buttonUser)
                this.CurrentMenu = Define.E_MenuBottomEquipment.User;
            else if (button == this.buttonEngineerSetting)
                this.CurrentMenu = Define.E_MenuBottomEquipment.Engineer;
            else
                this.CurrentMenu = Define.E_MenuBottomEquipment.Help;

            this.ParentForm.DisplayBottomEquipment(this.CurrentMenu);
        }
        #endregion
    }
}
