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;
using System.Globalization;
using System.Resources;


namespace EduNetworkBuilder
{
    public partial class OptionsWindow : Form
    {
        private Network myNet;
        private ToolTip myToolTip = new ToolTip();
        Form QuestionForm = new Form();

        private OptionsWindow()
        {
            InitializeComponent();
        }

        public OptionsWindow(Network theNet)
        {
            myNet = theNet;
            InitializeComponent();
            LocalSetup();
            myToolTip.SetToolTip(tbItemSize, NB.Translate("OW_OWSize"));
            myToolTip.SetToolTip(lblItemSize, NB.Translate("OW_OWSize"));
            myToolTip.SetToolTip(tbMessage, NB.Translate("OW_OWLoad"));
            myToolTip.SetToolTip(lblNetMessage, NB.Translate("OW_OWLoad"));
            myToolTip.SetToolTip(tbNetworkTitle, NB.Translate("OW_OWTitle"));
            myToolTip.SetToolTip(lblNetTitle, NB.Translate("OW_OWTitle"));
            myToolTip.SetToolTip(lblNetSize, NB.Translate("OW_OWGraphic"));
            myToolTip.SetToolTip(tbNetworkX, NB.Translate("OW_OWWidth"));
            myToolTip.SetToolTip(tbNetworkY, NB.Translate("OW_OWHight"));
            myToolTip.SetToolTip(lbTags, NB.Translate("OW_OWTag"));
            myToolTip.SetToolTip(lblTags, NB.Translate("OW_OWTag"));
            myToolTip.SetToolTip(lbTests, NB.Translate("OW_OWThings"));
            myToolTip.SetToolTip(lblTests, NB.Translate("OW_OWThings"));
            myToolTip.SetToolTip(lblLevel, NB.Translate("OW_OWLevel"));
            myToolTip.SetToolTip(tbLevel, NB.Translate("OW_OWLevel"));
            myToolTip.SetToolTip(lblSortOrder, NB.Translate("OW_OWSort"));
            myToolTip.SetToolTip(tbSortOrder, NB.Translate("OW_OWSort"));
            myToolTip.SetToolTip(cb_ColoredPackets, NB.Translate("OW_OWVLANColoredPacketsTT"));
            myToolTip.SetToolTip(cbVLANs, NB.Translate("OW_OWVLANs"));
            myToolTip.SetToolTip(tbVidURL, NB.Translate("OW_OWVidURL"));
            LoadValuesFromNetwork();
            Icon = Properties.Resources.NBIco;
        }

        private void LocalSetup()
        {
            cbCaptions.Items.Clear();
            foreach(string one in CaptionOptionList())
            {
                string ToTranslate = "Options_Caption_" + one;
                cbCaptions.Items.Add(NB.Translate(ToTranslate));
            }
        }

        private void LanguagifyComponents()
        {
            Text = NB.Translate("_Done");
            lblNetTitle.Text = NB.Translate("OW_lblNetTitle");
            lblCaptions.Text = NB.Translate("OW_cbDisplayTitles");
            lblNetSize.Text = NB.Translate("OW_lblNetSize");
            lblNetSize.Text = NB.Translate("OW_lblItemSize");
            lblNetMessage.Text = NB.Translate("OW_lblNetMessage");
            lblTests.Text = NB.Translate("OW_lblTests");
            lblTags.Text = NB.Translate("OW_lblTags");
            lblLevel.Text = NB.Translate("OW_lblLevel");
            lblSortOrder.Text = NB.Translate("OW_lblSortOrder");
            lblStartingHelp.Text = NB.Translate("OW_lblStartingHelp");
            cbVLANs.Text = NB.Translate("OW_cbVLANs");
            cb_ColoredPackets.Text = NB.Translate("OW_OWVLANColoredPackets");
            lblVidURL.Text = NB.Translate("OW_VidURL");
            Text = NB.Translate("OW_Form");
        }

        List<string> CaptionOptionList()
        {
            List<string> theList = new List<string>();
            foreach (string one in Enum.GetNames(typeof(CaptionType)))
                theList.Add(one);
            return theList;
        }

        private void LoadValuesFromNetwork()
        {
            tbItemSize.Text = myNet.itemsize.ToString();
            tbMessage.Text = myNet.NetMessage.GetText();
            tbNetworkTitle.Text = myNet.NetTitle.GetText();
            tbVidURL.Text = myNet.NetURL.GetText();

            //select the Caption Level item we have defined.
            List<string> CaptionItemList = CaptionOptionList();
            int index = CaptionItemList.IndexOf(myNet.OptionShowLabels.ToString());
            if (index < 0) index = 0;
            if (index >= 0) cbCaptions.SelectedIndex = index;

            cbVLANs.Checked = myNet.VLANsEnabled;
            cb_ColoredPackets.Checked = myNet.VLANPacketColors;
            tbNetworkX.Text = myNet.myWidth.ToString();
            tbNetworkY.Text = myNet.myHeight.ToString();
            tbLevel.Text = myNet.Level.ToString();
            tbSortOrder.Text = myNet.SortOrder.ToString("00.000");

            lbTests.Items.Clear();
            string what;
            foreach(NetTest NT in myNet.NetTests)
            {
                what = NT.GetDescription(NetTestVerbosity.full);
                if (what != "")
                {
                    lbTests.Items.Add(what);
                }
            }
            lbTags.Items.Clear();
            myNet.SuggestedReadings.Sort();
            foreach (HelpTopics HT in myNet.SuggestedReadings)
            {
                lbTags.Items.Add(HT.ToString());
            }
            cbStartingHelpLevel.Items.Clear();
            foreach (string helplevel in Enum.GetNames(typeof(NetTestVerbosity)))
            {
                cbStartingHelpLevel.Items.Add(helplevel);
            }
            cbStartingHelpLevel.SelectedItem = myNet.StartingHelpLevel.ToString();
            cbVLANs_CheckedChanged(this, EventArgs.Empty);
        }

        private void SaveValuesToNetwork()
        {
            int.TryParse(tbItemSize.Text, out myNet.itemsize);
            myNet.NetMessage.Add( tbMessage.Text);
            myNet.NetTitle.Add(tbNetworkTitle.Text);
            myNet.NetURL.Add(tbVidURL.Text);

            List<string> CaptionItemList = CaptionOptionList();
            int index = cbCaptions.SelectedIndex;
            if (index > 0)
                myNet.OptionShowLabels = NB.TryParseEnum<CaptionType>(CaptionItemList[index], CaptionType.none);

            //myNet.OptionShowLabels = cbDisplayTitles.Checked;
            myNet.VLANsEnabled = cbVLANs.Checked;
            myNet.VLANPacketColors = cb_ColoredPackets.Checked;
            myNet.ShowLabelsHere = myNet.OptionShowLabels;
            int.TryParse(tbNetworkX.Text, out myNet.myWidth);
            int.TryParse(tbNetworkY.Text, out myNet.myHeight);
            myNet.InvalidateEverything();
            int.TryParse(tbLevel.Text, out myNet.Level);
            double.TryParse(tbSortOrder.Text, out myNet.SortOrder);
            myNet.StartingHelpLevel = NB.ParseEnum<NetTestVerbosity>(cbStartingHelpLevel.SelectedItem.ToString());
        }

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

        private void lbTests_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //if left-click.
            //if on an item, edit it
            //if not on an item, make a new one and edit it
            NetTest NT;
            SaveValuesToNetwork();
            if(lbTests.SelectedIndex != -1)
            {
                //Something is selected. Edit it
                NT = myNet.NetTests[lbTests.SelectedIndex];
                NT.Edit();
            }
            else
            {
                //nothing is selected, create new one
                NT = new NetTest("", "", NetTestType.NeedsDefaultGW);
                if(NT.Edit())
                {
                    //Returns true.  Something was changed.  Save it.
                    myNet.NetTests.Add(NT);
                }
            }
            LoadValuesFromNetwork();
        }

        private void lbTests_Edit_Click(object sender, EventArgs e)
        {
            lbTests_MouseDoubleClick(sender, null);
        }

        private void lbTests_Add_Click(object sender, EventArgs e)
        {
            lbTests.SelectedIndex = -1;
            lbTests_MouseDoubleClick(sender, null);
        }

        private void lbTests_Delete_Click(object sender, EventArgs e)
        {
            if (lbTests.SelectedIndex >= lbTests.Items.Count) return;
            if (lbTests.SelectedIndex < 0 ) return;
            SaveValuesToNetwork();
            myNet.NetTests.RemoveAt(lbTests.SelectedIndex);
            LoadValuesFromNetwork();
        }

        private void lbTests_MouseClick(object sender, MouseEventArgs e)
        {
            //see if it is right-click
            //  Add, delete, edit
            int index = 0;
            if(e.Button == MouseButtons.Right)
            {
                if (lbTests.ContextMenuStrip == null)
                {
                    lbTests.ContextMenuStrip = new ContextMenuStrip();
                }
                lbTests.ContextMenuStrip.Items.Clear();
                lbTests.ContextMenuStrip.Items.Add(NB.Translate("_Add"));
                lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Add_Click;
                lbTests.ContextMenuStrip.Items.Add(NB.Translate("_Edit"));
                lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Edit_Click;
                lbTests.ContextMenuStrip.Items.Add(NB.Translate("_Delete"));
                lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Delete_Click;
            }
        }

        private void lbTests_MouseDown(object sender, MouseEventArgs e)
        {
            //see if it is right-click
            //  Add, delete, edit
            int index = 0;
            if (e.Button == MouseButtons.Right)
            {
                if (lbTests.ContextMenuStrip == null)
                {
                    lbTests.ContextMenuStrip = new ContextMenuStrip();
                }
                lbTests.ContextMenuStrip.Items.Clear();
                lbTests.ContextMenuStrip.Items.Add(NB.Translate("_Add"));
                lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Add_Click;
                lbTests.ContextMenuStrip.Items.Add(NB.Translate("_Edit"));
                lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Edit_Click;
                lbTests.ContextMenuStrip.Items.Add(NB.Translate("_Delete"));
                lbTests.ContextMenuStrip.Items[index++].Click += lbTests_Delete_Click;
            }
        }


        /*******************************************************/
        /*******  STUFF FOR TAGS *******************************/
        private void lbTags_Edit_Click(object sender, EventArgs e)
        {
            lbTags_MouseDoubleClick(sender, null);
        }
        private void lbTags_Add_Click(object sender, EventArgs e)
        {
            lbTags.ClearSelected();
            lbTags_MouseDoubleClick(sender, null);
        }
        private void lbTags_Read_Click(object sender, EventArgs e)
        {
            //We need to add this.
            HelpTopics HT = HelpTopics.None;
            if (lbTags.SelectedIndex >= lbTags.Items.Count) return;
            if (lbTags.SelectedIndex < 0) return;
            HT = NB.TryParseEnum(lbTags.SelectedItem.ToString(), HelpTopics.None);
            NB.ReadContextHelp(HT);
        }

        private void lbTags_Delete_Click(object sender, EventArgs e)
        {
            if (lbTags.SelectedIndex >= lbTags.Items.Count) return;
            if (lbTags.SelectedIndex < 0) return;
            SaveValuesToNetwork();
            myNet.SuggestedReadings.RemoveAt(lbTags.SelectedIndex);
            LoadValuesFromNetwork();
        }

        private void lbTags_MouseDown(object sender, MouseEventArgs e)
        {
            //see if it is right-click
            //  Add, delete, edit
            int index = 0;
            if (e.Button == MouseButtons.Right)
            {
                if (lbTags.ContextMenuStrip == null)
                {
                    lbTags.ContextMenuStrip = new ContextMenuStrip();
                }
                lbTags.ContextMenuStrip.Items.Clear();
                lbTags.ContextMenuStrip.Items.Add(NB.Translate("_Add"));
                lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Add_Click;
                if (lbTags.SelectedIndex != -1)
                {
                    lbTags.ContextMenuStrip.Items.Add(NB.Translate("_Edit"));
                    lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Edit_Click;
                    lbTags.ContextMenuStrip.Items.Add(NB.Translate("_Read"));
                    lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Read_Click;
                    lbTags.ContextMenuStrip.Items.Add(NB.Translate("_Delete"));
                    lbTags.ContextMenuStrip.Items[index++].Click += lbTags_Delete_Click;
                }
            }

        }

        private string QuickPrompt(string title, string prompt, string value)
        {
            
            QuestionForm.Text = title;
            Label lbPrompt = new Label();
            lbPrompt.Text = prompt;
            lbPrompt.Location = new Point(1, 1);
            lbPrompt.Size = new Size(200, lbPrompt.Size.Height);

            ListBox Choice = new ListBox();
            List<string> tlist = new List<string>();
            foreach(HelpTopics HT in Enum.GetValues(typeof(HelpTopics)))
            {
                tlist.Add(HT.ToString());
            }
            tlist.Remove("None");
            tlist.Sort();
            foreach(string tstring in tlist)
            {
                Choice.Items.Add(tstring);
            }

            if(value != "" && Choice.Items.Contains(value))
            {
                Choice.SelectedItem = value;
            }
            Choice.Location = new Point(1, lbPrompt.Location.Y + lbPrompt.Height + 5);

            Button Done = new Button();
            Done.Click += btnClose_Click;
            Choice.DoubleClick += btnClose_Click;
            Done.Text = NB.Translate("_Done");
            Done.Location = new Point(1, Choice.Location.Y + Choice.Height + 5);
            QuestionForm.Controls.Clear();
            QuestionForm.Controls.Add(Choice);
            QuestionForm.Controls.Add(Done);
            QuestionForm.Controls.Add(lbPrompt);
            QuestionForm.AcceptButton = Done;
            QuestionForm.FormBorderStyle = FormBorderStyle.FixedDialog;
            QuestionForm.AutoSize = true;
            QuestionForm.Height = Done.Location.Y + Done.Height + 5; //This is too small for the form, it autosizes to "big enough"
            QuestionForm.Width = Choice.Location.X + Choice.Width + 5;
            QuestionForm.ShowDialog();
            return Choice.Text;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (QuestionForm != null)
                QuestionForm.Close();
        }


        private void lbTags_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //if left-click.
            //if on an item, edit it
            //if not on an item, make a new one and edit it
            SaveValuesToNetwork();
            string newstring;
            if (lbTags.SelectedIndex != -1)
            {
                //Something is selected. Edit it
                newstring = QuickPrompt(NB.Translate("OW_EdTag"), NB.Translate("OW_Tag") + ":", myNet.SuggestedReadings[lbTags.SelectedIndex].ToString());
                if (newstring != "")
                {
                    HelpTopics tHelpTopic = NB.TryParseEnum<HelpTopics>(newstring, HelpTopics.None);
                    if (tHelpTopic != HelpTopics.None)
                        myNet.SuggestedReadings[lbTags.SelectedIndex] = tHelpTopic;
                }
                else
                {
                    myNet.SuggestedReadings.RemoveAt(lbTags.SelectedIndex);
                }
            }
            else
            {
                //nothing is selected, create new one
                newstring = QuickPrompt(NB.Translate("OW_EdTag"), NB.Translate("OW_Tag") + ":", "");
                if (newstring != "")
                {
                    HelpTopics tHelpTopic = NB.TryParseEnum<HelpTopics>(newstring, HelpTopics.None);
                    if(tHelpTopic != HelpTopics.None)
                        myNet.SuggestedReadings.Add(tHelpTopic);
                }
            }
            LoadValuesFromNetwork();

        }

        private void tbSortOrder_Validating(object sender, CancelEventArgs e)
        {
            double value;
            double.TryParse(tbSortOrder.Text, out value);
            tbSortOrder.Text = value.ToString("00.000");
        }

        private void tbLevel_Validating(object sender, CancelEventArgs e)
        {
            int value;
            int.TryParse(tbLevel.Text, out value);
            tbLevel.Text = value.ToString();
        }

        private void cbVLANs_CheckedChanged(object sender, EventArgs e)
        {
            if(cbVLANs.Checked)
            {
                cb_ColoredPackets.Visible = true;
            }
            else
            {
                cb_ColoredPackets.Visible = false;
                cb_ColoredPackets.Checked = false;
            }
        }

        private void OptionsWindow_Shown(object sender, EventArgs e)
        {
            BuilderWindow BW = NB.GetBuilderWin();
            if (BW != null)
            {
                Point tLocation = new Point(BW.Location.X + ((BW.Width - Width) / 2), BW.Location.Y + ((BW.Height - Height) / 2));
                Location = tLocation;
            }
        }

        private void tbMessage_MouseDown(object sender, MouseEventArgs e)
        {
            if(ModifierKeys == Keys.Control && e.Button == MouseButtons.Left)
            {
                //ctrl-click -- open translators window
                TranslationWindow TW = new TranslationWindow(myNet);
                TW.ShowDialog();
                LoadValuesFromNetwork();
            }
        }
    }
}
