using Ck_Objects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpriteEditor_2017
{
    public class formLimb_New_Text : Form
    {
        static classLimbTextInfo cLimbTextInfo = new classLimbTextInfo();

        public classLimbTextInfo LimbInfo { get { return cLimbTextInfo; } }

        Label lblHeading = new Label();
        Label lblText = new Label();
        TextBox txtText = new TextBox();
        CheckBox chkMultipleLimbs = new CheckBox();
        classLabelButton lbtnFont = new classLabelButton();
        classLabelButton lbtnColor_Foreground = new classLabelButton();
        classLabelButton lbtnColor_Transparent = new classLabelButton();
        Label lblFontSize = new Label();
        Ck_Objects.textBox_NumericInput_Double txtFontSize = new textBox_NumericInput_Double();
        classLabelButton lbtnOk = new classLabelButton();
        classLabelButton lbtnCancel = new classLabelButton();

        public formLimb_New_Text()
        {
            FormBorderStyle = FormBorderStyle.None;
            Hide();

            BackColor = Color.LightGreen;
            ForeColor = Color.Black;

            Controls.Add(lblHeading);
            lblHeading.Text = "Define new Text limb";
            lblHeading.AutoSize = true;
            lblHeading.Font = new Font("ms sans-serif", 12);

            Controls.Add(lblText);
            lblText.AutoSize = true;
            lblText.Text = "Text";

            Controls.Add(chkMultipleLimbs);
            chkMultipleLimbs.AutoSize = true;
            chkMultipleLimbs.Text = "Multiple Limbs";
            chkMultipleLimbs.Checked = true;
            chkMultipleLimbs.CheckedChanged += ChkMultipleLimbs_CheckedChanged;

            Controls.Add(txtText);
            txtText.Text = "Text";
            txtText.FontChanged += TxtText_FontChanged;
            txtText.KeyDown += TxtText_KeyDown;

            Controls.Add(lbtnFont);
            lbtnFont.AutoSize = true;
            lbtnFont.Text = "Font";
            lbtnFont.Click += LbtnFont_Click;

            Controls.Add(lbtnColor_Foreground);
            lbtnColor_Foreground.Text = "Color Foreground";
            lbtnColor_Foreground.Click += LbtnColor_Foreground_Click;

            Controls.Add(lbtnColor_Transparent);
            lbtnColor_Transparent.Text = "Color Transparent";
            lbtnColor_Transparent.Click += LbtnColor_Transparent_Click;

            Controls.Add(lblFontSize);
            lblFontSize.AutoSize = true;
            lblFontSize.Text = "Font Size";

            Controls.Add(txtFontSize);
            txtFontSize.Width = 40;
            txtFontSize.Min = 5;
            txtFontSize.Max = 100;
            txtFontSize.Value = 8;
            txtFontSize._eventValueChanged = txtFontSize_ValueChanged;

            Controls.Add(lbtnOk);
            lbtnOk.AutoSize = true;
            lbtnOk.Text = "Ok";
            lbtnOk.Click += LbtnOk_Click;

            Controls.Add(lbtnCancel);
            lbtnCancel.AutoSize = true;
            lbtnCancel.Text = "Cancel";
            lbtnCancel.Click += LbtnCancel_Click;

            Activated += FormLimb_New_Text_Activated;
        }

        private void ChkMultipleLimbs_CheckedChanged(object sender, EventArgs e)
        {
            LimbInfo.MultipleLimbs = chkMultipleLimbs.Checked;
        }

        bool bolActivated = false;
        private void FormLimb_New_Text_Activated(object sender, EventArgs e)
        {
            if (bolActivated) return;
            bolActivated = true;
            placeObjects();
            txtText.Focus();
        }

        void Ok()
        {
            DialogResult = DialogResult.OK;
            LimbInfo.Text = txtText.Text;
            Close();
        }

        void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        void placeObjects()
        {
            int intGap = 3;
            Width = 500;

            lblHeading.Location = new Point(intGap, intGap);
            lblText.Location = new Point(lblHeading.Left, lblHeading.Bottom);
            txtText.Location = new Point(lblText.Right, lblText.Top);
            txtText.Font = LimbInfo.Font;
            txtText.ForeColor = LimbInfo.Color_Fore;
            txtText.BackColor = Color.FromArgb((txtText.ForeColor.R + 128) % 256, (txtText.ForeColor.G + 128) % 256, (txtText.ForeColor.B + 128) % 256);
            txtText.Width = Width - intGap - txtText.Left;
            chkMultipleLimbs.Location = new Point(txtText.Left, txtText.Bottom);

            lbtnFont.Location = new Point(lblText.Left, chkMultipleLimbs.Bottom);
            lbtnColor_Foreground.Location = new Point(lbtnFont.Right, lbtnFont.Top);
            lbtnColor_Transparent.Location = new Point(lbtnColor_Foreground.Right, lbtnColor_Foreground.Top);
            lblFontSize.Location = new Point(lbtnColor_Transparent.Right, lbtnColor_Transparent.Top);
            txtFontSize.Location = new Point(lblFontSize.Right, lblFontSize.Top);
            txtFontSize.Value = LimbInfo.Font.Size;
            lbtnOk.Location = new Point(Width - intGap - lbtnOk.Width, txtFontSize.Bottom);
            lbtnCancel.Location = new Point(lbtnOk.Left - lbtnCancel.Width, lbtnOk.Top);

            Height = lbtnOk.Bottom + intGap;

            Location = new Point((Screen.PrimaryScreen.Bounds.Width - Width) / 2, (Screen.PrimaryScreen.Bounds.Height - Height) / 2);
            BringToFront();
        }

        private void LbtnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void LbtnOk_Click(object sender, EventArgs e)
        {
            Ok();
        }

        private void txtFontSize_ValueChanged(object sender, EventArgs e)
        {
            LimbInfo.FontSize = (float)txtFontSize.Value;
            placeObjects();
        }

        private void txtFontSize_MouseWheel(object sender, MouseEventArgs e)
        {
            placeObjects();
        }

        private void LbtnColor_Transparent_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
            {
                LimbInfo.Color_Transparent = cd.Color;
                placeObjects();
            }
        }

        private void LbtnColor_Foreground_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
            {
                LimbInfo.Color_Fore = cd.Color;
                placeObjects();
            }
        }

        private void LbtnFont_Click(object sender, EventArgs e)
        {
            FontDialog fntDialog = new FontDialog();
            fntDialog.Font = LimbInfo.Font;
            if (fntDialog.ShowDialog() == DialogResult.OK)
            {
                LimbInfo.Font = fntDialog.Font;
                placeObjects();
            }
        }

        private void TxtText_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    {
                        e.SuppressKeyPress = true;
                        Ok();
                    }
                    break;

                case Keys.Escape:
                    {
                        e.SuppressKeyPress = true;
                        Cancel();
                    }
                    break;

                default:
                    {
                    }
                    break;
            }
        }

        private void TxtText_FontChanged(object sender, EventArgs e)
        {
            placeObjects();
        }
    }

    public class classLimbTextInfo
    {
        public Font Font = new Font("ms Sans-serif", 35);
        Color clrColor_Fore= Color.FromArgb(1,1,1);
        public Color Color_Fore
        {
            get { return clrColor_Fore; }
            set
            {
                clrColor_Fore = value;
                // sprite class always treats color(0,0,0) as transparent
                if (clrColor_Fore.R == 0
                    && clrColor_Fore.G == 0
                    && clrColor_Fore.B == 0)
                    clrColor_Fore = Color.FromArgb(1, 1, 1);
            }
        }
        public Color Color_Transparent= Color.Black;
        public string Text = "text";
        public bool MultipleLimbs = true;

        public string FontName
        { 
            get { return Font.Name; }
            set
            {
                Font = new Font(value, Font.Size, Font.Style);
            }
        }

        public float FontSize
        { 
            get { return Font.Size; }
            set { Font = new Font(Font.Name, value, Font.Style); }
        }
    }
}
