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

namespace Sentience
{
    public partial class FormTeam : Form
    {
        public FormTeam(int id, Team team, List<IBehavior> behaviors, List<Team> existingTeams)
        {
            InitializeComponent();

            Team = team;
            Behaviors = behaviors;
            ExistingTeams = existingTeams;

            numMembers.Maximum = Defaults.MaxTeamSize;

            if (Team != null)
            {
                UpdateGUI();
                this.Text = "Edit Team";
                cmbBehavior.SelectedIndex = Team.Behavior.ID;
            }
            else
            {
                Random randomNumber = new Random();

                Team = new Team(id,
                    "Team " + id,
                    Color.FromArgb((int)randomNumber.Next(255),
                    (int)randomNumber.Next(255),
                    (int)randomNumber.Next(255)));

                UpdateGUI();
                this.Text = "Add Team";

                if (Behaviors.Count > 0)
                    cmbBehavior.SelectedIndex = 0;
            }
        }

        public void UpdateGUI()
        {
            txtID.Text = Team.ID.ToString();
            txtName.Text = Team.Name;

            numMembers.Value = Team.Members;

            pnlColor.BackColor = Team.Color;

            cmbBehavior.Items.Clear();

            foreach (IBehavior Behavior in Behaviors)
                cmbBehavior.Items.Add(Behavior.Name);
        }

        public List<IBehavior> Behaviors { get; set; }
        public Team Team { get; set; }
        public List<Team> ExistingTeams { get; set; }

        private void btnSetColor_Click(object sender, EventArgs e)
        {
            Color newColor = Utils.GetColor(this);

            if (!newColor.IsEmpty)
                pnlColor.BackColor = newColor;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (Team ExistingTeam in ExistingTeams)
            {
                if (ExistingTeam != Team)
                {
                    if (ExistingTeam.Name == txtName.Text)
                    {
                        MessageBox.Show(FormMain.ActiveForm, "A team with this name already exists.\nTeam names must be unique.", "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        DialogResult = DialogResult.None;
                        return;
                    }
                    if (ExistingTeam.Color == pnlColor.BackColor)
                    {
                        MessageBox.Show(FormMain.ActiveForm, "A team with this color already exists.\nTeam colors must be unique.", "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        DialogResult = DialogResult.None;
                        return;
                    }
                }
            }

            Team.Color = pnlColor.BackColor;
            Team.Name = txtName.Text;
            Team.Members = (int)numMembers.Value;
            Team.Behavior = Behaviors[cmbBehavior.SelectedIndex];
        }
    }
}
