import type { BlockType } from './def'
import type { InventoryItem } from '../util/usePlayer'

export type ItemQuality = 'wood' | 'iron' | 'silver' | 'gold' | 'diamond'
export type ItemType = 'tool' | 'weapon' | 'block' | 'ore'

export interface Item {
  name: string
  type: ItemType
  icon: string
  hasQuality: boolean
}

export const items: Item[] = [
  { name: 'Shovel', type: 'tool', icon: 'shovel', hasQuality: true },
  { name: 'Pick Axe', type: 'tool', icon: 'pick', hasQuality: true },
  { name: 'Sword', type: 'weapon', icon: 'sword', hasQuality: true },

  { name: 'leaves', type: 'block', icon: 'leaves', hasQuality: false },
  { name: 'dirt', type: 'block', icon: 'dirt', hasQuality: false },
  { name: 'wood', type: 'block', icon: 'wood', hasQuality: false },
  { name: 'stone', type: 'block', icon: 'stone', hasQuality: false },
  { name: 'gravel', type: 'block', icon: 'stone', hasQuality: false }, // TODO

  { name: 'coal', type: 'ore', icon: 'ore_coal', hasQuality: false },
  { name: 'iron', type: 'ore', icon: 'ore_iron', hasQuality: false },
  { name: 'silver', type: 'ore', icon: 'ore_silver', hasQuality: false },
  { name: 'gold', type: 'ore', icon: 'ore_gold', hasQuality: false },
  { name: 'ruby', type: 'ore', icon: 'ore_ruby', hasQuality: false },
  { name: 'diamond', type: 'ore', icon: 'ore_diamond', hasQuality: false },
  { name: 'emerald', type: 'ore', icon: 'ore_emerald', hasQuality: false },
]

export const damage: Record<ItemQuality, number> = {
  wood: 1,
  iron: 2,
  silver: 3,
  gold: 5,
  diamond: 8,
}

export function getItem(name: string, quality = null) {
  const item = items.find(i => i.name === name)
  if (item) {
    return {
      ...item,
      quality,
    }
  }
}

export function getItemClass(item: InventoryItem) {
  if (item.quality) return `${item.type}-${item.icon}-${item.quality}`
  return `${item.type}-${item.icon}`
}
