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

namespace SpriteEditor_2017
{
    /// <summary>
    /// provides an interface to insert multiple frames between two existing frame to generate a smooth transition from start to finish
    /// </summary>
    public class panelAnimationSpeed : panelMoveable
    {
        formSpriteEditor_2017 frmSpriteEditor = null;
        
        public Ck_Objects.textBox_NumericInput_Integer txtInput = new textBox_NumericInput_Integer();
        public Ck_Objects.classLabelButton lbtnGo = new classLabelButton();
        public Ck_Objects.classLabelButton lbtnCancel = new classLabelButton();


        public panelAnimationSpeed() { Init(panelMoveable.fntStandard); }
        public panelAnimationSpeed(Font fnt) { Init(fnt); }
        /// <summary>
        /// initializes the panel's controls
        /// </summary>
        /// <param name="fnt">font used by the panel's header</param>
        void Init(Font fnt)
        {
            BorderStyle = BorderStyle.Fixed3D;
            Heading.Font = fnt;
            Heading.BackColor = Color.LawnGreen;
            if (frmSpriteEditor == null) frmSpriteEditor = formSpriteEditor_2017.instance;

            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            ///                     add controls to panel
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            pnlContainer.Controls.Add(txtInput);
            txtInput.Dock = DockStyle.Fill;
            txtInput.Multiline = true;
            txtInput.KeyDown += TxtInput_KeyDown;

            txtInput.Controls.Add(lbtnGo);
            lbtnGo.Text = "Go";
            lbtnGo.AutoSize = true;
            lbtnGo.Click += lbtnGo_Click;

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

            init_Format();

            SizeChanged += panelAnimationSpeed_SizeChanged;
            LocationChanged += panelAnimationSpeed_LocationChanged;

            placeObjects();
            setToolTip();
        }


        public void init_Format()
        {    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            ///                                         set panel's formatting variables
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].bolIgnoreChanges = true;
            Size = new Size(formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].size_Width,
                               formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].size_Height);
            Location = new Point(formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].location_X,
                                 formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].location_Y);
            if (formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].Collapsed) Collapse();
            else Expand();
            ExpansionDirection = formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].ExpansionDirection;
            formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].bolIgnoreChanges = false;
        }

        /// <summary>
        /// binds the panel's controls to formSpriteEditor_2017's tooltip
        /// </summary>
        void setToolTip()
        {
            ToolTip tt = formSpriteEditor_2017.instance.toolTip;

            tt.SetToolTip(lbtnCancel, formSpriteEditor_2017.conDefaultToolTip);
            tt.SetToolTip(lbtnGo, formSpriteEditor_2017.conDefaultToolTip);
            tt.SetToolTip(txtInput, formSpriteEditor_2017.conDefaultToolTip);
        }

        /// <summary>
        /// records panel's location after a location change for panel formatting purposes
        /// </summary>
        private void panelAnimationSpeed_LocationChanged(object sender, EventArgs e)
        {
            if (!formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].bolIgnoreChanges)
            {
                formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].location_X = Location.X;
                formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].location_Y = Location.Y;
                formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].bolInit = true;
            }
        }

        /// <summary>
        /// records panel's size after a size change for panel formatting purposes
        /// </summary>
        private void panelAnimationSpeed_SizeChanged(object sender, EventArgs e)
        {
            if (!formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].bolIgnoreChanges
                && !Collapsed)
            {
                formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].bolInit = true;
                formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].size_Width = Size.Width;
                formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].size_Height = Size.Height;
            }
            formSpriteEditor_2017.cPanelSizeLocations[(int)formSpriteEditor_2017.enuPanels.AutoInsertIntermediaryFrames].Collapsed = Collapsed;

            placeObjects();
        }

        /// <summary>
        /// positions the panel's controls onto the panel after at initialization or sizechange
        /// </summary>
        void placeObjects()
        {
            Size szForm = new Size(5, 5);
            lbtnGo.Left = txtInput.Width - lbtnGo.Width - szForm.Width ;
            lbtnGo.Top = txtInput.Height - lbtnGo.Height-szForm.Height;

            lbtnCancel.Left = lbtnGo.Left - lbtnCancel.Width;
            lbtnCancel.Top = lbtnGo.Top;
        }

        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    {
                        Cancel();
                    }
                    break;

                case Keys.Enter:
                    {
                        Ok();
                    }
                    break;
            }
        }

        /// <summary>
        /// cancels operation and hides the panel
        /// </summary>
        private void lbtnCancel_Click(object sender, EventArgs e) { Cancel(); }
        private void lbtnGo_Click(object sender, EventArgs e) { Ok(); }

        void Cancel()
        {
            Hide();
        }

        void Ok()
        {
            bool bolAnimationEnabled = formSpriteEditor_2017.instance.tmrAnimation.Enabled;
            formSpriteEditor_2017.instance.tmrAnimation.Enabled = false;
            classSpriteData cSpriteData = formSpriteEditor_2017.instance.cSpriteData;

            double dblNumFrames = (double)txtInput.Value;
            if (dblNumFrames < 0)
            {
                for (int intAnimationCounter = 0; intAnimationCounter < cSpriteData.cAnimationData.cSprite.lstAnimations.Count; intAnimationCounter++)
                {
                    cSpriteData.cAnimationData.cAnimation = cSpriteData.cAnimationData.cSprite.lstAnimations[intAnimationCounter];

                    int intFrameCounter = 1;
                    while (intFrameCounter < cSpriteData.cAnimationData.cAnimation.lstFrames.Count)
                    {
                        cSpriteData.cAnimationData.cAnimation.lstFrames.RemoveAt(intFrameCounter);
                        intFrameCounter++;
                    }
                }
            }
            else if (dblNumFrames >0)
            {
                for (int intAnimationCounter = 0; intAnimationCounter < cSpriteData.cAnimationData.cSprite.lstAnimations.Count; intAnimationCounter++)
                {
                    cSpriteData.cAnimationData.cAnimation = cSpriteData.cAnimationData.cSprite.lstAnimations[intAnimationCounter];

                    for (int intFrameCounter = cSpriteData.cAnimationData.cAnimation.lstFrames.Count - 1; intFrameCounter >= 0; intFrameCounter--)
                    {
                        cSpriteData.cAnimationData.intFrameIndex = intFrameCounter;
                        formSpriteEditor_2017.instance.AutoInsertIntermediaryFrames(intFrameCounter, dblNumFrames);
                    }
                }
            }
            formSpriteEditor_2017.instance.nudFrameCounter_Reset();
            formSpriteEditor_2017.instance.tmrAnimation.Enabled = bolAnimationEnabled;
        }

    

    }

}
