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

public class MainControl : MonoBehaviour
{
    public static MainControl TheGame;
    public LevelControl[] levels;
    LevelControl currentLevel;
    int currentLevelNumber;
    public Camera mainCamera;
    float startEndTime;
    bool isCamaraMove;
    GameObject[] PlayersObjects;
    Collider2D PlayerRCol;
    Collider2D PlayerGCol;
    Collider2D PlayerBCol;
    Vector3 nextCamLoc;
    public int LevelStart;
    void Awake()
    {
        TheGame = this;
    }
    // Use this for initialization
    void Start ()
    {
        PlayersObjects = new GameObject[3];
        FindPlayers();
        for (int i=0;i<levels.Length;i++)
        {
            levels[i].LevelNumber = i;
        }
        if (levels.Length > 0)
        {
            currentLevel = levels[LevelStart-1];
        }
        currentLevelNumber = LevelStart-1;
        if(LevelStart>1)
        {
            TeleportPlayer(currentLevel.startPoint.transform.position);
            mainCamera.orthographicSize = currentLevel.camaraSettings.size;
            mainCamera.transform.position = currentLevel.camaraSettings.postion;
        }
        

	}
	// Update is called once per frame
	void Update ()
    {
        if (isCamaraMove)
        {
           if(CameraMovement())
            {
                isCamaraMove = false;
                LevelChangeEnd();

            }
        }
    }
    public void LevelChangeStart()
    {
        if (!currentLevel.lastLevel)
        {
            CamaraMoveSet();
        }
        
    }
    public void LevelChangeEnd()
    {
        currentLevel = levels[currentLevelNumber + 1];
        currentLevelNumber++;
        FreeMovement();
    }
    public void CamaraMoveSet()
    {
        isCamaraMove = true;
        startEndTime = Time.fixedTime;
    }
    public bool CameraMovement()
    { //move the camara per frame. 
      //return is the movment is over

        var pos = (Time.fixedTime - startEndTime) / currentLevel.CameraMoveTime;
        Vector3 startPos = currentLevel.CamLoc;
        float nextSize = levels[currentLevelNumber+1].CamSize;
        Vector3 nextLoc = levels[currentLevelNumber+1].CamLoc;
        float startSize = currentLevel.CamSize;
        float gx = 0;
        var maxSize = Mathf.Max(startSize, nextSize) + 1;
        if (pos <= 0.5f)
        {
            gx = 2 * pos * pos;

            mainCamera.orthographicSize = startSize + gx * 2 * (maxSize - startSize);

        }
        else
        {
            gx = 1 - 2 * (pos - 1) * (pos - 1);
            mainCamera.orthographicSize = maxSize + (gx - 0.5f) * 2 * (nextSize - maxSize);
        }

        nextCamLoc = startPos + gx * (nextLoc - startPos);
        mainCamera.transform.position = nextCamLoc;

        if (Time.fixedTime - startEndTime >= currentLevel.CameraMoveTime)
        {

            mainCamera.transform.position = nextLoc;
            mainCamera.orthographicSize = nextSize;
           
            return true;
        }
        return false;
    }
    public void FindPlayers()
    {
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject play in players)
        {
            switch (play.GetComponent<MovmentControl>().avaterColor)
            {
                case MovmentControl.PlayersLights.Blue:
                    {
                        PlayersObjects[2] = play;
                        PlayerBCol = play.GetComponent<Collider2D>();
                        break;
                    }
                case MovmentControl.PlayersLights.Green:
                    {
                        PlayersObjects[1] = play;
                        PlayerGCol = play.GetComponent<Collider2D>();
                        break;
                    }
                case MovmentControl.PlayersLights.Red:
                    {
                        PlayersObjects[0] = play;
                        PlayerRCol = play.GetComponent<Collider2D>();
                        break;
                    }
            }
        }
    }
    public void FreeMovement()
    {
        foreach(GameObject part in PlayersObjects)
        {
            part.gameObject.GetComponent<MovmentControl>().enabled = true;
        }
    }
    public void FreezeMovementAll()
    {
        foreach (GameObject part in PlayersObjects)
        {
            part.gameObject.GetComponent<MovmentControl>().enabled = false;
        }
    }
    public void OnlevelStart()
    {
        if(currentLevelNumber!=0)
        {
            levels[currentLevelNumber - 1].LockLevel();
        }
    }
    public void ResetLevel()
    {

        GameObject[] spikes = GameObject.FindGameObjectsWithTag("Spike");
        FreezeMovementAll();
        Vector3 moveHere = currentLevel.startPoint.transform.position;
        foreach (GameObject player in PlayersObjects)
        {
            PutTogeder();
            player.GetComponent<Rigidbody2D>().velocity.Set(0, 0);
            print(player.GetComponent<Rigidbody2D>().velocity);
        }
        TeleportPlayer(moveHere);
        foreach(GameObject spiker in spikes)
        {
            spiker.GetComponent<SpilkesScript>().IsKill = false;
        }
            FreeMovement();
    }
    public void PutTogeder()
    {
        // Stick them all together
        var x = (PlayersObjects[0].transform.position.x + PlayersObjects[1].transform.position.x + PlayersObjects[2].transform.position.x) / 3.0f;
        var y = (PlayersObjects[0].transform.position.y + PlayersObjects[1].transform.position.y + PlayersObjects[2].transform.position.y) / 3.0f;
        PlayersObjects[0].transform.position = PlayersObjects[2].transform.position = PlayersObjects[1].transform.position = new Vector3(x, y, 0);
        //PlayerR.GetComponent<Rigidbody2D>().velocity = PlayerG.GetComponent<Rigidbody2D>().velocity = PlayerB.GetComponent<Rigidbody2D>().velocity =
        //(PlayerR.GetComponent<Rigidbody2D>().velocity + PlayerG.GetComponent<Rigidbody2D>().velocity + PlayerB.GetComponent<Rigidbody2D>().velocity) / 3.0f;
    }
    public void TeleportPlayer(Vector3 telpHere)
    {
      
        foreach (GameObject player in PlayersObjects)
        {
            player.transform.position = telpHere;
        }
    }
}
