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

public class EndPoint : MonoBehaviour
{


    public float Radius;
    private bool isEnded;
    public LevelControl myLevel;
    GameObject PlayerR;
    GameObject PlayerG;
    GameObject PlayerB;
    Collider2D PlayerGCol;
    Collider2D PlayerRCol;
    Collider2D PlayerBCol;
    // Use this for initialization
    void Start()
    {
        FindPlayers();
    }
    // Update is called once per frame
    void Update()
    {
        if (!isEnded)
        {
            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();
                isEnded = true;
                myLevel.OnLevelEnd();
            }
        }

    }


    //public void EndDisCalc()
    //{
    //    var dist = Mathf.Max(Math.Max(PlayerR.transform.position.x, Math.Max(PlayerG.transform.position.x, PlayerB.transform.position.x)) - Math.Min(PlayerR.transform.position.x, Math.Min(PlayerG.transform.position.x, PlayerB.transform.position.x)), Math.Max(PlayerR.transform.position.y, Math.Max(PlayerG.transform.position.y, PlayerB.transform.position.y)) - Math.Min(PlayerR.transform.position.y, Math.Min(PlayerG.transform.position.y, PlayerB.transform.position.y)));
    //    if (dist > MaxDist)
    //    {
    //        return;
    //    }
    //}

    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;
                    }
            }
        }
    }
}
