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

public class MapParametersManager : MonoBehaviour
{
    public static MapParametersManager Instance;

    public MeshRenderer playerQuad;
    public MeshRenderer resultQuad;

    MapParameterController[] controllers;
    int[] randomControllerIndexes;

    float powerTimeDecreaseTimer;

    bool parametersMingle;

    const float powerTimeDecrease = 0.01f;
    const float powerTimeDecreaseDelay = 3f;

    void Awake()
    {
        Instance = this;
        powerTimeDecreaseTimer = powerTimeDecreaseDelay;
    }

	// Use this for initialization
	void Start ()
    {
        controllers = GetComponentsInChildren<MapParameterController>();
        randomControllerIndexes = new int[controllers.Length];

        parametersMingle = true;
        for(int i = 0; i < controllers.Length; i++)
        {
            randomControllerIndexes[i] = i;

            controllers[i].Init();
            controllers[i].AssignRandomValue(resultQuad);
        }
        parametersMingle = false;
        CheckParameters();

        GameEventManager.OnJumpEnd += OnJumpEnd;
	}

    void OnDestroy()
    {
        GameEventManager.OnJumpEnd -= OnJumpEnd;
    }

    void Update()
    {
        powerTimeDecreaseTimer -= Time.deltaTime;
        if(powerTimeDecreaseTimer <= 0)
        {
            LoosePower(powerTimeDecrease);
            powerTimeDecreaseTimer = powerTimeDecreaseDelay;
        }
    }

    public void CheckParameters()
    {
        if(!parametersMingle)
        {
            UIManager.Instance.ActivateJumpButton(AreAllParametersCorrect());
            if(AreAllParametersCorrect())
            {
                GameEventManager.JumpReady();
            }
        }
    }

    public bool AreAllParametersCorrect()
    {
        bool isCorrect = true;
        for(int i = 0; i < controllers.Length; i++)
        {
            if(!controllers[i].IsCorrect())
            {
                isCorrect = false;
            }
        }
        return isCorrect;
    }

    public void OnJumpEnd()
    {
        if(StoryManager.Instance.RemainingJumpsAmount > 0)
        {
            LoosePower(1f);
            parametersMingle = true;
            for(int i = 0; i < controllers.Length; i++)
            {
                controllers[i].AssignRandomValue(resultQuad);
            }
            parametersMingle = false;
            CheckParameters();
        }
    }

    void LoosePower(float powerRatio)
    {
        for(int i = 0; i < 20; i++)
        {
            int randomIndex1 = Random.Range(0, randomControllerIndexes.Length);
            int randomIndex2 = Random.Range(0, randomControllerIndexes.Length);
            int tempValue = randomControllerIndexes[randomIndex1];
            randomControllerIndexes[randomIndex1] = randomControllerIndexes[randomIndex2];
            randomControllerIndexes[randomIndex2] = tempValue;
        }
        int lostSystemsAmount = StoryManager.Instance.GetLostSystemsAmount(); 
        for(int i = 0; i < randomControllerIndexes.Length; i++)
        {
            if(controllers[randomControllerIndexes[i]].LoosePower(powerRatio))
            {
                lostSystemsAmount--;
                if(lostSystemsAmount <= 0)
                {
                    break;
                }
            }
        }
    }
}
