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

public class ProgramLevelManager : MonoBehaviour
{

    public GameObject Canvas;
    InputField[] inputFieldList;
    public bool ChildrenAssigned;
    public Text inputCodeText1;
    public string inputCode1;
    public InputField inputField1;
    public Text inputCodeText2;
    public string inputCode2;
    public InputField inputField2;
    public Text inputCodeText3;
    public string inputCode3;
    public InputField inputField3;
    public Text inputCodeText4;
    public string inputCode4;
    public InputField inputField4;
	public string targetScene;
	public string originScene;

    // Use this for initialization
    public void Awake()
    {
        DontDestroyOnLoad(this);

        if (FindObjectsOfType(GetType()).Length > 1)
        {
            Destroy(gameObject);
        }
        Canvas = GameObject.Find("Canvas");
        inputFieldList = Canvas.GetComponentsInChildren<InputField>();

        inputField1 = inputFieldList[0];
        inputField2 = inputFieldList[1];
        inputField3 = inputFieldList[2];
        inputField4 = inputFieldList[3];
        inputCodeText1 = inputField1.GetComponentsInChildren<Text>()[1];
        inputCodeText1.text = inputField1.text;
        inputCodeText2 = inputField2.GetComponentsInChildren<Text>()[1];
        inputCodeText2.text = inputField2.text;
        inputCodeText3 = inputField3.GetComponentsInChildren<Text>()[1];
        inputCodeText3.text = inputField3.text;
        inputCodeText4 = inputField4.GetComponentsInChildren<Text>()[1];
        inputCodeText4.text = inputField4.text;
        ChildrenAssigned = true;

    }

    // Update is called once per frame
    void Update()
    {
		CheckScene ();
        if (Input.GetKeyDown(KeyCode.Return))
        {
            AssignChildren();
            inputCode1 = inputCodeText1.text;
            inputCode2 = inputCodeText2.text;
            inputCode3 = inputCodeText3.text;
            inputCode4 = inputCodeText4.text;
            ChildrenAssigned = false;
            SceneManager.LoadScene(targetScene);
        }
    }
    void AssignChildren()
    {
        if (!Canvas && !ChildrenAssigned)
        {
            Canvas = GameObject.Find("Canvas");
            inputFieldList = Canvas.GetComponentsInChildren<InputField>();

            inputField1 = inputFieldList[0];
            inputField2 = inputFieldList[1];
            inputField3 = inputFieldList[2];
            inputField4 = inputFieldList[3];
            inputCodeText1 = inputField1.GetComponentsInChildren<Text>()[1];
            inputCodeText1.text = inputField1.text;
            inputCodeText2 = inputField2.GetComponentsInChildren<Text>()[1];
            inputCodeText2.text = inputField2.text;
            inputCodeText3 = inputField3.GetComponentsInChildren<Text>()[1];
            inputCodeText3.text = inputField3.text;
            inputCodeText4 = inputField4.GetComponentsInChildren<Text>()[1];
            inputCodeText4.text = inputField4.text;
            ChildrenAssigned = true;
        }
    }
	void CheckScene()
	{
		Scene scene1;
		Scene scene2;
		Scene scene3;
		scene1 = SceneManager.GetSceneByName (targetScene);
		scene2 = SceneManager.GetSceneByName (originScene);
		scene3 = SceneManager.GetActiveScene();
		if ((scene1 != scene3) && (scene2 != scene3)) 
		{
			Destroy (gameObject);
		}
	}

}
