using Http.Core;
using PCUT.Entities;
using PCUT.Models;
using System;
using System.Threading.Tasks;
using Http.Core.Extensions;
using static Http.Core.Constants.HttpConstants;
using Http.Core.Contexts;
using Windows.UI.Xaml;

namespace PCUT.ViewModels
{
    public class MainMenuPageViewModel : NotificationBase
    {
        private string _userName;
        public string UserName
        {
            get => _userName;
            set => SetProperty(ref _userName, value);
        }

        private string _expiry;
        public string Expiry
        {
            get => _expiry;
            set => SetProperty(ref _expiry, value);
        }

        private bool _isUserRoleAdmin = false;
        public bool IsUserRoleAdmin
        {
            get => _isUserRoleAdmin;
            set
            {
                if (_isUserRoleAdmin != value)
                {
                    _isUserRoleAdmin = value;
                    RaisePropertyChanged(nameof(IsUserRoleAdmin));
                }
            }
        }

        private bool _isMenuEnabled = false;
        public bool IsMenuEnabled
        {
            get => _isMenuEnabled;
            set
            {
                SetProperty(ref _isMenuEnabled, value);
            }
        }

        private Visibility _isMenuDisplay = Visibility.Collapsed;
        public Visibility IsMenuDisplay
        {
            get => _isMenuDisplay;
            set
            {
                if (_isMenuDisplay != value)
                {
                    SetProperty(ref _isMenuDisplay, value);
                }
            }
        }

        public SelectableCollection<Category> Categories { get; }

        public MainMenuPageViewModel()
        {
            IsUserRoleAdmin = UserContext.Instance.Profile?.Role?.Equals("admin", StringComparison.OrdinalIgnoreCase) ?? false;
            Categories = new SelectableCollection<Category>(new Category { Name = "-None-" });
        }

        public void ToggleMenuDisplay()
        {
            IsMenuDisplay = IsMenuDisplay == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
        }

        public void HideMenuDisplay()
        {
            IsMenuDisplay = Visibility.Collapsed;
        }

        public async Task GetCategoriesAsync()
        {
            using (var client = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                var categories = await client.GetCategoriesAsync(filterByRole: true);
                Categories.Load(categories);
            }
        }

        public string DataCenterSearchValue { get; set; }
    }
}
