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

public class LevelControl : MonoBehaviour
{
    public GameObject startPoint;
    bool levelActive = false;
    int levelNumber;
    public bool lastLevel;
    public bool startLevel;
    private bool Ended = false;
    GameObject PlayerR;
    GameObject PlayerG;
    GameObject PlayerB;
    Collider2D PlayerRCol;
    Collider2D PlayerGCol;
    Collider2D PlayerBCol;
    private float StartEndTime;
    public float CameraMoveTime;
    public CamaraData camaraSettings;
    public List<GameObject> RemoveWhenEnd;
    public List<GameObject> MakeUnwalkable;
    // Use this for initialization
    void Start()
    {
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject play in players)
        {
            switch (play.GetComponent<MovmentControl>().avaterColor)
            {
                case MovmentControl.PlayersLights.Blue:
                    {
                        PlayerB = play;
                        PlayerBCol = play.GetComponent<Collider2D>();
                        break;
                    }
                case MovmentControl.PlayersLights.Green:
                    {
                        PlayerG = play;
                        PlayerGCol = play.GetComponent<Collider2D>();
                        break;
                    }
                case MovmentControl.PlayersLights.Red:
                    {
                        PlayerR = play;
                        PlayerRCol = play.GetComponent<Collider2D>();
                        break;
                    }
            }
        }

    }
    // Update is called once per frame
    void Update()
    {

    }
    public int LevelNumber
    {
        set
        {
            levelNumber = value;
        }
    }

    private static void SetAlphaToLevel(GameObject level, float alpha)
    {
        foreach (var renderer in level.GetComponentsInChildren<Renderer>())
        {
            var color = renderer.material.GetColor("_Color");
            color.a = alpha;
            renderer.material.SetColor("_Color", color);
        }
    }

    public void OnLevelEnd()
    {
        if (lastLevel)
        {
            SceneManager.LoadScene("Menu");
        }
        MainControl.TheGame.FreeMovement();
        MainControl.TheGame.PutTogeder();
        CloseLevel();
        MainControl.TheGame.LevelChangeStart();

        foreach (var level in GameObject.FindGameObjectsWithTag("Level"))
        {
            SetAlphaToLevel(level, 1);
        }
    }
    public float CamSize
    {
        get
        {
            return camaraSettings.size;
        }
    }
    public Vector3 CamLoc
    {
        get
        {
            return camaraSettings.postion;
        }
    }
    public void CloseLevel()
    {
        foreach (GameObject kill in RemoveWhenEnd)
        {
            kill.SetActive(false);
        }
        foreach (GameObject unWalk in MakeUnwalkable)
        {
            unWalk.layer = LayerMask.NameToLayer(Library.Data.noColors);
        }
    }
    public void LockLevel()
    {
        foreach (GameObject kill in RemoveWhenEnd)
        {
            kill.SetActive(true);
        }
    }
}


