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.Forms;

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

        private DataStore.MenuBottomSystem CurrentMenu;
        private Collection<SmartButton> CollectionButtonMenu;
        #endregion

        #region Constructor
        public ControlBottomSystem(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 = DataStore.MenuBottomSystem.Help;

            this.CollectionButtonMenu = new Collection<SmartButton>();
            this.CollectionButtonMenu.Clear();
            this.CollectionButtonMenu.Add(this.buttonHelp);
            this.CollectionButtonMenu.Add(this.buttonCalibration);
            this.CollectionButtonMenu.Add(this.buttonJudgmentSetting);
            this.CollectionButtonMenu.Add(this.buttonSorter);
            this.CollectionButtonMenu.Add(this.buttonAutoZero);
            this.CollectionButtonMenu.Add(this.buttonExternalOutput);
            this.CollectionButtonMenu.Add(this.buttonIOTest);
        }

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

        public void UpdateMenuDisplay(SystemStatus status)
        {
            switch (status.CurrentUser.Group)
            {
                case DataStore.UserGroup.Level1Operator:
                    this.buttonCalibration.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemCalibration;
                    this.buttonJudgmentSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemJudgmentSetting;
                    this.buttonSorter.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemSorterSetting;
                    this.buttonAutoZero.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemAutoZero;
                    this.buttonIOTest.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemIOTest;
                    this.buttonExternalOutput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemExternalOutput;
                    this.buttonExternalInput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsSystemExternalInput;
                    break;
                case DataStore.UserGroup.Level2Engineer:
                    this.buttonCalibration.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemCalibration;
                    this.buttonJudgmentSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemJudgmentSetting;
                    this.buttonSorter.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemSorterSetting;
                    this.buttonAutoZero.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemAutoZero;
                    this.buttonIOTest.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemIOTest;
                    this.buttonExternalOutput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemExternalOutput;
                    this.buttonExternalInput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsSystemExternalInput;
                    break;
                case DataStore.UserGroup.Level3Manager:
                    this.buttonCalibration.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemCalibration;
                    this.buttonJudgmentSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemJudgmentSetting;
                    this.buttonSorter.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemSorterSetting;
                    this.buttonAutoZero.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemAutoZero;
                    this.buttonIOTest.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemIOTest;
                    this.buttonExternalOutput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemExternalOutput;
                    this.buttonExternalInput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsSystemExternalInput;
                    break;
                case DataStore.UserGroup.Level4Developer:
                    this.buttonCalibration.Enabled = true;
                    this.buttonJudgmentSetting.Enabled = true;
                    this.buttonSorter.Enabled = true;
                    this.buttonAutoZero.Enabled = true;
                    this.buttonIOTest.Enabled = true;
                    this.buttonExternalOutput.Enabled = true;
                    this.buttonExternalInput.Enabled = true;
                    break;
                case DataStore.UserGroup.NotLogin:
                    this.buttonCalibration.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemCalibration;
                    this.buttonJudgmentSetting.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemJudgmentSetting;
                    this.buttonSorter.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemSorterSetting;
                    this.buttonAutoZero.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemAutoZero;
                    this.buttonIOTest.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemIOTest;
                    this.buttonExternalOutput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemExternalOutput;
                    this.buttonExternalInput.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsSystemExternalInput;
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayMenuButton(DataStore.MenuBottomSystem menu)
        {
            switch (menu)
            {
                case DataStore.MenuBottomSystem.AutoZero:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonAutoZero.ButtonDown();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.Calibration:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonCalibration.ButtonDown();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.ExternalOutput:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonExternalOutput.ButtonDown();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.Help:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonHelp.ButtonDown();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.IOTest:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonIOTest.ButtonDown();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.JudgmentSetting:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonJudgmentSetting.ButtonDown();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.SorterSetting:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonSorter.ButtonDown();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalInput.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.ExternalInput:
                    if (this.buttonAutoZero.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonAutoZero.ButtonUp();
                    if (this.buttonCalibration.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonCalibration.ButtonUp();
                    if (this.buttonExternalOutput.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonExternalOutput.ButtonUp();
                    if (this.buttonHelp.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonHelp.ButtonUp();
                    if (this.buttonIOTest.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonIOTest.ButtonUp();
                    if (this.buttonJudgmentSetting.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonJudgmentSetting.ButtonUp();
                    if (this.buttonSorter.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonSorter.ButtonUp();
                    if (this.buttonExternalInput.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonExternalInput.ButtonDown();
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.buttonHelp.ButtonDown();
            this.buttonCalibration.ButtonUp();
            this.buttonJudgmentSetting.ButtonUp();
            this.buttonAutoZero.ButtonUp();
            this.buttonIOTest.ButtonUp();

            this.CurrentMenu = DataStore.MenuBottomSystem.Help;
            this.ParentForm.DisplayBottomSystem(this.CurrentMenu);
        }
        #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 = DataStore.MenuBottomSystem.Help;
            else if (button == this.buttonCalibration)
                this.CurrentMenu = DataStore.MenuBottomSystem.Calibration;
            else if (button == this.buttonJudgmentSetting)
                this.CurrentMenu = DataStore.MenuBottomSystem.JudgmentSetting;
            else if (button == this.buttonSorter)
                this.CurrentMenu = DataStore.MenuBottomSystem.SorterSetting;
            else if (button == this.buttonAutoZero)
                this.CurrentMenu = DataStore.MenuBottomSystem.AutoZero;
            else if (button == this.buttonExternalOutput)
                this.CurrentMenu = DataStore.MenuBottomSystem.ExternalOutput;
            else if (button == this.buttonIOTest)
                this.CurrentMenu = DataStore.MenuBottomSystem.IOTest;
            else if (button == this.buttonExternalInput)
                this.CurrentMenu = DataStore.MenuBottomSystem.ExternalInput;
            else
                this.CurrentMenu = DataStore.MenuBottomSystem.Help;

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