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

namespace Administration
{
    public partial class BanPlayerForm : Form
    {
        private int banDays;
        private int banLevel;
        private ulong playerID;
        private string reason;

        public BanPlayerForm()
        {
            InitializeComponent();
        }

        private void banDaysBox_TextChanged(object sender, EventArgs e)
        {
        }

        private bool validateForm()
        {
            bool isFormValid = true;

            if (!int.TryParse(banDaysBox.Text, out banDays) || banDays <= 0)
            {
                SetBanDaysError("Počet dní musí být přirozené číslo");
                isFormValid = false;
            }
            else
            {
                ResetBanDaysError();
            }

            if (!int.TryParse(banLevelBox.Text, out banLevel) || banLevel <= 0)
            {
                SetBanLevelError("Úroveň banu musí být přirozené číslo");
                isFormValid = false;
            }
            else
            {
                ResetBanLevelError();
            }

            if (isFormValid)
            {
                return true;
            }
            else
            {
                MessageBox.Show("Formulář obsahuje chyby. Opravte, prosím, veškeré chyby ve formuláři a pak formulář odešlete znovu.", "Chyba ve formuláři");
                return false;
            }
        }

        private void SetBanDaysError(string errorMessage)
        {
            banDaysBox.BackColor = Color.Pink;
            banDaysErrorLabel.Text = errorMessage;
        }

        private void SetBanLevelError(string errorMessage)
        {
            banLevelBox.BackColor = Color.Pink;
            banLevelErrorLabel.Text = errorMessage;
        }

        private void ResetBanDaysError()
        {
            banDaysBox.BackColor = Color.White;
            banDaysErrorLabel.Text = "";
        }

        private void ResetBanLevelError()
        {
            banLevelBox.BackColor = Color.White;
            banLevelErrorLabel.Text = "";
        }

        private void confirmBanButton_Click(object sender, EventArgs e)
        {
            if (validateForm())
            {
                banDays = int.Parse(this.banDaysBox.Text);
                banLevel = int.Parse(this.banLevelBox.Text);
                reason = this.reasonMessageBox.Text;

                BanPlayerRequest request = new BanPlayerRequest{player_id = this.playerID, ban_duration_in_days = this.banDays, ban_level = this.banLevel, ban_reason = this.reason, player_has_ban = true};
                ClientCommunicator.Instance.SendMessage(request);
                Hide();
            }
        }

        public void SetState(ComplexPlayer player, PlayerBanInformationAnswer banInformation)
        {
            ResetBanDaysError();
            ResetBanLevelError();

            this.playerID = player.ID;

            banPlayerValueLabel.Text = player.Name + " (ID: " + player.ID.ToString() + ")";

            this.reasonMessageBox.Text = banInformation.ban_reason;
            this.banDaysBox.Text = banInformation.ban_duration_in_days.ToString();
            this.banLevelBox.Text = banInformation.ban_level.ToString();

            if (banInformation.player_has_ban)
            {
                removeBanButton.Enabled = true;
            }
            else
            {
                removeBanButton.Enabled = false;
            }
        }

        private void removeBanButton_Click(object sender, EventArgs e)
        {
            BanPlayerRequest request = new BanPlayerRequest { player_id = this.playerID, player_has_ban = false };
            ClientCommunicator.Instance.SendMessage(request);
            Hide();
        }
    }
}
