using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Blazorise.DataGrid;
using Blazorise.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using myBlazorApp.Models;
using myBlazorApp.Services;
using Syncfusion.Blazor.Grids;

namespace myBlazorApp.Components
{
    public partial class MyInventory
    {
        [Parameter]
        public List<InventoryListItem> ItemsInventory { get; set; } = new List<InventoryListItem>();

        public int index { get; set; } = 0;

        public ObservableCollection<InventoryAction> Actions { get; set; }

        public string CurrentDragItem { get; set; }
        public int CurrentDragIndex { get; set; }
        public int CurrentDragNumber { get; set; }
        public int CurrentDragStackSize { get; set; }
        public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem();


        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; }

        private bool isSorted = false;

        public MyInventory()
        {
            Actions = new ObservableCollection<InventoryAction>();
            Actions.CollectionChanged += OnActionsCollectionChanged;
            
        }

        [Inject]
        public IStringLocalizer<MyInventory> Localizer { get; set; }


        [Inject]
        internal IJSRuntime JavaScriptRuntime { get; set; }

        private async void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems);
            if (this.Actions.Last().Action == "End" || this.Actions.Last().Action == "Drop" || this.Actions.Last().Action == "Start")
            {
                    string[] infos = new string[] { this.Actions.Last().ItemName, this.Actions.Last().Index.ToString(), this.Actions.Last().Number.ToString(), this.Actions.Last().StackSize.ToString() };
                    await DataService.UpdateInventory(infos);      
            }     
        }



        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);
                ItemsInventory = 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;
        }
    }
}

