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;

namespace DataManipulation
{
    public partial class MainForm : Form
    {

        #region const
        Color MENU_BACK_COLOR = System.Drawing.Color.FromArgb(249, 249, 249);
        Color MENU_ONOVER_COLOR = System.Drawing.Color.FromArgb(21, 164, 250);
        Color MENU_SELECTED_COLOR = System.Drawing.Color.FromArgb(230, 230, 230);
        int MENU_BUTTON_HEIGHT = 30;
        #endregion

        Image _menuButtonImage;

        string[] menuButtons =
        {
            "Top-N", "Sorting", "Aggregation", "Y-Function Series", "Parametric Function Series"
        };
        public MainForm()
        {
            InitializeComponent();

            InitializeMenu(); 
        }

        private void InitializeMenu()
        {
            panelLinks.BackColor = MENU_BACK_COLOR;

            var top = 20;
            foreach (var item in menuButtons)
            {
                var newButton = new Button()
                {
                    Text = item,
                    Font = new Font("Calibri", 9, FontStyle.Bold),
                    ForeColor = Color.FromArgb(64, 64, 64),
                    TextAlign =  ContentAlignment.MiddleLeft,
                    Width = panelLinks.Width - 2,
                    Height = MENU_BUTTON_HEIGHT,
                    Top = top,
                    FlatStyle = FlatStyle.Flat,
                    Left = 1
                };
                top += MENU_BUTTON_HEIGHT;
                newButton.FlatAppearance.BorderSize = 0;
                newButton.FlatAppearance.MouseOverBackColor = MENU_ONOVER_COLOR;
                newButton.Padding = new Padding(15, 0, 0, 0);
                newButton.Click += button1_Click;
                panelLinks.Controls.Add(newButton);

                //Init button style image
                _menuButtonImage = new Bitmap(panelLinks.Width + 15, MENU_BUTTON_HEIGHT + 1);
                using (Graphics g = Graphics.FromImage(_menuButtonImage))
                {
                    g.Clear(MENU_SELECTED_COLOR);
                    g.FillRectangle(new SolidBrush(MENU_ONOVER_COLOR), 0, 0, 7, MENU_BUTTON_HEIGHT);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Open first link
            if (panelLinks.Controls[0] is Button)
                (panelLinks.Controls[0] as Button).PerformClick();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var clickedButton = (sender as Button);
            panelMain.Controls.Clear();
            Control newControl;
            var str = clickedButton.Text;
            switch (str)
            {
                case "Top-N": newControl = new TopN(); break;
                case "Sorting": newControl = new Sorting(); break;
                case "Aggregation": newControl = new Aggregation(); break;
                case "Y-Function Series": newControl = new YFunctionSeriesSample(); break;
                case "Parametric Function Series": newControl = new ParametricFunctionSeriesSample(); break;
                default: newControl = new TopN(); break;
            }

            newControl.Dock = DockStyle.Fill;
            panelMain.Controls.Add(newControl);

            //Paint selection button color
            foreach (Button item in panelLinks.Controls) 
                item.Image = null;

            clickedButton.Image = _menuButtonImage;
        }

        private void panelLinks_Paint(object sender, PaintEventArgs e)
        {
            using (var graphics = panelLinks.CreateGraphics())
            {
                graphics.Clear(MENU_BACK_COLOR);
                using (var pen = new Pen(new SolidBrush(Color.DarkGray)))
                {
                    graphics.DrawRectangle(pen, 0, 0, panelLinks.Width - 1, panelLinks.Height - 1);
                }
            }
        }
    }
}
