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.IO;

namespace EduNetworkBuilder
{
    public partial class TranslationWindow : Form
    {
        Network myNet = null;
        string[,] LanguageChoices = { { NB.Translate("NB_NBEn"), "en" }, { NB.Translate("NB_NBFr"), "fr" } };

        public TranslationWindow(Network ToEdit)
        {
            InitializeComponent();
            this.KeyPreview = true;
            myNet = ToEdit;

            LocalSetup();
        }

        void LocalSetup()
        {            
            cb1Language.Items.Clear();
            cb2Language.Items.Clear();
            NBSettings mySettings = NB.GetSettings();
            for (int i = 0; i < LanguageChoices.GetLength(0); i++)
            {
                cb1Language.Items.Add(LanguageChoices[i, 0]);
                cb2Language.Items.Add(LanguageChoices[i, 0]);
                if(mySettings.LastTrans1Lang == LanguageChoices[i, 1])
                {
                    cb1Language.SelectedItem = LanguageChoices[i, 0];
                }
                if (mySettings.LastTrans2Lang == LanguageChoices[i, 1])
                {
                    cb2Language.SelectedItem = LanguageChoices[i, 0];
                }
            }
            if (cb1Language.SelectedIndex == -1) cb1Language.SelectedIndex = 0;
            if (cb2Language.SelectedIndex == -1) cb2Language.SelectedIndex = 0;
            Icon = Properties.Resources.NBIco;
            lblStatus.Text = "";
            LanguagifyComponents();
        }

        private void LanguagifyComponents()
        {
            Text = NB.Translate("TW_Translate");
            btnSave.Text = NB.Translate("_Save");
            btnClose.Text = NB.Translate("_Done");
        }

        private void ClosingStuff()
        {
            NBSettings mySettings = NB.GetSettings();
            for (int i = 0; i < LanguageChoices.GetLength(0); i++)
            {
                if (cb1Language.Text == LanguageChoices[i, 0])
                {
                    mySettings.LastTrans1Lang = LanguageChoices[i, 1];
                }
                if (cb2Language.Text == LanguageChoices[i, 0])
                {
                    mySettings.LastTrans2Lang = LanguageChoices[i, 1];
                }
            }
            mySettings.Save();
        }

        string CodeFromLang(string language)
        {
            for (int i = 0; i < LanguageChoices.GetLength(0); i++)
            {
                if (language == LanguageChoices[i, 0]) return LanguageChoices[i, 1];
            }
            return "";
        }

        string LangFromCode(string Code)
        {
            for (int i = 0; i < LanguageChoices.GetLength(0); i++)
            {
                if (Code == LanguageChoices[i, 1]) return LanguageChoices[i, 0];
            }
            return "";
        }

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

        private void Save()
        {
            if (File.Exists(myNet.NetworkFilename))
            {
                myNet.Save();
                //Console.WriteLine("Saved");
                lblStatus.Text = NB.Translate("_Saved");
            }
        }

        private void TranslationWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Modifiers == Keys.Control && e.KeyCode == Keys.S)
            {
                //ctrl-s was pressed
                Save();
            }
        }

        private void TranslationWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClosingStuff();
        }

        private void cb1Language_SelectedIndexChanged(object sender, EventArgs e)
        {
            string What = CodeFromLang(cb1Language.Text);
            if (What != "")
            {
                tb1Message.Text = myNet.NetMessage.GetText(What);
                tb1Title.Text = myNet.NetTitle.GetText(What);
            }
        }

        private void cb2Language_SelectedIndexChanged(object sender, EventArgs e)
        {
            string What = CodeFromLang(cb2Language.Text);
            if (What != "")
            {
                tb2Message.Text = myNet.NetMessage.GetText(What);
                tb2Title.Text = myNet.NetTitle.GetText(What);
            }
        }

        void RefreshContents()
        {
            cb1Language_SelectedIndexChanged(null, EventArgs.Empty);
            cb2Language_SelectedIndexChanged(null, EventArgs.Empty);
            lblStatus.Text = "";
        }

        private void tb1Message_Leave(object sender, EventArgs e)
        {
            string What = CodeFromLang(cb1Language.Text);
            if (What != "")
            {
                myNet.NetMessage.ChangeText(What,tb1Message.Text);
            }
            RefreshContents();
        }

        private void tb2Message_Leave(object sender, EventArgs e)
        {
            string What = CodeFromLang(cb2Language.Text);
            if (What != "")
            {
                myNet.NetMessage.ChangeText(What, tb2Message.Text);
            }
            RefreshContents();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void tb1Title_Leave(object sender, EventArgs e)
        {
            string What = CodeFromLang(cb1Language.Text);
            if (What != "")
            {
                myNet.NetTitle.ChangeText(What, tb1Title.Text);
            }
            RefreshContents();
        }

        private void tb2Title_Leave(object sender, EventArgs e)
        {
            string What = CodeFromLang(cb2Language.Text);
            if (What != "")
            {
                myNet.NetTitle.ChangeText(What, tb2Title.Text);
            }
            RefreshContents();
        }
    }
}
