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

public class Controller : MonoBehaviour {



    public GameObject player;
    public GameObject enemy;
    //public Button[] sequencesBtn;
    public GameObject[] operationBtnGO;
    public GameObject[] choiceBtnGo;


    private Actions actions;

    

    public GameObject pointer;
    public float actionTime = 0.2f;

    private float startSequenceTime;
    private float sequence_tume_to_end;
    private float secuenceTime;

    private int actions_amount = 5;
    private int next_action = 0;

    public Actions.the_action action_to_set;

    public bool player1=false;
    public bool player2=false;

    public Sprite[] actions_in_order;

    private int rnd;

    private void Awake()
    {
        actions = new Actions();
        actions.rb = player.GetComponent<Robot_BehaviourScript>();
        actions.cnrlr = this;
        
        Start_New_Sequence();

        secuenceTime = actions_amount * actionTime;

        
    }

    private void Start()
    {
        Set_new_choise();
    }

    private void Start_New_Sequence()
    {

        startSequenceTime = Time.realtimeSinceStartup;
        sequence_tume_to_end = 5;// startSequenceTime + secuenceTime;
        next_action = 0;
        
    }

    private void Set_new_choise()
    {
        /*foreach(GameObject go in choiceBtnGo)
        {
            int rnd = Random.Range(0, actions.actions_list.Count);
            Actions.the_action rand_action = actions.actions_list[rnd];
            go.GetComponent<operation>().set_action(ref rand_action);
        }*/
        rnd = Random.Range(0, actions.actions_list.Count);
        Actions.the_action rand_action = actions.actions_list[rnd];
        action_to_set = rand_action;
        choiceBtnGo[0].GetComponent<operation>().set_action(ref rand_action);
        Set_sprite(choiceBtnGo[0], rnd);

    }
    
    private void Set_sprite(GameObject go, int place)
    {
        //go.GetComponentInChildren<Image>().sprite = actions_in_order[place];
        go.transform.GetChild(0).GetComponentInChildren<Image>().sprite = actions_in_order[place];
    }
     
    private void Update()
    {
        //time in sequence
        float part_in_sequence = (Time.realtimeSinceStartup - startSequenceTime)/ sequence_tume_to_end;

        
        //new sequence
        if (part_in_sequence > 1)
        {
            Start_New_Sequence();
        }

        //pointer movment
        Pointer_Movement(part_in_sequence);

        //operations things:
        if (part_in_sequence*(float)actions_amount >= (float)next_action)
        {
            actions.enemy_location = enemy.GetComponent<Transform>().localPosition;
            operationBtnGO[next_action].GetComponent<operation>().do_action();
            next_action++;
            
        }

        Set_Choise();

        if (Input.GetKeyDown(KeyCode.R)) //restart
        {
            //Application.LoadLevel(0);
            UnityEngine.SceneManagement.SceneManager.LoadScene(0);
        }

        if (Input.GetKeyDown(KeyCode.Q)) //Quit game
        {
            Application.Quit();
        }

    }

    private void Set_Choise()
    {
        if (player1)
        {

            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                operationBtnGO[0].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[0], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                operationBtnGO[1].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[1], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Alpha3))
            {
                operationBtnGO[2].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[2], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Alpha4))
            {
                operationBtnGO[3].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[3], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Alpha5))
            {
                operationBtnGO[4].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[4], rnd);
                Set_new_choise();
            }
        }
        else if(player2)
        {

            if (Input.GetKeyDown(KeyCode.Keypad1))
            {
                operationBtnGO[0].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[0], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Keypad2))
            {
                operationBtnGO[1].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[1], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Keypad3))
            {
                operationBtnGO[2].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[2], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Keypad4))
            {
                operationBtnGO[3].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[3], rnd);
                Set_new_choise();
            }
            if (Input.GetKeyDown(KeyCode.Keypad5))
            {
                operationBtnGO[4].GetComponent<operation>().Set_new_action();
                Set_sprite(operationBtnGO[4], rnd);
                Set_new_choise();
            }
        }

    }

    private void Pointer_Movement(float part_in_sequence)
    {
        pointer.GetComponent<RectTransform>().anchorMin = new Vector2(pointer.GetComponent<RectTransform>().anchorMin.x,  (1 - part_in_sequence));
        pointer.GetComponent<RectTransform>().anchorMax = new Vector2(pointer.GetComponent<RectTransform>().anchorMax.x, (1- part_in_sequence));
    }

    private int enabled_buttons_amount()
    {
        int amount = 0;

        foreach (GameObject btnGO in operationBtnGO)
        {
            if (btnGO.GetComponent<Button>().enabled) { amount++; }
        }
        return amount;
    }

}

