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

public class Actions {

    public Robot_BehaviourScript rb;

    public Controller cnrlr;

    public Vector3 enemy_location;

    public delegate void the_action();
    the_action some_action;

    public List<the_action> actions_list;

    public Actions()
    {
        actions_list = new List<the_action>();
        actions_list.Add(Forward);
        actions_list.Add(Right);
        actions_list.Add(Left);
        actions_list.Add(Turn_Around);
        //actions_list.Add(Step_Right);
        //actions_list.Add(Step_Left);
        actions_list.Add(shoot);
    }

    private void Awake()
    {
        actions_list = new List<the_action>();
        actions_list.Add(Forward);
        actions_list.Add(Right);
    }

    float pos_num = 0.56005f;

    public void Forward()
    {
        Vector3 new_position = rb.GetComponent<Transform>().localPosition;
        new_position.x += rb.direction.x * rb.size;
        new_position.y += rb.direction.y * rb.size;
        if (new_position.x >= -pos_num && new_position.x <= pos_num && new_position.y >= -pos_num && new_position.y <= pos_num
            && new_position != enemy_location)
        {
            rb.GetComponent<Transform>().localPosition = new_position;
        }
    }

    public void Right()
    {
        rb.direction = new Vector2(rb.direction.y, -rb.direction.x);
        rb.SpriteRotation();
    }

    public void Left()
    {
        rb.direction = new Vector2(-rb.direction.y, rb.direction.x);
        rb.SpriteRotation();
    }

    public void Turn_Around()
    {
        rb.direction = new Vector2(-rb.direction.x, -rb.direction.y);
        rb.SpriteRotation();
    }

    public void Step_Right()
    {

        Vector3 new_position = rb.GetComponent<Transform>().localPosition;
        if (new_position.x >= -pos_num && new_position.x <= pos_num && new_position.y >= -pos_num && new_position.y <= pos_num)
        {
            new_position.x += rb.direction.y * rb.size;
            new_position.y -= rb.direction.x * rb.size;
            rb.GetComponent<Transform>().localPosition = new_position;
        }
    }

    public void Step_Left()
    {
        Vector3 new_position = rb.GetComponent<Transform>().localPosition;
        if (new_position.x >= -pos_num && new_position.x <= pos_num && new_position.y >= -pos_num && new_position.y <= pos_num)
        {
            new_position.x -= rb.direction.y * rb.size;
            new_position.y += rb.direction.x * rb.size;
            rb.GetComponent<Transform>().localPosition = new_position;
        }
    }

    public void shoot()
    {
        
        Vector2 direction = rb.direction;
        Vector3 location = rb.transform.localPosition;
        float x = -(location.x - enemy_location.x);
        float y = -(location.y - enemy_location.y);

        float tolerance = 0.0001f;
        if (x <= tolerance && x>=-tolerance)
        {
            if(y>0 && direction.y==1)
            {
                Winn();
            }
            if (y < 0 && direction.y == -1)
            {
                Winn();
            }
        }

        if (y <= tolerance && y >= -tolerance)
        {
            if (x > 0 && direction.x == 1)
            {
                Winn();
            }
            if (x < 0 && direction.x == -1)
            {
                Winn();
            }
        }
    }

    public void Winn()
    {
        string player = "player";
        if (cnrlr.player1) { player += "1"; }
        if (cnrlr.player2) { player += "2"; }
        GameObject.Find("Win").GetComponent<Text>().text = player+" WIN!!";
        //rb.Win();
    }


}
