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

namespace PenniesAreGold.ui
{
    class TroopButton : Entity
    {
        private Spritemap<string> _spriteMap;
        private Rectangle rect;
        public static bool IsTroopBuildingOnMouse = false;
        private float _oX = 0;
        private float _oY = 0;
        private float opacity = 1f;
        private bool one = true;

        public TroopButton(float x, float y)
        {
            X = x; Y = y;
            _oX = x; _oY = y;
            SetHitbox(50, 50, (int)Global.Type.TROOPBUTTON);
            rect = new Rectangle((int)X, (int)Y, 50, 50);
            _spriteMap = new Spritemap<string>(Assets.TROOPBUTTON, 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;
            //Console.WriteLine("MiningButtonIsOnMouse: {0}", MineButton.IsMiningBuildingOnMouse);
            //Console.WriteLine("TroopButtonIsOnMouse: {0}", TroopButton.IsTroopBuildingOnMouse);

            var mouseLocation = new Vector2((int)Scene.MouseX, (int)Scene.MouseY);
            if (Util.InRect(mouseLocation, rect))
            {
                if (Global.numberOfCoins >= Global.TroopBuildingCost)
                {
                    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 && Global.numberOfCoins >= Global.TroopBuildingCost && !IsTroopBuildingOnMouse
                    && !MineButton.IsMiningBuildingOnMouse)
                {
                    SoundManager.PlayRequestedSound("_click2");
                    one = true;
                    opacity = 1f;
                    IsTroopBuildingOnMouse = true;
                    Global.numberOfCoins -= Global.TroopBuildingCost;
                    Global.TroopBuildingCost += 1;
                    Global.Score += Global.TroopBuildingCost * 10;
                    Scene.Add(new TroopBuilding(X, Y));
                }
            }
            else if (Global.numberOfCoins >= Global.TroopBuildingCost)
            {
                if (one)
                {
                    one = false;
                    SoundManager.PlayRequestedSound("_buzzReady");
                }
                _spriteMap.Play("ready");
            }
            else
                _spriteMap.Play("main");
            //_spriteMap.Color = Color.White;
            if (Global.numberOfCoins <= Global.TroopBuildingCost)
            {
                if (Global.numberOfCoins == 0)
                    opacity = .1f;
                else if (Global.numberOfCoins <= Global.TroopBuildingCost / 3)
                    opacity = .2f;
                else if (Global.numberOfCoins <= Global.TroopBuildingCost / 2)
                    opacity = .4f;
                else if (Global.numberOfCoins <= Global.TroopBuildingCost / 1.2)
                    opacity = .6f;
                else if (Global.numberOfCoins <= Global.TroopBuildingCost / 1.1)
                    opacity = .8f;
                else if (Global.numberOfCoins >= Global.TroopBuildingCost)
                    opacity = 1f;
                _spriteMap.Color = Color.FromHSV(360, 0, 1, opacity);
            }


        }

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