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

public class Enemy : MonoBehaviour
{
    [SerializeField]
    private GameObject powerDrop;
    [SerializeField]
    private PowerLevels powerLevel;
    [SerializeField]
    private GameObject IndicatorCoverVeryLow;
    [SerializeField]
    private GameObject IndicatorCoverLow;
    [SerializeField]
    private GameObject IndicatorCoverMedium;
    [SerializeField]
    private GameObject IndicatorCoverHigh;
    [SerializeField]
    private GameObject IndicatorCoverFull;

    private float power = 1.0f;
    private enum PowerLevels { VeryLow, Low, Medium, High, Full }
    private EnemyAI.BehaviorController behaviorController;
    private DayTimeManager dayTimeManager;
    private float health;

    private DayTimeLabel lastCharging;
    public float maxHealth = 2.5f;
    private bool dead;

    public AudioClip explosion;
    public AudioClip[] hitMe;
    public WeaponEnemy weapon;


    void OnCollisionEnter(Collision collision)
    {
        if (!dead)
        {
            if (collision.collider.tag == "Bullet")
            {
                Player.AudioSource.clip = hitMe[Random.Range(0, 1) > 0.5 ? 0 : 1];
                Player.AudioSource.Play();
                health -= 1f;
            }
        }
        if (health <= 0)
            Death();
    }

    // Use this for initialization
    void Start()
    {
        health = maxHealth;
        dayTimeManager = GameObject.FindObjectOfType<DayTimeManager>();

        behaviorController = new EnemyAI.BehaviorController();
        behaviorController.blackboard.Weapon = weapon;
        behaviorController.blackboard.navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!dead)
        {
            // AI
            EnemyAI.Blackboard blackboard = behaviorController.blackboard;
            if (dayTimeManager.dayTimeLabel == DayTimeLabel.Night)
            {
                blackboard.Night = true;
            }
            else
            {
                blackboard.Night = false;
            }
            blackboard.PlayerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
            blackboard.EnemyPosition = transform.position;

            behaviorController.Update();

            // Energy collection
            if (dayTimeManager.dayTimeLabel != lastCharging)
            {
                switch (dayTimeManager.dayTimeLabel)
                {
                    case (DayTimeLabel.Night):
                        powerLevel = PowerLevels.Full;
                        power = 16;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        IndicatorCoverVeryLow.SetActive(false);
                        IndicatorCoverLow.SetActive(false);
                        IndicatorCoverMedium.SetActive(false);
                        IndicatorCoverHigh.SetActive(false);
                        IndicatorCoverFull.SetActive(false);
                        break;
                    case (DayTimeLabel.Morning):
                        powerLevel = PowerLevels.VeryLow;
                        power = 1;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        IndicatorCoverVeryLow.SetActive(false);
                        IndicatorCoverLow.SetActive(true);
                        IndicatorCoverMedium.SetActive(true);
                        IndicatorCoverHigh.SetActive(true);
                        IndicatorCoverFull.SetActive(true);
                        break;
                    case (DayTimeLabel.Noon):
                        powerLevel = PowerLevels.Low;
                        power = 2;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        IndicatorCoverVeryLow.SetActive(false);
                        IndicatorCoverLow.SetActive(false);
                        IndicatorCoverMedium.SetActive(true);
                        IndicatorCoverHigh.SetActive(true);
                        IndicatorCoverFull.SetActive(true);
                        break;
                    case (DayTimeLabel.Evening):
                        powerLevel = PowerLevels.Medium;
                        power = 4;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        IndicatorCoverVeryLow.SetActive(false);
                        IndicatorCoverLow.SetActive(false);
                        IndicatorCoverMedium.SetActive(false);
                        IndicatorCoverHigh.SetActive(true);
                        IndicatorCoverFull.SetActive(true);
                        break;
                }
            }
        }
    }

    private void Death()
    {
        Player.AudioSource.clip = explosion;
        Player.AudioSource.pitch = 1.0f;
        Player.AudioSource.Play();

        dead = true;
        weapon.gameObject.SetActive(false);

        GetComponent<UnityEngine.AI.NavMeshAgent>().isStopped = true;

        GameObject go = GameObject.Instantiate(powerDrop);
        go.GetComponent<PowerDrop>().powerAmount = power;
        go.transform.position = gameObject.transform.position;

        GameObject.Destroy(gameObject);
    }
}
