using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WTD_Editor
{
    public partial class Upgrades : Form
    {
        Map map;
        Tower tow;

        public Upgrades(Map m, Tower t)
        {
            InitializeComponent();
            map = m;
            tow = t;
            textBox1.Text = tow.name;
            refreshAll();
        }

        private void refreshAll() {
            textBox2.Text = tow.upgrades.Count.ToString();
            List<Tower> list = new List<Tower>(map.towers);
            list.Remove(tow);
            foreach(Tower t in tow.upgrades)
                list.Remove(t);

            if (list.Count > 0)
            {
                if (!comboBox1.Enabled)
                    comboBox1.Enabled = true;
                comboBox1.DataSource = list;
            }
            else
            {
                comboBox1.SelectedIndex = -1;
                comboBox1.Enabled = false;
            }

            listBox1.DataSource = null;
            listBox1.DataSource = tow.upgrades;
        }

        private void Upgrades_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1 && comboBox1.Enabled && tow.upgrades.Count < Map.UPGRADES_MAX)
            {
                Tower t = ((List<Tower>)(comboBox1.DataSource))[comboBox1.SelectedIndex];
                t.usedby++;
                tow.upgrades.Insert(listBox1.SelectedIndex + 1, t);
                int sel = listBox1.SelectedIndex + 1;
                refreshAll();
                listBox1.SelectedIndex = sel;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tow.upgrades.Count > 0 && listBox1.SelectedIndex >= 0)
            {
                Tower t = tow.upgrades[listBox1.SelectedIndex];
                t.usedby--;
                tow.upgrades.RemoveAt(listBox1.SelectedIndex);
                refreshAll();
            }
        }
    }
}