using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class character : MonoBehaviour {
    public Object[] clappers;
    public SpriteRenderer torso;
    public SpriteRenderer skull;
    public SpriteRenderer face;

    Animator anim;

    bool sex;

    Animator clapperAnimator;

    string[] anim_triggers = new string[] { "bob", "headswing", "bodyswing" };
    private int fidgedness;

    bool alive = true;

    public void MakeWith(bool sex /*true = male*/, Sprite torso, Sprite skull, Sprite face)
    {
        this.sex = sex;
        this.torso.sortingOrder -= (int)(transform.position.z * 10);
        this.skull.sortingOrder -= (int)(transform.position.z * 10);
        this.face.sortingOrder -= (int)(transform.position.z * 10);

        float colorScale = (35 - transform.position.z) / 20;
        this.torso.sprite = torso;
        this.skull.sprite = skull;
        this.torso.color = new Color(colorScale, colorScale, colorScale);
        this.skull.color = new Color(colorScale, colorScale, colorScale);
        if (face != null)
        {
            this.face.sprite = face;
            this.face.color = new Color(colorScale, colorScale, colorScale);
        }
        else
        {
            this.face.enabled = false;
        }

        GameObject go = Instantiate(clappers[(sex ? 0 : 1)], this.torso.transform, false) as GameObject;
        go.transform.localPosition += new Vector3(0, 5, 0);
        clapperAnimator = go.GetComponent<Animator>();

        ComboCenter.Instance.HugeComboHandlers += Instance_HugeComboHandlers;

    }

    private void Instance_HugeComboHandlers() {
        if (Random.Range(0f, 1f) < 0.3f) DoClap();
    }

    void Die(float dmg)
    {
        //print("character died, disabling animations");
        alive = false;
        anim.enabled = false;
    }

    void DoClap()
    {
        if (clapperAnimator != null)
            clapperAnimator.SetTrigger("clap");
    }

    // Use this for initialization
    void Awake () {
        anim = GetComponent<Animator>();
        fidgedness = (int)Mathf.Pow(Random.Range(9f, 50f), 2f);
    }
	
	// Update is called once per frame
	void FixedUpdate () {
        if (!alive) return;
		if (Random.Range(0,fidgedness) == 0)
        {
            string animation = anim_triggers[Random.Range(0, anim_triggers.Length)];
            anim.SetTrigger(animation);
            //DoClap();
        }
	}
}
