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 EduNetworkBuilder
{
    public partial class ShapeEditor : Form
    {
        NetShape ShapeForEditing = null;

        public ShapeEditor()
        {
            InitializeComponent();
            LocalSetup();
        }

        public ShapeEditor(NetShape WhatToEdit)
        {
            InitializeComponent();
            ShapeForEditing = WhatToEdit;
            LocalSetup();
            lbShapeList.Text = WhatToEdit.ToString(); //Select the item
        }

        void LocalSetup()
        {

            Network myNet = NB.GetNetwork();
            List<String> ColorNames = myNet.ColorNames;
            ColorNames.Sort();

            cbFillColor.Items.Clear();
            cbLineColor.Items.Clear();
            foreach (string one in ColorNames)
            {
                cbFillColor.Items.Add(one);
                cbLineColor.Items.Add(one);
            }

            cbShape.Items.Clear();
            foreach(NetShapeType one in Enum.GetValues(typeof(NetShapeType)))
            {
                if (one != NetShapeType.none)
                    cbShape.Items.Add(one.ToString());
            }

            tbHeight.Leave += DataUpdated;
            tbWidth.Leave += DataUpdated;
            tbX.Leave += DataUpdated;
            tbY.Leave += DataUpdated;
            cbFillColor.Leave += DataUpdated;
            cbLineColor.Leave += DataUpdated;
            cbShape.Leave += DataUpdated;
            tbName.Leave += DataUpdated;

            FillFormItems();
        }

        private void LanguagifyComponents()
        {
            NBSettings OurSettings = NB.GetSettings();
            lblFill.Text = NB.Translate("SE_Fill", OurSettings);
            lblLine.Text = NB.Translate("SE_Line", OurSettings);
            lblX.Text = NB.Translate("SE_X", OurSettings);
            lblY.Text = NB.Translate("SE_Y", OurSettings);
            lblWidth.Text = NB.Translate("SE_Width", OurSettings);
            lblHeight.Text = NB.Translate("SE_Height", OurSettings);
            lblShape.Text = NB.Translate("SE_Shape", OurSettings);
            lblShapes.Text = NB.Translate("SE_Shapes", OurSettings);
            lblName.Text = NB.Translate("SE_Name", OurSettings);
        }

        void FillFormItems(bool SaveIndex = false)
        {
            Network myNet = NB.GetNetwork();

            int currentindex = lbShapeList.SelectedIndex;

            lbShapeList.Items.Clear();
            foreach (string one in myNet.ShapeDescriptions())
            {
                lbShapeList.Items.Add(one);
            }
            if (SaveIndex && currentindex >= 0) lbShapeList.SelectedIndex = currentindex;
        }

        void DataUpdated(object sender, EventArgs e)
        {
            ValuesToShape();
        }

        private void lbShapeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeSelectionTo(lbShapeList.Text);
        }

        void ChangeSelectionTo(string ToSelect)
        {
            Network myNet = NB.GetNetwork();
            //Fill in the text from the shape

            ShapeForEditing = null;
            foreach (NetShape oneShape in myNet.Shapes)
            {
                if (oneShape.ToString() == ToSelect)
                    ShapeForEditing = oneShape;
            }
            FieldsFromShape(ShapeForEditing);
        }

        private void FieldsFromShape()
        {
            FieldsFromShape(ShapeForEditing);
        }
        private void FieldsFromShape(NetShape theShape)
        {
            ShapeForEditing = theShape;
            if(ShapeForEditing != null)
            {
                tbX.Text = ShapeForEditing.InArea.X.ToString();
                tbY.Text = ShapeForEditing.InArea.Y.ToString();
                tbHeight.Text = ShapeForEditing.InArea.Height.ToString();
                tbWidth.Text = ShapeForEditing.InArea.Width.ToString();
                cbFillColor.Text = ShapeForEditing.FillColor.Name;
                cbLineColor.Text = ShapeForEditing.LineColor.Name;
                cbShape.Text = ShapeForEditing.MyShape.ToString();
                tbName.Text = ShapeForEditing.Name;
            }
            else
            {
                {
                    tbX.Text = "0";
                    tbY.Text = "0";
                    tbHeight.Text = "0";
                    tbWidth.Text = "0";
                    cbFillColor.Text = "Empty";
                    cbLineColor.Text = "Empty";
                    cbShape.Text = "None";
                    tbName.Text = "";
                }
            }
        }

        private void ValuesToShape()
        {
            ValuesToShape(ShapeForEditing);
        }
        private void ValuesToShape(NetShape theShape)
        {
            ShapeForEditing = theShape;
            if (ShapeForEditing != null)
            {
                int X, Y, Width, Height;
                X = ShapeForEditing.InArea.X;
                Y = ShapeForEditing.InArea.Y;
                Width = ShapeForEditing.InArea.Width;
                Height = ShapeForEditing.InArea.Height;
                int.TryParse(tbX.Text, out X);
                int.TryParse(tbY.Text, out Y);
                int.TryParse(tbWidth.Text, out Width);
                int.TryParse(tbHeight.Text, out Height);
                if (X < 0) X = 0;
                if (Y < 0) Y = 0;
                if (Height < 0) Height = 0;
                if (Width < 0) Width = 0;
                ShapeForEditing.InArea = new Rectangle(X, Y, Width, Height);
                ShapeForEditing.FillColor = Color.FromName(cbFillColor.Text);
                ShapeForEditing.LineColor = Color.FromName(cbLineColor.Text);
                ShapeForEditing.MyShape = NB.TryParseEnum<NetShapeType>(cbShape.Text, NetShapeType.rectangle);
                ShapeForEditing.Name = tbName.Text;
            }
            FillFormItems(true); //Update the listbox
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lbShapeList_MouseUp(object sender, MouseEventArgs e)
        {
            if(lbShapeList.SelectedItem != null)
            {
                //We have an item that we have clicked on
                ChangeSelectionTo(lbShapeList.Text);
            }
            if (e.Button == MouseButtons.Right)
            {
                //We should make a context menu here.
                //  Delete
                //  Move Up
                //  Move Down
                //  Move to top
                //  Move to bottom
                if(ShapeForEditing != null)
                {
                    int index = 0;
                    if (lbShapeList.ContextMenuStrip == null)
                    {
                        lbShapeList.ContextMenuStrip = new ContextMenuStrip();
                    }
                    lbShapeList.ContextMenuStrip.Items.Clear();
                    lbShapeList.ContextMenuStrip.Items.Add(NB.Translate("_Delete"));
                    lbShapeList.ContextMenuStrip.Items[index++].Click += lblShape_Delete;
                    lbShapeList.ContextMenuStrip.Items.Add(NB.Translate("SE_MoveToTop"));
                    lbShapeList.ContextMenuStrip.Items[index++].Click += lblShape_MoveToTop;
                    lbShapeList.ContextMenuStrip.Items.Add(NB.Translate("SE_MoveUp"));
                    lbShapeList.ContextMenuStrip.Items[index++].Click += lblShape_MoveUp;
                    lbShapeList.ContextMenuStrip.Items.Add(NB.Translate("SE_MoveDown"));
                    lbShapeList.ContextMenuStrip.Items[index++].Click += lblShape_MoveDown;
                    lbShapeList.ContextMenuStrip.Items.Add(NB.Translate("SE_MoveToTop"));
                    lbShapeList.ContextMenuStrip.Items[index++].Click += lblShape_MoveToTop;

                    lbShapeList.ContextMenuStrip.Visible = true;
                    lbShapeList.ContextMenuStrip.Show(Cursor.Position);
                }
            }
            //If it is a left mouse button, we simply select the item so we can edit it
        }

        void lblShape_Delete(object sender, EventArgs e)
        {
            if(ShapeForEditing != null)
            {
                Network myNet = NB.GetNetwork();
                myNet.RemoveShape(ShapeForEditing);
                FillFormItems();
            }
        }

        void lblShape_MoveToTop(object sender, EventArgs e)
        {
            if (ShapeForEditing != null)
            {
                Network myNet = NB.GetNetwork();
                myNet.RemoveShape(ShapeForEditing);
                myNet.Shapes.Insert(0, ShapeForEditing);
                FillFormItems();
            }
        }

        void lblShapeMove(int delta)
        {
            if (delta == 0) return;
            if (ShapeForEditing == null) return;

            Network myNet = NB.GetNetwork();
            int index = myNet.Shapes.IndexOf(ShapeForEditing);
            int newindex = index + delta;
            myNet.Shapes.Remove(ShapeForEditing);
            if (newindex < 0) newindex = 0;
            if (newindex >= myNet.Shapes.Count) myNet.Shapes.Add(ShapeForEditing);
            else myNet.Shapes.Insert(newindex, ShapeForEditing);
            FillFormItems();
        }

        void lblShape_MoveUp(object sender, EventArgs e)
        {
            lblShapeMove(-1);
        }
        void lblShape_MoveDown(object sender, EventArgs e)
        {
            lblShapeMove(1);
        }
        void lblShape_MoveToEnd(object sender, EventArgs e)
        {
            if (ShapeForEditing != null)
            {
                Network myNet = NB.GetNetwork();
                myNet.RemoveShape(ShapeForEditing);
                myNet.Shapes.Add(ShapeForEditing);
                FillFormItems();
            }
        }

        private void lbShapeList_MouseDown(object sender, MouseEventArgs e)
        {

        }
    }
}
