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

public class Enemy : MonoBehaviour 
{
    [SerializeField]
    private GameObject powerDrop;
    [SerializeField]
    private PowerLevels powerLevel;
    private float power = 1.0f;
    private enum PowerLevels { VeryLow, Low, Medium, High, Full}
    private EnemyAI.BehaviorController behaviorController;
    private DayTimeManager dayTimeManager;
    private bool dead = false;

    private DayTimeLabel lastCharging;

    void OnCollisionEnter(Collision collision)
    {
        if(!dead)
        {
            if (collision.collider.tag == "Bullet")
            {
                Death();
            }
        }
    }

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

        behaviorController = new EnemyAI.BehaviorController();
        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;
                        break;
                    case (DayTimeLabel.Morning):
                        powerLevel = PowerLevels.VeryLow;
                        power = 1;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        break;
                    case (DayTimeLabel.Noon):
                        powerLevel = PowerLevels.Low;
                        power = 2;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        break;
                    case (DayTimeLabel.Evening):
                        powerLevel = PowerLevels.Medium;
                        power = 4;
                        lastCharging = dayTimeManager.dayTimeLabel;
                        break;
                }
            }
        }
	}

    private void Death()
    {
        dead = true;

        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);
    }
}
