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 RandomPuzzleChooser : Form
    {
        public bool Canceled = false;
        public string Filename = "";
        public int Difficulty = 5;
        string LastChosenFile = "";
        public bool IsResource = true;
        NBSettings PersonSettings;

        public RandomPuzzleChooser(NBSettings Settings)
        {
            InitializeComponent();
            LanguagifyComponents();
            LocalSetup();
            PersonSettings = Settings;
        }

        private void LanguagifyComponents()
        {
            lblDifficulty.Text = NB.Translate("RPC_Difficulty");
            lblNetwork.Text = NB.Translate("RPC_Network");
            btnCancel.Text = NB.Translate("_Cancel");
            btnOK.Text = NB.Translate("_OK");
        }

        List<string> MapsWeCanUse()
        {
            List<string> MapList = new List<string>();
            foreach (string one in Enum.GetNames(typeof(SolvedNetworkNames)))
            {
                if (one == SolvedNetworkNames.DHCP.ToString()) continue;
                if (one == SolvedNetworkNames.OneNet.ToString()) continue;
                MapList.Add(one);
            }
            return MapList;
        }

        private void LocalSetup()
        {
            lblFilename.Text = "";//It is nothing unless we have chosen a file.  In that case, it is the file we have chosen

            cbDifficulty.Items.Clear();
            for(int i=1; i< 16; i++)
            {
                cbDifficulty.Items.Add(i.ToString());
            }
            cbDifficulty.SelectedIndex=4;

            cbChosenNet.Items.Clear();
            foreach(string one in MapsWeCanUse())
            {
                cbChosenNet.Items.Add(one);
            }
            cbChosenNet.SelectedIndex = 2;
        }

        private void cbChosenNet_Leave(object sender, EventArgs e)
        {
            lblFilename.Text = ""; //If we entered and then left, we are going to use this.
        }

        private void btnUseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog mydialog = new OpenFileDialog();
            mydialog.AddExtension = true;
            //If we have a user loaded, we can open homework files
            string filter = "EduNet File (*.enbx)|*.enbx";
            mydialog.Filter = filter;
            mydialog.Multiselect = false;
            mydialog.ShowHelp = true;
            if (LastChosenFile != null && LastChosenFile != "") mydialog.FileName = LastChosenFile;
            else mydialog.FileName = PersonSettings.LastPath;
            DialogResult result = mydialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel) return;
            string extension = Path.GetExtension(mydialog.FileName).ToString();
            if (extension != ".enbx" && extension != ".enbu" && extension != ".enbh")
            {
                MessageBox.Show(NB.Translate("_LoadErr"));
                return;
            }
            LastChosenFile = mydialog.FileName;
            if (!File.Exists(mydialog.FileName)) return;
            lblFilename.Text = mydialog.FileName;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Canceled = true;
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            IsResource = false;
            int.TryParse(cbDifficulty.Text, out Difficulty);

            if (lblFilename.Text != "") Filename = lblFilename.Text;
            else
            {
                Filename = cbChosenNet.Text;
                IsResource = true;
            }
            Canceled = false;
            Close();
        }

        private void cbChosenNet_Enter(object sender, EventArgs e)
        {
            lblFilename.Text = "";
        }
    }
}
