using System;
using UnityEngine;

public class CharacterAppearance : MonoBehaviour
{
    [Header("Runtime")]
    [SerializeField, ReadOnly] private CatController player;
    [SerializeField, ReadOnly] private SpriteRenderer bodySprite;
    [SerializeField, ReadOnly] private Animator spriteAnimator;
    [SerializeField, ReadOnly] private ParticleSystem psBlood;
    [SerializeField, ReadOnly] private ParticleSystem psTrail;
    [SerializeField, ReadOnly] private ParticleSystem psWater;

    public CatController Player { get { return player; } set { player = value; } }


    private void Awake ()
    {
        bodySprite = GetComponent<SpriteRenderer>();
        spriteAnimator = GetComponent<Animator>();
        psBlood = transform.Find("Blood").GetComponentInChildren<ParticleSystem>();
        psTrail = transform.Find("Trail").GetComponentInChildren<ParticleSystem>();
        psWater = transform.Find("WaterSplash").GetComponentInChildren<ParticleSystem>();
    }

    public void UpdateAnimation(Vector2 direction)
    {
        spriteAnimator.SetFloat("dirX", direction.x);
        spriteAnimator.SetFloat("dirY", direction.y);

        if(Mathf.Abs(direction.x) > 0f || Mathf.Abs(direction.y) > 0f)
            spriteAnimator.SetBool("Moving", true);
        else
            spriteAnimator.SetBool("Moving", false);

        if(player.Physics.WasBumped)
        {
            spriteAnimator.SetTrigger("Bump");
            player.Physics.WasBumped = false;
        }
    }

    public void ToggleVisibility(bool b)
    {
        bodySprite.enabled = b;
    }

    public void Kill(bool blownUp)
    {
        if (blownUp)
            psBlood.Play();
        else
            psWater.Play();

        player.Physics.DisableCollider();

        psTrail.Stop();
        psTrail.Clear();
        ToggleVisibility(false);
		player.Input.SetPermissions(false);
		GameStart.Instance.KillPlayer(transform);
    }

    public void SetSkin(AnimatorOverrideController aoc)
    {
        spriteAnimator.runtimeAnimatorController = aoc as RuntimeAnimatorController;
        //spriteAnimator.AnimatorOverrideController
    }
}