using System;
using UnityEngine;

public class MusicButton : MonoBehaviour
{
    public SkeletonAnimation SkeletonAnim;
    public Sprite SuccSprite;
    public Sprite FailSprite;
    public Sprite ActiveSprite;
    private Sprite DefaultSprite;
    public GameScript GameObjectScript;
    public String Key;
    private Collider2D droppingTile = null;
    private SpriteRenderer spriteRenderer;
    private float blinkDuration = 0.1f;
    public GameObject goodStuffPS;
    void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        DefaultSprite = spriteRenderer.sprite;
    }
    void Start()
    {
        spriteRenderer.sprite = DefaultSprite;
    }
    void OnTriggerEnter2D(Collider2D col)
    {
        droppingTile = col;
        setActiveColor();
    }

    void OnTriggerStay2D(Collider2D col)
    {
        droppingTile = col;
    }
    void OnTriggerExit2D(Collider2D col)
    {
        droppingTile = null;
        resetColor();
    }
    void Update()
    {
        var track = SkeletonAnim.state.GetCurrent(0);
        if(track == null)
        {
            SkeletonAnim.state.SetAnimation(0, "Zero", false);
        }
        if(Input.GetButtonDown(Key))
        {
            // success
            if(droppingTile != null)
            {
                GameObjectScript.SuccCount++;
                Destroy(droppingTile.gameObject);
                setSuccColor();
                playGoodParticleSystem();
                Invoke("resetColor", blinkDuration);
                SkeletonAnim.Reset();
                SkeletonAnim.state.SetAnimation(0, Key, false);
                GameObjectScript.PlaySound(Key);
                // shake
                var progressRate = GameObjectScript.GetSuccessRate() * GameObjectScript.GetTotalProgress();
                Camera.main.GetComponent<CameraShakeScript>().shake = 0.2f  * progressRate;
                Camera.main.GetComponent<CameraShakeScript>().shakeAmount = Mathf.Clamp(progressRate * 0.03f, 0.1f, 0.3f);
                // bolt
                if(UnityEngine.Random.Range(0, 1.0f) > 0.66f && Key == "Q" && progressRate >= 0.6f)
                {
                    GameObjectScript.Spark.Bolt();
                    GameObjectScript.PlaySound("BOLT");
                }
            }
            // fail hit
            else{
                if(spriteRenderer.sprite != FailSprite)
                {
                    GameObjectScript.FailCount++;
                    setFailColor();
                    Invoke("resetColor", blinkDuration);
                    GameObjectScript.PlaySound("FAIL");
                    SkeletonAnim.Reset();
                    SkeletonAnim.state.SetAnimation(0, "Fail", false);
                }
            }
        }
    }

    private void playGoodParticleSystem()
    {
        var ps = Instantiate(goodStuffPS, gameObject.transform.position, Quaternion.identity) as GameObject;
    }

    private void setFailColor()
    {
        spriteRenderer.sprite = FailSprite;
    }
    private void setSuccColor()
    {
        spriteRenderer.sprite = SuccSprite;
    }
    private void setActiveColor()
    {
        spriteRenderer.sprite = ActiveSprite;
    }
    private void resetColor()
    {
        spriteRenderer.sprite = DefaultSprite;
    }
}