using Otter;
using PenniesAreGold.objects;
using PenniesAreGold.Utility;
using System;

namespace PenniesAreGold.ui
{
    class MineButton : Entity
    {
        private Spritemap<string> _spriteMap;
        private Rectangle rect;
        public static bool IsMiningBuildingOnMouse = false;
        private float _oX = 0;
        private float _oY = 0;
        private float miningTimer = 0;
        private float opacityTimer = 50;
        public float miningTimerStart = 2;
        private bool one = true;

        public MineButton(float x, float y)
        {
            X = x; Y = y;
            _oX = x; _oY = y;
            SetHitbox(50, 50, (int)Global.Type.MININGBUTTON);
            rect = new Rectangle((int)X, (int)Y, 50, 50);
            _spriteMap = new Spritemap<string>(Assets.MININGBUTTON, 45, 50);
            _spriteMap.Add("main", new int[] { 0 }, new float[] { 10f, 10f });
            _spriteMap.Add("hover", new int[] { 1 }, new float[] { 10f, 10f });
            _spriteMap.Add("ready", new int[] { 2 }, new float[] { 10f, 10f });
            _spriteMap.Add("readyHover", new int[] { 3 }, new float[] { 10f, 10f });
            _spriteMap.Play("main");
            Graphic = _spriteMap;
            _spriteMap.OutlineColor = Color.Black;
            Layer = -10;
        }

        public override void Update()
        {
            base.Update();
            X = Scene.CameraX+_oX;
            Y = Scene.CameraY+_oY;
            rect.X = (int)X;
            rect.Y = (int)Y;
            
            var mouseLocation = new Vector2((int)Scene.MouseX, (int)Scene.MouseY);
            if (Util.InRect(mouseLocation, rect))
            {
                if (miningTimer <= 0)
                {
                    if (one)
                    {
                        one = false;
                        SoundManager.PlayRequestedSound("_buzzReady");
                    }
                    _spriteMap.Play("readyHover");
                }
                else
                    _spriteMap.Play("hover");
                //_spriteMap.Color = Color.Gold;
                if (Global.PlayerSession.Controller.Button("LeftMouseClick").Pressed && miningTimer <= 0 && !IsMiningBuildingOnMouse
                    && !TroopButton.IsTroopBuildingOnMouse)
                {
                    SoundManager.PlayRequestedSound("_click2");
                    miningTimerStart *= 2;
                    miningTimer = miningTimerStart;
                    one = true;
                    opacityTimer = 50;
                    IsMiningBuildingOnMouse = true;
                    Global.Score += (long)miningTimerStart;
                    Scene.Add(new MiningFacility(X, Y));
                }
            }
            else if (miningTimer <= 0)
            {
                if (one)
                {
                    one = false;
                    SoundManager.PlayRequestedSound("_buzzReady");
                }
                _spriteMap.Play("ready");
            }
            else
                _spriteMap.Play("main");
            //_spriteMap.Color = Color.White;
            if (miningTimer > 0) {
                miningTimer--;
                opacityTimer++;
                if (miningTimer != 0)
                    _spriteMap.Color = Color.FromHSV(360,0,1, opacityTimer / miningTimerStart);
            }

                
        }

        public override void Render()
        {
            base.Render();
            //Collider.Render(Color.Black);
        }
    }
}