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

namespace INT51DB.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 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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonServo.ButtonUp();
                    break;
                case DataStore.MenuBottomSystem.ServoMotor:
                    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.buttonServo.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonServo.ButtonDown();
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.buttonHelp.ButtonDown();
            this.buttonCalibration.ButtonUp();
            this.buttonJudgmentSetting.ButtonUp();
            this.buttonAutoZero.ButtonUp();
            this.buttonIOTest.ButtonUp();
            this.buttonServo.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.buttonServo)
                this.CurrentMenu = DataStore.MenuBottomSystem.ServoMotor;
            else
                this.CurrentMenu = DataStore.MenuBottomSystem.Help;

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