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

public class MenuCanvasController : MonoBehaviour {


    public Text modeText;

    public Text intensityText;

    public Slider intensitySlider;

    public Slider areaOfEffectSlider;

    public Slider skinTransparencySlider;

    public DeformationManager deformationManger;

    private void Start()
    {
        deformationManger.OnIntensityChange += (_ => ShowIntensity(deformationManger.CurrentIntensity));
        deformationManger.OnDeformationChange += (newDeformation => SetupDeformation(newDeformation));
        deformationManger.OnAreaOfEffectChange += (_ => ShowAreaOfEffect(deformationManger.AreaOfEffect));
        SetupMenu(deformationManger.MinAreaOfEffect, deformationManger.MaxAreaOfEffect);
    }

    public void SetupMenu(float minAOE, float maxAOE)
    {
        areaOfEffectSlider.minValue = minAOE;
        areaOfEffectSlider.maxValue = maxAOE;
    }

    public void ShowAreaOfEffect(float aoe)
    {
        //avoid to call these events if value changed on different place
        int onValueChangedCount = intensitySlider.onValueChanged.GetPersistentEventCount();
        for (int i = 0; i < onValueChangedCount; i++)
        {
            intensitySlider.onValueChanged.SetPersistentListenerState(0, UnityEngine.Events.UnityEventCallState.Off);
        }

        areaOfEffectSlider.value = aoe;

        for (int i = 0; i < onValueChangedCount; i++)
        {
            intensitySlider.onValueChanged.SetPersistentListenerState(0, UnityEngine.Events.UnityEventCallState.RuntimeOnly);
        }

    }

    public void ChangeAOEBySlider()
    {
        deformationManger.AreaOfEffect = (areaOfEffectSlider.value);
    }

    public void SetupDeformation(BaseDeformation deformation)
    {
        modeText.text = deformation.DeformationName;

        //avoid to call these events if value changed on different place
        int onValueChangedCount = intensitySlider.onValueChanged.GetPersistentEventCount();
        for (int i = 0; i < onValueChangedCount; i++)
        {
            intensitySlider.onValueChanged.SetPersistentListenerState(0, UnityEngine.Events.UnityEventCallState.Off);
        }

        intensitySlider.maxValue = deformation.MaxIntensity;
        intensitySlider.minValue = deformation.MinIntensity;

        for (int i = 0; i < onValueChangedCount; i++)
        {
            intensitySlider.onValueChanged.SetPersistentListenerState(0, UnityEngine.Events.UnityEventCallState.RuntimeOnly);
        }

        Debug.Log("changing to " + deformation.DeformationName + " with intensity of " + deformation.Intensity);
        ShowIntensity(deformation.Intensity);
    }


    public void ShowIntensity(float intensity)
    {
        intensityText.text = "Intensity: " + intensity;
        
        //avoid to call these events if value changed on different place
        int onValueChangedCount = intensitySlider.onValueChanged.GetPersistentEventCount();
        for(int i = 0; i < onValueChangedCount; i++)
        {
            intensitySlider.onValueChanged.SetPersistentListenerState(0, UnityEngine.Events.UnityEventCallState.Off);
        }
        
        intensitySlider.value = intensity;

        for (int i = 0; i < onValueChangedCount; i++)
        {
            intensitySlider.onValueChanged.SetPersistentListenerState(0, UnityEngine.Events.UnityEventCallState.RuntimeOnly);
        }
    }

    public void ChangeIntensityBySlider()
    {
        deformationManger.CurrentIntensity = (intensitySlider.value);
    }


    public void ChangeSkinTransparency()
    {
        deformationManger.ChangeSkinTransparency(skinTransparencySlider.value);
    }

    
}
