using System;
using UnityEngine.Events;

[Serializable]
public class ButtonPad
{
    public bool redButtonState;
    public bool greenButtonState;
    public bool whiteButtonState;
    public bool yellowButtonState;

    public EventHandler<bool> RedButtonStateChange;
    public EventHandler<bool> GreenButtonStateChange;
    public EventHandler<bool> WhiteButtonStateChange;
    public EventHandler<bool> YellowButtonStateChange;


    public UnityEvent allButtonsDownMechanic;
    public UnityEvent diagonalButtonsDownMechanic_RedYellow_03;
    public UnityEvent diagonalButtonsDownMechanic_GreenWhite_12;
    public UnityEvent parallelButtonsDownMechanic_RedGreen_01;
    public UnityEvent parallelButtonsDownMechanic_WhiteYellow_23;
    public bool AreAllButtonsPressed()
    {
        return redButtonState & greenButtonState & whiteButtonState & yellowButtonState;
    }


    public bool AreDiagonalButtonsPressed_RedYellow()
    {
        return redButtonState & yellowButtonState;
    }

    public bool AreParallelButtonsPressed_RedGreen()
    {
        return redButtonState & greenButtonState;
    }

    public bool AreParallelButtonsPressed_RedWhite()
    {
        return redButtonState & whiteButtonState;
    }

    public bool AreDiagonalButtonsPressed_GreenWhite()
    {
        return whiteButtonState & greenButtonState;
    }
    public bool AreParallelButtonsPressed_GreenYellow()
    {
        return greenButtonState & yellowButtonState;
    }

    public bool AreAllButtonsFree()
    {
        return (!redButtonState && !greenButtonState && !whiteButtonState && !yellowButtonState);
    }

    public void SetButtonState(int i, bool value)
    {
        switch (i)
        {
            case 0:
                redButtonState = value;
                RedButtonStateChange.Invoke(this, redButtonState);
                break;
            case 1:
                greenButtonState = value;
                GreenButtonStateChange.Invoke(this, greenButtonState);
                break;
            case 2:
                whiteButtonState = value;
                WhiteButtonStateChange.Invoke(this, whiteButtonState);
                break;
            default:
                yellowButtonState = value;
                YellowButtonStateChange.Invoke(this, yellowButtonState);
                break;
        }

        CombinationButtonChangeCheck();
    }

    public void CombinationButtonChangeCheck()
    {
        if (AreAllButtonsPressed())
            allButtonsDownMechanic.Invoke();

        if (AreDiagonalButtonsPressed_RedYellow())
            diagonalButtonsDownMechanic_RedYellow_03.Invoke();
        if (AreDiagonalButtonsPressed_GreenWhite())
            diagonalButtonsDownMechanic_GreenWhite_12.Invoke();
    }
}
