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

public class Person : MonoBehaviour
{

    public Population population;
    public SpriteRenderer sprite;
    private float health = 1;
    private Animator animator;
    public GameObject flies;

    public float Health
    {
        set
        {
            health = value;
        }
        get
        {
            return health;
        }
    }
    public NeighboursDetector neighbours;
    public Move moveScript;

    private float healthLossRate;
    private int personID;
    Sprite[] personSprites = new Sprite[3];

    private float timer;

    private void Start()
    {

        timer = Time.time + UnityEngine.Random.Range(0, 100f);
        animator = GetComponent<Animator>();
        neighbours.Population = population;
        healthLossRate = population.HealthLossRate / 2 + (UnityEngine.Random.value * population.HealthLossRate * 2);
        personID = UnityEngine.Random.Range(0, 9);
        personSprites = Resources.LoadAll<Sprite>("People/Person " + personID);
        sprite.sprite = personSprites[0];
    }

    private bool _contamination = false;
    public bool IsContaminated
    {
        get
        {
            return _contamination;
        }
        set
        {
            _contamination = value;
            if (value)
            {
                GetComponentInChildren<ParticleSystem>().Play();
                flies.GetComponent<ParticleSystem>().Play();
            }
        }
    }

    public void SetContaminated()
    {
        IsContaminated = true;
    }

    private void Update()
    {
        moveScript.Speed = IsContaminated ? 0.5f: 1.5f;
        //sprite.color = Color.black * (1 - health) + health * new Color(1 - 0.8f * (IsContaminated ? 1 : 0), 1f, 1 - 0.8f * (IsContaminated ? 1 : 0));
        if (health < 1 && health > 0.5f && sprite.sprite != personSprites[1])
            sprite.sprite = personSprites[1];
        else if(health < 0.5f && sprite.sprite != personSprites[2])
            sprite.sprite = personSprites[2];
    }

    private void FixedUpdate()
    {
        if (IsContaminated)
        {


            if (timer < Time.time) 
            {
                AudioManager.instance.PlayCoughSound();
                timer = Time.time + UnityEngine.Random.Range(0, 100f);
            }
            health -= healthLossRate * Time.deltaTime;
            foreach (var n in neighbours.list)
            {
                try
                {
                    n.GetContaminatedBySpread(population.ContaminationRate);
                }
                catch (Exception e)
                {
                    Debug.Log(name);
                    Debug.Log(e.Message);
                }
            }
        }
        if (health < 0)
        {
            animator.SetBool("Die", true);
        }
    }

    public void Kill()
    {
        population.Kill(this);
    }

    public bool GetContaminatedBySpread(float chance)
    {
        if (!IsContaminated)
        {
            IsContaminated = UnityEngine.Random.value > (1 - chance);
            return IsContaminated;
        }
        return false;

    }
}
