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

public class MapParameterControllerColor : MapParameterController
{
    public Image previewImage;

    public Slider rSlider;
    public Slider gSlider;
    public Slider bSlider;

    Color currentGoalValue;
    Color currentValue;

    const float maxValue = 2f;

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

        rSlider.minValue = 0f;
        rSlider.maxValue = maxValue;
        rSlider.onValueChanged.AddListener(OnRValueChanged);

        gSlider.minValue = 0f;
        gSlider.maxValue = maxValue;
        gSlider.onValueChanged.AddListener(OnGValueChanged);

        bSlider.minValue = 0f;
        bSlider.maxValue = maxValue;
        bSlider.onValueChanged.AddListener(OnBValueChanged);

        rSlider.interactable = false;
        gSlider.interactable = false;
        bSlider.interactable = false;

        currentValue.a = 1f;
        currentGoalValue.a = 1f;
    }

    public override void ActivateManualControl(bool manual)
    {
        base.ActivateManualControl(manual);
        rSlider.interactable = manual;
        gSlider.interactable = manual;
        bSlider.interactable = manual;
    }

    void OnRValueChanged(float newValue)
    {
        currentValue.r = newValue / maxValue;
        OnNewColor();
    }
    void OnGValueChanged(float newValue)
    {
        currentValue.g = newValue / maxValue;
        OnNewColor();
    }
    void OnBValueChanged(float newValue)
    {
        currentValue.b = newValue / maxValue;
        OnNewColor();
    }

    void OnNewColor()
    {
        AssignValue(MapParametersManager.Instance.playerQuad, currentValue);
        CheckNewValue();
        previewImage.color = currentValue;
    }

    public override void Init()
    {
        rSlider.value = 0;
        gSlider.value = 0;
        bSlider.value = 0;
    }

    protected override void SelectRandomValue(MeshRenderer resultQuad)
    {
        for(int i = 0; i < 3; i++)
        {
            float randomValue = UnityEngine.Random.Range(0f, 1f);
            randomValue *= maxValue;
            randomValue = ((float) Math.Round(randomValue)) / maxValue;
            if(i == 0)
                currentGoalValue.r = randomValue;
            else if(i == 1)
                currentGoalValue.g = randomValue;
            else 
                currentGoalValue.b = randomValue;
        }
        AssignValue(resultQuad, currentGoalValue);
    }

    void AssignValue(MeshRenderer quad, Color value)
    {
        quad.material.SetColor(shaderParameterName, value);
    }

    protected override void AutoUpdate()
    {
        float randomValue = UnityEngine.Random.Range(0f, 1f);
        randomValue *= maxValue;
        randomValue = ((float) Math.Round(randomValue));
        if(currentValue.r != currentGoalValue.r)
            rSlider.value = randomValue;
        else if(currentValue.g != currentGoalValue.g)
            gSlider.value = randomValue;
        else
            bSlider.value = randomValue;
    }

    public override bool IsCorrect()
    {
        return Math.Abs(currentValue.r - currentGoalValue.r) < 0.05f &&
            Math.Abs(currentValue.g - currentGoalValue.g) < 0.05f &&
            Math.Abs(currentValue.b - currentGoalValue.b) < 0.05f;
    }
}
