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

public class PowerModule : MonoBehaviour
{
    public enum PowerModuleState
    {
        Automatic, 
        Manual, 
        LowPower
    }
    
    public MeshRenderer barForegroundMesh;

    MapParameterController parameterController;

    PowerModuleState currentState = PowerModuleState.Automatic;
    float currentPower { get; set; }

    // Use this for initialization
    void Awake()
    {
        if(parameterController == null)
            parameterController = GetComponentInParent<MapParameterController>();

        currentPower = 2f;
    }

    public void Init(MapParameterController controller)
    {
        parameterController = controller;
    }

    public bool IsAuto()
    {
        return currentPower > 1f;
    }

    public bool HasValidityFeedback()
    {
        return currentPower > 0f;
    }

    public bool LoosePower(float lostRatio)
    {
        if(currentPower > 0)
        {
            currentPower = Mathf.Max(0f, currentPower - lostRatio);
            barForegroundMesh.transform.parent.localScale = new Vector3(1f, currentPower / 2f, 1f);

            if(currentState == PowerModuleState.Automatic && !IsAuto())
            {
                currentState = PowerModuleState.Manual;
                barForegroundMesh.material = GameUtils.Instance.lightOrangeMaterial;
                parameterController.ActivateManualControl(true);
            }
            if(currentState == PowerModuleState.Manual && !HasValidityFeedback())
            {
                currentState = PowerModuleState.LowPower;
                barForegroundMesh.material = GameUtils.Instance.lightRedMaterial;
                parameterController.DisableValidityFeedback();
            }
            return true;
        }
        return false;
    }
}
