using Remontor.Connector;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Remontor
{
    enum BarShow
    {
        HideBar,
        ShowHistory,
        ShowWords,
        ShowSetting
    }
    internal partial class BarSessionControl : UserControl
    {
        public delegate bool HideBars(BarShow state);
        public HideBars hideTabWords;
        public HideBars hideTabHistory;
        public HideBars hideTabSetting;

        private BarShow barShow = BarShow.HideBar;
        public List<BarButtonControl> buttons;


      
        public BarSessionControl()
        {
            InitializeComponent();
            SConnector.Update += UpdateBar;
            SConnector.UpdateMode += SetModeMonitor;
            buttons = new List<BarButtonControl>();
        }

        public void UpdateBar(List<Connect> connects)
        {
            bool GreyBackground = false;
            buttons.Clear();
            FlowButton.Controls.Clear();
            foreach (var item in connects)
            {
                BarButtonControl button = new BarButtonControl(item, GreyBackground);
                buttons.Add(button);
                GreyBackground = !GreyBackground;
                FlowButton.Controls.Add(button);
            }

            FlowButton.Controls.Add(NewBtn);
        }

        public void ShowBarSet(BarShow barShow)
        {

            if (barShow == BarShow.ShowSetting)
            {
                hideTabSetting(barShow);
                ShowSetting.BackColor = SystemColors.ActiveCaption;
            }
            else
            {
                if (hideTabSetting(barShow))
                {
                    barShow = BarShow.ShowSetting;
                    ShowSetting.BackColor = SystemColors.ActiveCaption;
                }
                else
                {
                    ShowSetting.BackColor = SystemColors.Control;
                }
                
            }
            if (barShow == BarShow.ShowWords) { ShowWords.BackColor = SystemColors.ActiveCaption; }
            else { ShowWords.BackColor = SystemColors.Control; }
            if (barShow == BarShow.ShowHistory) { ShowBar.BackColor = SystemColors.ActiveCaption; }
            else { ShowBar.BackColor = SystemColors.Control; }
           
            hideTabWords(barShow);
            hideTabHistory(barShow);
            
            

        }

        private void ShowBar_Click(object sender, EventArgs e)
        {
            ShowBarSet(BarShow.ShowHistory);
        }

        private void ShowWords_Click(object sender, EventArgs e)
        {
            ShowBarSet(BarShow.ShowWords);
        }
        private void ShowSetting_Click(object sender, EventArgs e)
        {
            ShowBarSet(BarShow.ShowSetting);
        }

        private void NewBtn_Click(object sender, EventArgs e)
        {
            Finder.Finder finder = new Finder.Finder();
            finder.ShowDialog();
        }

        private void FlowButton_ControlRemoved(object sender, ControlEventArgs e)
        {
            if(e.Control is BarButtonControl)  e.Control.Dispose();
        }

        private void BarSessionControl_Load(object sender, EventArgs e)
        {

        }


        public void SetModeMonitor(IModeMon mode)
        {
            if (mode is OneMon) { this.OneMonBt.BackColor = SystemColors.ActiveCaption; }
            else { this.OneMonBt.BackColor = SystemColors.Control; }
            if (mode is DobleMonOne) { this.DobleMonOneBt.BackColor = SystemColors.ActiveCaption; }
            else { this.DobleMonOneBt.BackColor = SystemColors.Control; }
            if (mode is DobleMonTwo) { this.DobleMonTwoBt.BackColor = SystemColors.ActiveCaption; }
            else { this.DobleMonTwoBt.BackColor = SystemColors.Control; }
            if (mode is DobleMonFull) { this.DobleMonFull.BackColor = SystemColors.ActiveCaption; }
            else { this.DobleMonFull.BackColor = SystemColors.Control; }
        }

        private void OneMonBt_Click(object sender, EventArgs e)
        {
            SConnector.SetModeMon(new OneMon());
        }

        private void DobleMonOneBt_Click(object sender, EventArgs e)
        {
            SConnector.SetModeMon(new DobleMonOne());
        }

        private void DobleMonTwoBt_Click(object sender, EventArgs e)
        {
            SConnector.SetModeMon(new DobleMonTwo());
        }

        private void DobleMonFull_Click(object sender, EventArgs e)
        {
            SConnector.SetModeMon(new DobleMonFull());
        }

        private void CloseMenu()
        {
            RecoonectBt.BackColor = SystemColors.Control;
        }
        private void RecoonectBt_Click(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
              
                if (SConnector.ActionConnect != null)
                {
                   // SConnector.DeleteConnect(SConnector.ActionConnect);
                    SConnector.ActionConnect.Reconnect();
                   // SConnector.NewConnect(SConnector.ActionConnect.GetComp);
                }
            }else if (e.Button == MouseButtons.Right)
            {
                if (SConnector.ActionConnect != null)
                {
                    if (sender is Button)
                    {
                        Button bt = (Button)sender;
                        bt.BackColor = SystemColors.ActiveCaption;
                        Control control = (Control)sender;
                        SConnector.FormMain.MenuStripCommand.ShowMenuUpdate(SConnector.ActionConnect, control.PointToScreen(e.Location), CloseMenu);
                    }
                }
               
            }
            
            
        }

        private void BarSessionControl_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
                  Color.Black, 0, ButtonBorderStyle.Dotted, // left
                  Color.Black, 0, ButtonBorderStyle.Dotted, // top
                  Color.Black, 0, ButtonBorderStyle.Dotted, // right
                  Color.Black, 1, ButtonBorderStyle.Solid);// bottom
        }
    }
}
