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 PlayerForm : Form
    {
        private BanPlayerForm banPlayerForm;
        private ComplexPlayer viewedPlayer;
        private bool hasBan;

        public PlayerForm()
        {
            InitializeComponent();
        }

        private void playerDetailsGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void shipsTable_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex < shipsTable.RowCount)
            {
                SimpleFortress ship = (SimpleFortress)shipsTable.Rows[e.RowIndex].DataBoundItem;

                if (ship != null)
                {
                    ShipInformationRequest request = new ShipInformationRequest { fortress_id = ship.ID };
                    ClientCommunicator.Instance.SendMessage(request);
                }
            }
        }

        private void ownerGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void PlayerForm_Load(object sender, EventArgs e)
        {

        }

        public void SetState(PlayerInformationAnswer answer)
        {
            ComplexPlayer complexPlayer = new ComplexPlayer(answer.player.player_id, answer.player.player_login, answer.player.player_name);

            foreach (ShipInformation shipInfo in answer.ship_information)
            {
                complexPlayer.Ships.Add(new SimpleFortress(shipInfo.fortress_id, shipInfo.fortress_name));
            }

            foreach (ShipInformation baseInfo in answer.base_information)
            {
                complexPlayer.Bases.Add(new SimpleFortress(baseInfo.fortress_id, baseInfo.fortress_name));
            }

            viewedPlayer = complexPlayer;

            idBox.Text = answer.player.player_id.ToString();
            nameBox.Text = answer.player.player_name.ToString();
            mailBox.Text = "?";
            loginBox.Text = answer.player.player_login.ToString();

            shipsTable.DataSource = complexPlayer.Ships;
            basesTable.DataSource = complexPlayer.Bases;

            shipsTable.Refresh();
            basesTable.Refresh();

            hasBan = answer.player.player_has_ban;

            UpdateBanInformation(hasBan);
        }

        private void UpdateBanInformation(bool hasBan)
        {
            if (hasBan)
            {
                banButton.Text = "Upravit ban";
                hasBanLabel.Text = "Má ban";
                hasBanLabel.ForeColor = Color.Red;
            }
            else
            {
                banButton.Text = "Udělit ban";
                hasBanLabel.Text = "Nemá ban";
                hasBanLabel.ForeColor = Color.Green;
            }
        }

        public void SetBanResult(BanPlayerAnswer answer)
        {
            UpdateBanInformation(answer.player_has_ban);

            switch (answer.ban_result)
            {
                case Constants.REQUEST_ADMIN_BAN_PLAYER_RESULT_ADDED:
                    MessageBox.Show("Hráč dostal ban");
                    break;
                case Constants.REQUEST_ADMIN_BAN_PLAYER_RESULT_UPDATED:
                    MessageBox.Show("Hráčův ban byl upraven");
                    break;
                case Constants.REQUEST_ADMIN_BAN_PLAYER_RESULT_CANCELED:
                    MessageBox.Show("Hráčův ban byl odstraněn");
                    break;
            }
        }

        private void banItem_Click(object sender, EventArgs e)
        {
            var request = new PlayerBanInformationRequest{player_id = viewedPlayer.ID};
            ClientCommunicator.Instance.SendMessage(request);
        }

        private void generatePasswordItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Hráči bude vygenerováno nové heslo a zasláno na mail. Chcete opravdu nové heslo vygenerovat?", "Žádost o potvrzení akce", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                GeneratePasswordRequest request = new GeneratePasswordRequest(viewedPlayer.ID);
                //Logic.Communicator.SendMessage(request);
            }
        }

        private void basesTable_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex < basesTable.RowCount)
            {
                SimpleFortress ship = (SimpleFortress)basesTable.Rows[e.RowIndex].DataBoundItem;

                if (ship != null)
                {
                    BaseInformationRequest request = new BaseInformationRequest { fortress_id = ship.ID };
                    ClientCommunicator.Instance.SendMessage(request);
                }
            }
        }

        internal void setBanState(PlayerBanInformationAnswer answer)
        {
            if (banPlayerForm == null || banPlayerForm.IsDisposed)
            {
                banPlayerForm = new BanPlayerForm();
            }       

            banPlayerForm.SetState(viewedPlayer, answer);
            banPlayerForm.ShowDialog();
        }
    }
}
