using Otter;
using PenniesAreGold.Utility;
using System;

namespace PenniesAreGold.utility
{
    class NoSound : Entity
    {
        private Spritemap<string> _spriteMap;
        private bool isOn = true;
        private Rectangle rect;
        public static Music music = new Music(Assets.MUSIC_ATTEMPT);
        private int _pX = 0;
        private int _pY = 0;

        public NoSound(float x, float y)
        {
            X = x; Y = y;
            _pX = (int)x;
            _pY = (int)y;
            //SetHitbox(20, 20, (int)Global.Type.NOSOUND);
            rect = new Rectangle((int)X, (int)Y, 17, 49);
            _spriteMap = new Spritemap<string>(Assets.NOSOUND, 17, 49);
            _spriteMap.Add("on", new int[] { 1 }, new float[] { 10f, 10f });
            _spriteMap.Add("off", new int[] { 0 }, new float[] { 10f, 10f });
            _spriteMap.Play("on");
            Graphic = _spriteMap;
            music.Play();
            music.Volume = .3f;
            music.Loop = true;
            Layer = -10;
        }

        public override void Update()
        {
            base.Update();
            X = Scene.CameraX + _pX;
            Y = Scene.CameraY + _pY;
            rect.X = (int)X;
            rect.Y = (int)Y;

            var mouseLocation = new Vector2((int)Scene.MouseX, (int)Scene.MouseY);
            if (Util.InRect(mouseLocation, rect))
            {
                if (Global.PlayerSession.Controller.Button("LeftMouseClick").Pressed)
                    TriggerSoundChange();
            }

            if (Global.PlayerSession.Controller.Button("M").Pressed)
                TriggerSoundChange();
        }

        public void TriggerSoundChange()
        {
            
            if (isOn)
            {
                ChangeAnimTo("off");
                isOn = false;
                SoundManager.PlayRequestedSound("_click");
                SoundManager.isSoundEnabled = false;
                music.Pause();
            }
            else
            {
                ChangeAnimTo("on");
                isOn = true;
                music.Play();//todo
                SoundManager.isSoundEnabled = true;
                SoundManager.PlayRequestedSound("_click");
            }
        }

        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }

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

        public void Destroy()
        {
            RemoveSelf();
        }
    }
}

