using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Blazored.Modal;
using Blazored.Modal.Services;
using Blazorise;
using Blazorise.DataGrid;
using Blazorise.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using myBlazorApp.Components;
using myBlazorApp.Factories;
using myBlazorApp.Modals;
using myBlazorApp.Models;
using myBlazorApp.Services;
using IModalService = Blazored.Modal.Services.IModalService;

namespace myBlazorApp.Pages
{
	public partial class Inventory 
    {
        public List<InventoryListItem> itemsInv = new List<InventoryListItem>();

        private List<Item> items = new List<Item>();

        private List<Item> full = new List<Item>();

        private List<Item> choix = new List<Item>();

        public string SearchText = "";

        private int totalItem;

        private int currentPage;

        private int pageSize;

        List<Item> Filtered = new List<Item>();

        List<Item> Sorted = new List<Item>();

        [Inject]
        public IDataService DataService { get; set; }

        [Inject]
        public IWebHostEnvironment WebHostEnvironment { get; set; }

        [Inject]
        public NavigationManager NavigationManager { get; set; }

        [CascadingParameter]
        public IModalService Modal { get; set; }

        private bool isSorted = false;


        private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
        {

            if (e.CancellationToken.IsCancellationRequested)
            {
                return;
            }

            if (!e.CancellationToken.IsCancellationRequested)
            {
                totalItem = await DataService.Count();
                items = await DataService.List(e.Page, e.PageSize);
                full = await DataService.List(e.Page, totalItem);
                itemsInv = await DataService.GetInventoryItems();
                currentPage = e.Page;
                pageSize = e.PageSize;
                if(isSorted==false)
                {
                    choix = items;
                }
                else
                {
                    choix = SortList();
                    return;
                }
                if (SearchText.IsNullOrEmpty())
                {
                    choix = items;
                }
                else
                {
                    choix = choseList();
                    return;
                }
                StateHasChanged();
            }
        }

        private List<Item> choseList()
        {
            if (SearchText.IsNullOrEmpty())
            {
                choix = items;
                totalItem = full.Count();
                NavigationManager.NavigateTo("inventory", false);
            }
            else
            {
                if (Filtered.Count() < (currentPage - 1) * 10 + pageSize)
                {
                    pageSize = Filtered.Count() - (currentPage - 1) * 10;
                }
                choix = Filtered.GetRange((currentPage - 1) * 10, pageSize);
                totalItem = Filtered.Count();
            }
            StateHasChanged();
            NavigationManager.NavigateTo("inventory", false);
            return choix;
        }

        private void inputValue()
        {
            Filtered = full.Where(
               itm => itm.DisplayName.ToLower().Contains(SearchText.ToLower())).ToList();
            choseList();
        }

        private void OnPress()
        {
            if (isSorted == true)
            {
                isSorted = false;
            }
            else isSorted = true;
            SortList();
        }

        private List<Item> SortList()
        {
            if (isSorted == false)
            {
                choix = items;
                NavigationManager.NavigateTo("inventory", true);
            }
            else
            {
                if(Sorted.IsNullOrEmpty())
                {
                    Sorted = full;
                }
                Sorted.Sort((x, y) => string.Compare(x.DisplayName, y.DisplayName));
                if (Sorted.Count() < (currentPage - 1) * 10 + pageSize)
                {
                    pageSize = Sorted.Count() - (currentPage - 1) * 10;
                }
                choix = Sorted.GetRange((currentPage - 1) * 10, pageSize);
            }
            return choix;
        }
    }
}

