﻿using UnityEngine;
using System.Collections;


public class Unit : Entity, IMovable, IAnimatedEntity {

    //private NavMeshAgent navComponent;

    private float tmpStoppingDistance;

    protected override void Awake()
    {
        base.Awake();
        ResourceManager.units.Add(this.gameObject);
    }

	protected override void Start () {
        base.Start();
        currentTarget = null;
        isMoving = false;
        isSelected = false;
        //navComponent = this.transform.GetComponent<NavMeshAgent>();
        //tmpStoppingDistance = navComponent.stoppingDistance;
	}

    protected override void Update()
    {
        base.Update();

        if (gameObject.activeSelf)
        {
            if (CanAttack() && !isMoving)
            {
                ChooseTarget();
                Attack();
            }

            if (isMoving)
            {
                MoveTo();
            }
        }

		if(this.tag == "Crystal" && this.healthPoints == 0)
			Application.LoadLevel(3);
    }

    protected override bool CanAttack()
    {
        return true;
    }

    protected override void ChooseTarget()
    {
        base.ChooseTarget();

        if (currentTarget == null){
            ArrayList monsters = ResourceManager.GetAllMonsters();

            for (int i = 0; i < monsters.Count; i++)
            {
                GameObject monster = (GameObject)monsters[i];

                if (TargetInRange(monster) && monster.activeSelf)
                {
                    currentTarget = monster;
                }
            }  
        }
        else
        {
            isMoving = true;
        }
    }

    protected override void Attack()
    {
        if (currentTarget != null)
        {
            if(currentTarget.tag == "monster")
            {
                if (currentTarget.GetComponent<Monster>().healthPoints > 0)
                {
                    if (Time.time > nextAttack)
                    {
                        nextAttack = Time.time + attackSpeed;
                        currentTarget.GetComponent<Monster>().healthPoints -= damage;
                    }
                }
                else
                {
                    currentTarget = null;
                    isMoving = false;
                }
            }
        }
    }

	public void MoveTo(){

        /*navComponent.SetDestination(currentTarget.transform.position);
        if (currentTarget.tag == "mouse"){
            navComponent.stoppingDistance = 0;

            if (Vector3.Distance(transform.position, currentTarget.transform.position) == navComponent.stoppingDistance)
            {
                isMoving = false;
                currentTarget = null;
                navComponent.stoppingDistance = tmpStoppingDistance;
            }
        }
        else
        {
            if (Vector3.Distance(transform.position, currentTarget.transform.position) <= navComponent.stoppingDistance)
            {
                isMoving = false;
            }
        }*/
       
	}

    public void CreateAnimationState()
    {

    }

    protected override void ShowDetails()
    {
        GUI.Box(new Rect(0, 0, 100, 100), "I'm an UNIT"); 
    }
}
