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 MessageForm : Form
    {
        private ulong playerID;

        public MessageForm()
        {
            InitializeComponent();
        }

        public void SetupFromPlayer(ComplexPlayer player)
        {
            this.playerID = player.ID;
            this.receiverValueLabel.Text = player.Name;

            ResetMessageError();
            ResetSubjectError();
        }

        private bool validateForm()
        {
            bool isFormValid = true;

            string subject = subjectBox.Text;
            string message = messageBodyBox.Text;            

            if (subject == string.Empty)
            {
                SetSubjectError("Předmět zprávy nesmí být prázdný");
                isFormValid = false;
            }
            else
            {
                ResetSubjectError();
            }

            if (message == string.Empty)
            {
                SetMessageError("Tělo zprávy nesmí být prázdné");
                isFormValid = false;
            }
            else
            {
                ResetMessageError();
            }

            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 SetMessageError(string message)
        {
            messageBodyBox.BackColor = Color.Pink;
            messageErrorLabel.Text = message;
            messageErrorLabel.ForeColor = Color.Red;
        }

        private void SetMessageCorrect(string message)
        {
            messageBodyBox.BackColor = Color.White;
            messageErrorLabel.Text = message;
            messageErrorLabel.ForeColor = Color.Green;
        }

        private void SetSubjectError(string message)
        {
            subjectBox.BackColor = Color.Pink;
            subjectErrorLabel.Text = message;
            subjectErrorLabel.ForeColor = Color.Red;
        }

        private void SetSubjectCorrect(string message)
        {
            subjectBox.BackColor = Color.White;
            subjectErrorLabel.Text = message;
            subjectErrorLabel.ForeColor = Color.Green;
        }

        private void ResetMessageError()
        {
            SetMessageCorrect(string.Empty);
        }

        private void ResetSubjectError()
        {
            SetSubjectCorrect(string.Empty);
        }

        private void confirmMessageButton_Click(object sender, EventArgs e)
        {
            if (validateForm())
            {
                string message = messageBodyBox.Text;
                string subject = subjectBox.Text;

                MessageTargets target;

                if (sendToGameRadio.Checked)
                {
                    target = MessageTargets.GAME;
                }
                else
                {
                    target = MessageTargets.MAIL;
                }

                SendMessageRequest request = new SendMessageRequest(playerID, subject, message, target);
                //Logic.Communicator.SendMessage(request);
                Close();
            }
        }

        private void subjectBox_KeyPress(object sender, KeyPressEventArgs e)
        {
        }
    }
}
