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

public class StartPoint : MonoBehaviour
{
    public LevelControl myLevel;
    GameObject PlayerR;
    GameObject PlayerG;
    GameObject PlayerB;
    Collider2D PlayerGCol;
    Collider2D PlayerRCol;
    Collider2D PlayerBCol;
    bool isStarted = false;
    public float Radius;
    // Use this for initialization
    void Start()
    {
        FindPlayers();
    }

    // Update is called once per frame
    void Update()
    {
        if (!isStarted)
        {
            if ((PlayerRCol.bounds.Contains(transform.position) || Physics2D.RaycastAll(transform.position, PlayerR.transform.position - transform.position, Radius, LayerMask.GetMask(new string[] { "RedPlayer" })).Length > 0)
                && (PlayerGCol.bounds.Contains(transform.position) || Physics2D.RaycastAll(transform.position, PlayerG.transform.position - transform.position, Radius, LayerMask.GetMask(new string[] { "GreenPlayer" })).Length > 0)
                && (PlayerBCol.bounds.Contains(transform.position) || Physics2D.RaycastAll(transform.position, PlayerB.transform.position - transform.position, Radius, LayerMask.GetMask(new string[] { "BluePlayer" })).Length > 0))
            {
                //EndDisCalc();
                isStarted = true;
                StartLevel();
            }
        }
    }

    public 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 StartLevel()
    {
        print("234");
        MainControl.TheGame.OnlevelStart();

        var current_level = this.transform.parent.gameObject;
        foreach (var level in GameObject.FindGameObjectsWithTag("Level"))
        {
            if (level == current_level)
            {
                SetAlphaToLevel(level, 1);
            }
            else
            {
                SetAlphaToLevel(level, 0.3f);
            }
        }
    }
    public void FindPlayers()
    {
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject play in players)
        {
            switch (play.GetComponent<MovmentControl>().avaterColor)
            {
                case MovmentControl.PlayersLights.Blue:
                    {

                        PlayerB = play;
                        PlayerBCol = PlayerB.GetComponent<Collider2D>();
                        break;
                    }
                case MovmentControl.PlayersLights.Green:
                    {
                        PlayerG = play;
                        PlayerGCol = PlayerG.GetComponent<Collider2D>();

                        break;
                    }
                case MovmentControl.PlayersLights.Red:
                    {
                        PlayerR = play;
                        PlayerRCol = PlayerR.GetComponent<Collider2D>();
                        break;
                    }
            }
        }
    }
}


