using UnityEngine;
using UnityEngine.SceneManagement;

public class MenuScene : MonoBehaviour
{

    private int currentSelectionP1 = 0;
    private int currentSelectionP2 = 0;
    private bool[] selectionBool = new bool[] { true, false, false, false };
    private GameObject[] selectionIdols;
    private GameObject selection01;
    private GameObject selection02;
    private GameObject selection03;
    private GameObject selection04;
    private GameObject signP1;
    private GameObject signP2;
    private Object[] selectionSprites;
    private float TimerFlashP1;
    private float axisDelayP1 = 5f;
    private float axisDelayP2 = 5f;
    private Vector2 playerSelected = new Vector2(0, 0);
    public float fadeSpeed = 1.5f;
    private GUITexture guiTextre;
    private GameObject globalObject;

    // Use this for initialization
    void Start()
    {
        globalObject = GameObject.Find("GlobalObject");
        selectionSprites = Resources.LoadAll("Sprites/SelectionIdol");
        selection01 = GameObject.Find("SelectionIdol_0");
        selection02 = GameObject.Find("SelectionIdol_1");
        selection03 = GameObject.Find("SelectionIdol_2");
        selection04 = GameObject.Find("SelectionIdol_3");
        selectionIdols = new GameObject[] { selection01, selection02, selection03, selection04 };
        signP1 = GameObject.Find("P1Sign");
        signP2 = GameObject.Find("P2Sign");
    }

    // Update is called once per frame
    void Update()
    {
        if (playerSelected.x == 0)
        {
            if (Input.GetAxis("P1_horizontal") != 0)
            {
                axisDelayP1--;
                if (axisDelayP1 < 0)
                {
                    axisDelayP1 = 5f;
                }
            }
            if (Input.GetAxis("P1_horizontal") <= -1f && axisDelayP1 == 0)
            {
                selectionBool[currentSelectionP1] = false;
                if (currentSelectionP1 == 0)
                {
                    currentSelectionP1 = 3;
                }
                else
                {
                    currentSelectionP1--;
                }
                selectionBool[currentSelectionP1] = true;
                signP1.transform.position = new Vector3(selectionIdols[currentSelectionP1].transform.position.x, signP1.transform.position.y, signP1.transform.position.z);
            }
            if (Input.GetAxis("P1_horizontal") >= 1f && axisDelayP1 == 0)
            {
                selectionBool[currentSelectionP1] = false;
                if (currentSelectionP1 == 3)
                {
                    currentSelectionP1 = 0;
                }
                else
                {
                    currentSelectionP1++;
                }
                signP1.transform.position = new Vector3(selectionIdols[currentSelectionP1].transform.position.x, signP1.transform.position.y, signP1.transform.position.z);
            }
            if (Input.GetButtonDown("P1_fire1") || Input.GetButtonDown("P1_fire2"))
            {
                playerSelected.x = 1;
                //StaticPref.player1Choice = currentSelectionP1;
                //PlayerPrefs.SetInt("player1Choice", currentSelectionP1);
                globalObject.GetComponent<GlobalScript>().player1Choice = currentSelectionP1;
            }
        }
        if (playerSelected.y == 0)
        {
            if (Input.GetAxis("P2_horizontal") != 0)
            {
                axisDelayP2--;
                if (axisDelayP2 < 0)
                {
                    axisDelayP2 = 5f;
                }
            }
            if (Input.GetAxis("P2_horizontal") <= -1f && axisDelayP2 == 0)
            {
                selectionBool[currentSelectionP2] = false;
                if (currentSelectionP2 == 0)
                {
                    currentSelectionP2 = 3;
                }
                else
                {
                    currentSelectionP2--;
                }
                selectionBool[currentSelectionP2] = true;
                signP2.transform.position = new Vector3(selectionIdols[currentSelectionP2].transform.position.x, signP2.transform.position.y, signP2.transform.position.z);
            }
            if (Input.GetAxis("P2_horizontal") >= 1f && axisDelayP2 == 0)
            {
                selectionBool[currentSelectionP2] = false;
                if (currentSelectionP2 == 3)
                {
                    currentSelectionP2 = 0;
                }
                else
                {
                    currentSelectionP2++;
                }
                
                signP2.transform.position = new Vector3(selectionIdols[currentSelectionP2].transform.position.x, signP2.transform.position.y, signP2.transform.position.z);
            }
            if (Input.GetButtonDown("P2_fire1") || Input.GetButtonDown("P2_fire2"))
            {
                playerSelected.y = 1;
                //StaticPref.player2Choice = currentSelectionP2;
                //PlayerPrefs.SetInt("player2Choice", currentSelectionP2);
                globalObject.GetComponent<GlobalScript>().player2Choice = currentSelectionP2;
            }
        }
                if (selectionBool[0])
                {
                    selection01.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[1];
                }
                else
                {
                    selection01.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[5];
                }
                if (selectionBool[1])
                {
                    selection02.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[2];
                }
                else
                {
                    selection02.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[6];
                }
                if (selectionBool[2])
                {
                    selection03.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[3];
                }
                else
                {
                    selection03.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[7];
                }
                if (selectionBool[3])
                {
                    selection04.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[4];
                }
                else
                {
                    selection04.GetComponent<SpriteRenderer>().sprite = (Sprite)selectionSprites[8];
                }
        selectionBool[currentSelectionP1] = true;
        selectionBool[currentSelectionP2] = true;
        if (playerSelected.x == 1 && playerSelected.y == 1)
        {
            FadeToWhite();
            if (guiTextre.color.a >= 0.95f)
            {
                SceneManager.LoadSceneAsync("Main");
            }
        }
    }

    void Awake()

    {
        guiTextre = GetComponent<GUITexture>();
        guiTextre.pixelInset = new Rect(0f, -400f, Screen.width, Screen.height);
    }

    public void FadeToWhite()

    {
        guiTextre.color = Color.Lerp(guiTextre.color, Color.white, fadeSpeed * Time.deltaTime);
    }
}