using System;
using BlazorApp.Models;
using BlazorApp.Components;
using Microsoft.AspNetCore.Components;

namespace BlazorApp.Components
{
	public partial class InventoryItem
	{
        [Parameter]
        public int Index { get; set; }

        [Parameter]
        public Item Item { get; set; }

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

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

        internal void OnDragEnter()
        {
            if (NoDrop)
            {
                return;
            }

        }

        internal void OnDragLeave()
        {
            if (NoDrop)
            {
                return;
            }

        }

        internal void OnDrop()
        {
            if (NoDrop)
            {
                return;
            }

            if (Parent.CurrentDragItem != null)
            {
                Item tmp = this.Item;
                this.Item = Parent.CurrentDragItem;
                Parent.InventoryItems[this.Index] = this.Item;
                Parent.CurrentDragItem = tmp;
                Parent.CurrentDragIndex = this.Index;
            }
            Parent.Actions.Add(new CraftingAction { Action = "On Drop", Item = this.Item, Index = this.Index });

        }

        private void OnDragStart()
        {

            Parent.CurrentDragItem = this.Item;
            if (this.Item != null)
            {
                if (!NoDrop)
                {
                    Parent.CurrentDragIndex = this.Index;
                    this.Item = null;
                    Parent.InventoryItems[Index] = null; 
                }
                else
                {
                    Parent.CurrentDragIndex = -1;
                }

            }
            Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index });

        }

        internal void OnDragEnd()
        {
            if (NoDrop || Parent.CurrentDragItem==null)
            {
                return;
            }
            if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!=this.Index)
            {
                Parent.InventoryItems[this.Index] = this.Item;
                this.Item = Parent.CurrentDragItem;
            }

            Parent.Actions.Add(new CraftingAction { Action = "Drag End", Item = this.Item, Index = this.Index });

            Parent.CurrentDragIndex = -1;
            Parent.CurrentDragItem = null;

        }
    }
}

