@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@inject IStringLocalizer<TriStateCheckBox> Localizer 

<img src="@_src" title="@_title" @onclick="SetValue" />

@code {
    private bool? _value = null;
    private string _title;
    private string _src = string.Empty;

    [Parameter]
    public bool? Value { get; set; }

    [Parameter]
    public bool Disabled { get; set; }

    [Parameter]
    public Action<bool?> OnChange { get; set; }

    protected override void OnInitialized()
    {
        _value = Value;
        SetImage();
    }

    private void SetValue()
    {
        if (!Disabled)
        {
            switch (_value)
            {
                case true:
                    _value = false;
                    break;
                case false:
                    _value = null;
                    break;
                case null:
                    _value = true;
                    break;
            }

            SetImage();
            OnChange(_value);
        }
    }

    private void SetImage()
    {
        switch (_value)
        {
            case true:
                _src = "images/checked.png";
                _title = Localizer["Permission Granted"];
                break;
            case false:
                _src = "images/unchecked.png";
                _title = Localizer["Permission Denied"];
                break;
            case null:
                _src = "images/null.png";
                _title = string.Empty;
                break;
        }

        StateHasChanged();
    }
}
