using Billing.Themes;
using Billing.UI;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace Billing.Views
{
    public partial class SettingPage : BillingPage
    {
        private static readonly BindableProperty VersionProperty = BindableProperty.Create(nameof(Version), typeof(string), typeof(SettingPage));
        private static readonly BindableProperty RedProperty = BindableProperty.Create(nameof(Red), typeof(byte), typeof(SettingPage));
        private static readonly BindableProperty GreenProperty = BindableProperty.Create(nameof(Green), typeof(byte), typeof(SettingPage));
        private static readonly BindableProperty BlueProperty = BindableProperty.Create(nameof(Blue), typeof(byte), typeof(SettingPage));

        public string Version => (string)GetValue(VersionProperty);
        public byte Red
        {
            get => (byte)GetValue(RedProperty);
            set => SetValue(RedProperty, value);
        }
        public byte Green
        {
            get => (byte)GetValue(GreenProperty);
            set => SetValue(GreenProperty, value);
        }
        public byte Blue
        {
            get => (byte)GetValue(BlueProperty);
            set => SetValue(BlueProperty, value);
        }

        public SettingPage()
        {
            InitializeComponent();

            var (main, build) = Definition.GetVersion();
            SetValue(VersionProperty, $"{main} ({build})");
        }

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

            //SetValue(VersionProperty, $"{AppInfo.VersionString} ({AppInfo.BuildString})");
            var colorString = Preferences.Get(Definition.PrimaryColorKey, "#183153");
            var color = Color.FromHex(colorString);
            Red = (byte)(color.R * 255);
            Green = (byte)(color.G * 255);
            Blue = (byte)(color.B * 255);
        }

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

            var color = Color.FromRgb(Red, Green, Blue);
            Preferences.Set(Definition.PrimaryColorKey, color.ToHex());
            Light.Instance.RefreshColor(color);
        }
    }
}