using System;
namespace myBlazorApp.Components;

using System.Reflection.Metadata;
using Blazorise;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using myBlazorApp.Models;

public partial class InventoryItem
{
    [Parameter]
    public string Item { get; set; }

    [Parameter]
    public int Index { get; set; }

    [Parameter]
    public int Number { get; set; }

    [Parameter]
    public int StackSize { get; set; }

    [Parameter]
    public bool NoDrop { get; set; }

    [Parameter]
    public string Image { get; set; }

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

    [CascadingParameter]
    public MyInventory Parent { get; set; }

    internal void OnDragEnter()
    {
        if (NoDrop)
        {
            return;
        }
        Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
    }

    internal void OnDragLeave()
    {
        if (NoDrop)
        {
            return;
        }
        Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
    }

    internal void OnDrop()
    {
        if (Parent.CurrentDragItem != "null")
        {
            if(Parent.CurrentDragIndex == 0)
            {
                this.Number = this.Number + Parent.CurrentDragNumber;
                this.Item = Parent.CurrentDragItem;
                this.StackSize = Parent.CurrentDragStackSize;
                Parent.ItemsInventory[this.Index - 1] = new InventoryListItem(this.Item, this.Index, this.Number, this.StackSize);
            }
            else if (this.Item == Parent.CurrentDragItem)
            {
                this.Number = this.Number + Parent.CurrentDragNumber;
                Parent.CurrentInventoryItem.Number = 0;
                Parent.CurrentInventoryItem.Item = "null";
                Parent.CurrentInventoryItem.StackSize = 0;
                Parent.ItemsInventory[Parent.CurrentInventoryItem.Index - 1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number, Parent.CurrentInventoryItem.StackSize);
                Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = Parent.CurrentInventoryItem.Item, Index = Parent.CurrentInventoryItem.Index, Number = Parent.CurrentInventoryItem.Number, StackSize = Parent.CurrentInventoryItem.StackSize });
            }
            else
            {
                string tmpItem = this.Item;
                int tmpNumber = this.Number;
                int tmpStackSize = this.StackSize;
                Parent.ItemsInventory[Parent.CurrentDragIndex - 1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber, tmpStackSize);
                this.Item = Parent.CurrentDragItem;
                this.Number = Parent.CurrentDragNumber;
                this.StackSize = Parent.CurrentDragStackSize;
                Parent.CurrentInventoryItem.Item = tmpItem;
                Parent.CurrentInventoryItem.Number = tmpNumber;
                Parent.CurrentInventoryItem.StackSize = tmpStackSize;
            }

        }
        Parent.Actions.Add(new InventoryAction { Action = "Drop", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
        
    }

    internal void OnDragEnd()
    {
        if(NoDrop || Parent.CurrentDragItem == "null")
        {
            return;
        }
        if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex != this.Index)
        {
            Parent.ItemsInventory[this.Index-1] = new InventoryListItem(Parent.CurrentDragItem, Parent.CurrentDragIndex, Parent.CurrentDragNumber, Parent.CurrentDragStackSize);
            this.Item = Parent.CurrentDragItem;
            this.Number = Parent.CurrentDragNumber;
            this.StackSize = Parent.CurrentDragStackSize;
        }
        Parent.CurrentDragIndex = -1;
        Parent.CurrentDragItem = "null";
        Parent.CurrentDragNumber = 0;
        Parent.CurrentDragStackSize = 0;
        Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });

    }

    private void OnDragStart()
    {
        Parent.CurrentInventoryItem = this;
        Parent.CurrentDragIndex = this.Index;
        Parent.CurrentDragItem = this.Item;
        Parent.CurrentDragNumber = this.Number;
        Parent.CurrentDragStackSize = this.StackSize;
        this.Item = "null";
        this.Number = 0;
        this.StackSize = 0;
        Parent.Actions.Add(new InventoryAction { Action = "Drag Start", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize});

    }
}

