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

public class ReturnToHngar : MonoBehaviour
{

    public GameObject cameraPrefab;
    /*public Rect[][] camRecs={
		new Rect[]{ new Rect(0,0,1,1), },
		new Rect[]{ new Rect(0,0.5f,1,0.5f),new Rect(0,0,1,0.5f) },
		new Rect[]{ new Rect(0,0.5f,1,0.5f),new Rect(0,0,0.5f,0.5f),new Rect(0.5f,0,0.5f,0.5f) },
		new Rect[]{ new Rect(0,0.5f,1,0.5f),new Rect(0.5f,0.5f,0.5f,0.5f),new Rect(0,0,0.5f,0.5f),new Rect(0.5f,0,0.5f,0.5f) },
	};*/
    public Rect[][] camRecs ={
        new Rect[]{ new Rect(0,0,1,1), },
        new Rect[]{ new Rect(0,0,0.5f,1f),new Rect(0.5f,0,0.5f,1f) },
        new Rect[]{ new Rect(0,0,0.5f,1f),new Rect(0.5f,0.5f,0.5f,0.5f),new Rect(0.5f,0,0.5f,0.5f) },
        new Rect[]{ new Rect(0,0.5f,1,0.5f),new Rect(0.5f,0.5f,0.5f,0.5f),new Rect(0,0,0.5f,0.5f),new Rect(0.5f,0,0.5f,0.5f) },
    };
    public LayerMask[] masks ={

    };

    public int[] palyerMasks;

    public GameObject gameOver;
    public Transform[] spawnPoints;

    public GameObject[] submarinePrefabs;

	int deadPlayers=0;

    public static void SetLayerRecursively(GameObject go, int layerNumber)
    {
        if (go == null) return;
        foreach (Transform trans in go.GetComponentsInChildren<Transform>(true))
        {
            trans.gameObject.layer = layerNumber;
        }
    }
    void Start()
    {
        PlayerController.players = new List<PlayerController>();
        for (int i = 0; i < PartyController.Instance.players.Count; i++)
        {
            var go = Instantiate(submarinePrefabs[PartyController.Instance.players[i].selectedCharacter],spawnPoints[Random.Range(0,spawnPoints.Length)].position,Quaternion.identity);
            var camera = go.GetComponentInChildren<Camera>();
            camera.rect = camRecs[PartyController.Instance.players.Count - 1][i];
            camera.cullingMask = masks[i];
            SetLayerRecursively(go.transform.GetChild(2).gameObject, palyerMasks[i]);
            var pow = go.GetComponentInChildren<PlayerController>();
            pow.playerInput = PartyController.Instance.players[i];
        }
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.R))
			UnityEngine.SceneManagement.SceneManager.LoadScene(2);
    }

}
