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

public class MapParameterControllerSprite : MapParameterController
{
    public Sprite[] sprites;

    Slider valueSlider;

    int currentGoalValue;
    int currentValue;

    protected override void Awake()
    {
        base.Awake();

        valueSlider = GetComponentInChildren<Slider>();
        valueSlider.minValue = 0f;
        valueSlider.maxValue = sprites.Length - 1;
        valueSlider.wholeNumbers = true;
        valueSlider.onValueChanged.AddListener(OnValueChanged);
        valueSlider.interactable = false;
    }

    public override void ActivateManualControl(bool manual)
    {
        base.ActivateManualControl(manual);
        valueSlider.interactable = manual;
    }

    void OnValueChanged(float newValue)
    {
        currentValue = (int) newValue;
        AssignValue(MapParametersManager.Instance.playerQuad, currentValue);
        CheckNewValue();
    }

    public override void Init()
    {
        valueSlider.value = valueSlider.minValue;
    }

    protected override void SelectRandomValue(MeshRenderer resultQuad)
    {
        while(IsCorrect())
        {
            currentGoalValue = UnityEngine.Random.Range(0, sprites.Length);
        }
        AssignValue(resultQuad, currentGoalValue);
    }

    protected override void AutoUpdate()
    {
        currentValue++;
        currentValue %= sprites.Length;
        valueSlider.value = currentValue;
    }

    void AssignValue(MeshRenderer quad, int value)
    {
        quad.material.SetTexture(shaderParameterName, sprites[value].texture);
    }

    public override bool IsCorrect()
    {
        return currentValue == currentGoalValue;
    }
}
