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

public class MapParameterControllerColorButtons : MapParameterController
{
    public Image previewImage;

    public Button rButton;
    public Button gButton;
    public Button bButton;

    public MeshRenderer rMeshRenderer;
    public MeshRenderer gMeshRenderer;
    public MeshRenderer bMeshRenderer;

    //public ColorBlock activatedColors;
    //public ColorBlock offColors;

    Color currentGoalValue;
    Color currentValue;

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

        rButton.interactable = false;
        gButton.interactable = false;
        bButton.interactable = false;

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

    public override void ActivateManualControl(bool manual)
    {
        base.ActivateManualControl(manual);
        rButton.interactable = manual;
        gButton.interactable = manual;
        bButton.interactable = manual;
    }

    public void OnRButton()
    {
        currentValue.r = 1f - currentValue.r;
        //rButton.colors = currentValue.r > 0 ? activatedColors : offColors;
        rMeshRenderer.material = currentValue.r > 0 ? GameUtils.Instance.lightGreenMaterial : GameUtils.Instance.lightOffMaterial;
        OnNewColor();
    }
    public void OnGButton()
    {
        currentValue.g = 1f - currentValue.g;
        //gButton.colors = currentValue.g > 0 ? activatedColors : offColors;
        gMeshRenderer.material = currentValue.g > 0 ? GameUtils.Instance.lightGreenMaterial : GameUtils.Instance.lightOffMaterial;
        OnNewColor();
    }
    public void OnBButton()
    {
        currentValue.b = 1f - currentValue.b;
        //bButton.colors = currentValue.b > 0 ? activatedColors : offColors;
        bMeshRenderer.material = currentValue.b > 0 ? GameUtils.Instance.lightGreenMaterial : GameUtils.Instance.lightOffMaterial;
        OnNewColor();
    }

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

    public override void Init()
    {
    }

    protected override void SelectRandomValue(MeshRenderer resultQuad)
    {
        while(IsCorrect())
        {
            currentGoalValue.r = UnityEngine.Random.Range(0f, 1f) < 0.4 ? 0f : 1f;
            currentGoalValue.g = UnityEngine.Random.Range(0f, 1f) < 0.4 ? 0f : 1f;
            currentGoalValue.b = UnityEngine.Random.Range(0f, 1f) < 0.4 ? 0f : 1f;
        }
        AssignValue(resultQuad, currentGoalValue);
    }

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

    protected override void AutoUpdate()
    {
        if(currentValue.r != currentGoalValue.r)
            OnRButton();
        else if(currentValue.g != currentGoalValue.g)
            OnGButton();
        else
            OnBButton();
    }

    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;
    }
}
