﻿using OKHOSTING.UI.Controls;
using System;
using System.Threading.Tasks;

namespace OKHOSTING.UI.Lara.Controls
{
    public class CheckBox : Control, ICheckBox, IInputControl<bool>
    {
        public CheckBox() : base("input")
        {
            InnerElement.SetAttribute("type", "checkbox");
            InnerElement.On("click", () => 
            {
                return OnValueChangedAsync();
            });
        }

        public bool _Value;

        public bool Value 
        { 
            get
            {
                if (_Value != bool.Parse(InnerElement.GetAttribute("value")))
                {
                    InnerElement.SetAttribute("value", _Value.ToString());
                }

                return _Value;
            }
            set
            {
                _Value = value;
                InnerElement.SetAttribute("value", value.ToString());
            }
        }

        public event EventHandler<bool> ValueChanged;

        protected async Task OnValueChangedAsync()
        {
            ValueChanged?.Invoke(this, ((ICheckBox)this).Value);
            await Task.CompletedTask;
        }
    }
}
