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

public class PulseManager1 : MonoBehaviour
{
    GameObject programLevelManager;
    public int pulseIndex;
    string orders;
    public string[] ordersList = new string[] { "", "", "", "" };
    Collider2D otherCollider;
    public int totalEnergy;
    public int movementCost;
    public int actionCost;
    public Sprite doorSprite;
    public LayerMask mask = 1;
    public Animator anim;
    //ArrayList ordersArray;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
        programLevelManager = GameObject.FindGameObjectWithTag("ProgramLevelManager");

        //ordersList = programLevelManager.GetComponents<ProgramLevelManager>();
        //ordersList = programLevelManager.GetComponents<string>();
        ordersList[0] = programLevelManager.GetComponent<ProgramLevelManager>().inputCode1;
        ordersList[1] = programLevelManager.GetComponent<ProgramLevelManager>().inputCode2;
        ordersList[2] = programLevelManager.GetComponent<ProgramLevelManager>().inputCode3;
        ordersList[3] = programLevelManager.GetComponent<ProgramLevelManager>().inputCode4;
        orders = ordersList[pulseIndex];
        InvokeRepeating("NextAction", 1.0f, 1.0f);

    }

    // Update is called once per frame
    void Update()
    {

    }

    void NextAction()
    {
        if (orders.Length > 0)
        {
            if (orders[0] == 'u')
            { //up
                transform.position = transform.position + new Vector3(0, 1, 0);
                totalEnergy -= movementCost;
            }
            else if (orders[0] == 'd')
            { //down
                transform.position = transform.position + new Vector3(0, -1, 0);
                totalEnergy -= movementCost;
            }
            else if (orders[0] == 'l')
            { //left
                transform.position = transform.position + new Vector3(-1, 0, 0);
                totalEnergy -= movementCost;
            }
            else if (orders[0] == 'r')
            { //right
                transform.position = transform.position + new Vector3(1, 0, 0);
                totalEnergy -= movementCost;
            }
            else if (orders[0] == 'w')
            { //wait

            }
            else if (orders[0] == 'a')
            { // action!
                otherCollider = Physics2D.OverlapCircle(new Vector2(transform.position.x, transform.position.y), 0.2f, mask.value);
                Debug.Log(otherCollider.gameObject.tag);
                if (otherCollider.gameObject.tag == "Door")
                {
                    //Debug.Log ("activo la puerta");
                    otherCollider.gameObject.GetComponent<Door>().OpenDoor();
                    ExplodePulse();


                }
                else if (otherCollider.gameObject.tag == "Camera")
                {
                    //Debug.Log ("vamos bien");
                    otherCollider.gameObject.GetComponent<CameraScript>().TurnCamera();
                    ExplodePulse();

                }
                else if (otherCollider.gameObject.tag == "Enemy")
                {
                    //Debug.Log ("vamos bien");
                    otherCollider.gameObject.GetComponent<EnemyLine>().DisableFOV();
                    ExplodePulse();
                }


            }
            else if (orders[0] == 'x') // XTRA!
            {
                otherCollider = Physics2D.OverlapCircle(new Vector2(transform.position.x, transform.position.y), 0.2f, mask.value);
                Debug.Log(otherCollider.gameObject.tag);
                if (otherCollider.gameObject.tag == "Camera")
                {
                    Debug.Log("destruyo la camara?");
                    otherCollider.gameObject.GetComponent<CameraScript>().ExplodeCamera();
                }
            }

            orders = orders.Substring(1);
        }

    }
    public void ExplodePulse()
    {

        anim.Play("Pulse 1 Explosion");

    }

}
