import {
  ActionsForEquipmentSet,
  ActionsForInventory,
  ActionsForLoot,
  ActionsForMapContainer,
  DepotSocketEvents,
  IItem,
  ItemContainerType,
  ItemSocketEvents,
  ItemSocketEventsDisplayLabels,
  ItemType
} from '@rpg-engine/shared';

export interface IContextMenuItem {
  id: string;
  text: string;
}

const generateContextMenuListOptions = (actionsByTypeList: any) => {
  const contextMenu: IContextMenuItem[] = actionsByTypeList.map(
    (action: string) => {
      return { id: action, text: ItemSocketEventsDisplayLabels[action] };
    }
  );
  return contextMenu;
};

export const generateContextMenu = (
  item: IItem,
  itemContainerType: ItemContainerType | null,
  isDepotSystem?: boolean
) => {
  let contextActionMenu: IContextMenuItem[] = [];

  if (itemContainerType === ItemContainerType.Inventory) {
    switch (item.type) {
      case ItemType.Weapon:
      case ItemType.Armor:
      case ItemType.Accessory:
      case ItemType.Jewelry:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForInventory.Equipment
        );
        break;
      case ItemType.Container:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForInventory.Container
        );
        break;
      case ItemType.Consumable:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForInventory.Consumable
        );
        break;
      case ItemType.CraftingResource:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForInventory.CraftingResource
        );
        break;
      case ItemType.Tool:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForInventory.Tool
        );
        break;

      default:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForInventory.Other
        );
        break;
    }

    if (isDepotSystem) {
      contextActionMenu.push({
        id: DepotSocketEvents.Deposit,
        text: 'Deposit',
      });
    }

    const contextActionMenuDontHaveUseWith = !contextActionMenu.find(action =>
      action.text.toLowerCase().includes('use with')
    );

    if (item.hasUseWith && contextActionMenuDontHaveUseWith) {
      contextActionMenu.push({
        id: ItemSocketEvents.UseWith,
        text: ItemSocketEvents.UseWith,
      });
    }
  }
  if (itemContainerType === ItemContainerType.Equipment) {
    switch (item.type) {
      case ItemType.Container:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForEquipmentSet.Container
        );

        break;
      default:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForEquipmentSet.Equipment
        );
    }
  }
  if (itemContainerType === ItemContainerType.Loot) {
    switch (item.type) {
      case ItemType.Weapon:
      case ItemType.Armor:
      case ItemType.Accessory:
      case ItemType.Jewelry:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForLoot.Equipment
        );
        break;
      case ItemType.Consumable:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForLoot.Consumable
        );
        break;
      case ItemType.CraftingResource:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForLoot.CraftingResource
        );
        break;
      case ItemType.Tool:
        contextActionMenu = generateContextMenuListOptions(ActionsForLoot.Tool);
        break;
      default:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForLoot.Other
        );
        break;
    }
  }
  if (itemContainerType === ItemContainerType.MapContainer) {
    switch (item.type) {
      case ItemType.Weapon:
      case ItemType.Armor:
      case ItemType.Accessory:
      case ItemType.Jewelry:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForMapContainer.Equipment
        );
        break;
      case ItemType.Consumable:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForMapContainer.Consumable
        );
        break;
      case ItemType.CraftingResource:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForMapContainer.CraftingResource
        );
        break;
      case ItemType.Tool:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForMapContainer.Tool
        );
        break;
      default:
        contextActionMenu = generateContextMenuListOptions(
          ActionsForMapContainer.Other
        );
        break;
    }

    const contextActionMenuDontHaveUseWith = !contextActionMenu.find(action =>
      action.text.toLowerCase().includes('use with')
    );

    if (item.hasUseWith && contextActionMenuDontHaveUseWith) {
      contextActionMenu.push({ id: 'use-with', text: 'Use with...' });
    }
  }
  if (itemContainerType === ItemContainerType.Depot) {
    contextActionMenu = [
      {
        id: ItemSocketEvents.GetItemInfo,
        text: ItemSocketEventsDisplayLabels.GetItemInfo,
      },
      { id: DepotSocketEvents.Withdraw, text: 'Withdraw' },
    ];
  }

  if (item.type && item.type === ItemType.Container) {
    const existInContextAction = contextActionMenu.some(
      item => item.text === DepotSocketEvents.OpenContainer
        || item.id === ItemSocketEvents.ContainerOpen
        || item.text === 'Open'
    );

    if (!existInContextAction) {
      contextActionMenu.push({ id: DepotSocketEvents.OpenContainer, text: 'Open' });
    }

    if (itemContainerType === ItemContainerType.Inventory) {
      if (item.textureAtlas === 'items' && item.type === ItemType.Container) {
        contextActionMenu.push({
          id: ItemSocketEvents.ItemPropertyUpdate,
          text: 'Set Color...'
        });
      }
    }
  }

  if (item.attachedGems && item.attachedGems.length > 0) {
    contextActionMenu.push({ id: ItemSocketEvents.DetachGem, text: "Detach Gems" });
  }

  return contextActionMenu;
};
