import { ItemCategory } from "./itemCategories.js";
import { MaterialTier, GatherMaterialId } from "./materialTiers.js";
import { StatusEffectType } from "../statusEffects/rules.js";

const DEFAULT_STACK = 99;

/** Default spark charges on a fresh tinderbox. */
export const TINDERBOX_MAX_CHARGES = 6;

/**
 * Reserved numeric IDs for gathering, crafting, and cooking content (9–25).
 * Metal ores/bars/pickaxes above bronze live in metalItems.js (35+).
 * IDs 11 / 12 / 25 are cotton fibre / yarn / cloth (see clothItems.js).
 * Burnt venison uses 280; ash tiers live in woodItems.js (281+).
 */
export const CraftingItemId = Object.freeze({
	tinderbox: 10,
	copperOre: 13,
	bronzeBar: 14,
	/** Uncooked Venison */
	rawMeat: 15,
	/** Cooked Venison */
	cookedMeat: 16,
	leather: 17,
	arrowTip: 18,
	arrow: 19,
	bronzeAxe: 20,
	bronzePickaxe: 21,
	tinOre: 53,
	burntMeat: 280,
	rawMutton: 506,
	cookedMutton: 507,
	burntMutton: 508,
	rawGoat: 509,
	cookedGoat: 510,
	burntGoat: 511,
	/** Mined rock salt (ash sprite, rock-salt tint). */
	salt: 589
});

/** Magenta cookable base; tinted per cook state. */
const VENISON_COOK_COLORS = Object.freeze({
	raw: Object.freeze({ r: 0.72, g: 0.28, b: 0.32 }),
	cooked: Object.freeze({ r: 0.55, g: 0.32, b: 0.18 }),
	burnt: Object.freeze({ r: 0.18, g: 0.14, b: 0.12 })
});

const MUTTON_COOK_COLORS = Object.freeze({
	raw: Object.freeze({ r: 0.78, g: 0.36, b: 0.4 }),
	cooked: Object.freeze({ r: 0.62, g: 0.36, b: 0.22 }),
	burnt: Object.freeze({ r: 0.2, g: 0.15, b: 0.12 })
});

const GOAT_COOK_COLORS = Object.freeze({
	raw: Object.freeze({ r: 0.68, g: 0.34, b: 0.38 }),
	cooked: Object.freeze({ r: 0.5, g: 0.3, b: 0.2 }),
	burnt: Object.freeze({ r: 0.16, g: 0.13, b: 0.11 })
});

/**
 * Fresh tinderbox instance payload (charges, not potion fill 1–5).
 * @param {number} [charges]
 */
export function tinderboxItemInstance (charges = TINDERBOX_MAX_CHARGES)
{
	return Object.freeze({
		kind: "tinderbox",
		charges: Math.max(0, Math.min(TINDERBOX_MAX_CHARGES, charges | 0 || TINDERBOX_MAX_CHARGES))
	});
}

function stub (itemId, key, displayName, category, extra = {})
{
	return Object.freeze({
		itemId,
		key,
		displayName,
		description: extra.description ?? "An unfinished good awaiting a proper name.",
		seedValue: extra.seedValue ?? 1,
		stackable: extra.stackable ?? true,
		maxStackSize: extra.maxStackSize ?? DEFAULT_STACK,
		uniqueInstance: false,
		category,
		stub: true,
		iconKey: extra.iconKey ?? "paper",
		...extra
	});
}

/** Minimal defs so recipes and the craft UI can resolve names/icons before art exists. */
export const CRAFTING_ITEM_STUBS = Object.freeze({
	[CraftingItemId.tinderbox]: Object.freeze({
		itemId: CraftingItemId.tinderbox,
		key: "tinderbox",
		displayName: "Tinderbox",
		description: "A flint and steel kit for coaxing sparks onto dry wood.",
		seedValue: 8,
		stackable: false,
		maxStackSize: 1,
		uniqueInstance: true,
		category: ItemCategory.tool,
		iconKey: "tinderbox",
		defaultState: "tinderbox",
		/** Sheet: 3×2 of 16px — frame index matches remaining uses (1…6). */
		frames: 6,
		framesPerRow: 3,
		maxCharges: TINDERBOX_MAX_CHARGES,
		/** Show tinderbox-equip while lighting a ground log (not worn gear). */
		gatherEquipSprite: true,
		npcShopAllowed: true,
		defaultInstance: tinderboxItemInstance()
	}),
	[CraftingItemId.copperOre]: Object.freeze({
		itemId: CraftingItemId.copperOre,
		key: "copper-ore",
		displayName: "Copper ore",
		description: "Reddish copper ore. Smelt with tin at a furnace into bronze bars.",
		seedValue: 5,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.ingredient,
		iconKey: "metal",
		defaultState: "ore",
		tier: GatherMaterialId.copper,
		tintFromTier: true
	}),
	[CraftingItemId.tinOre]: Object.freeze({
		itemId: CraftingItemId.tinOre,
		key: "tin-ore",
		displayName: "Tin ore",
		description: "Pale tin ore. Smelt with copper at a furnace into bronze bars.",
		seedValue: 5,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.ingredient,
		iconKey: "metal",
		defaultState: "ore",
		tier: GatherMaterialId.tin,
		tintFromTier: true
	}),
	[CraftingItemId.bronzeBar]: Object.freeze({
		itemId: CraftingItemId.bronzeBar,
		key: "bronze-bar",
		displayName: "Bronze bar",
		description: "A solid bronze bar, ready for the anvil.",
		seedValue: 4,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.intermediate,
		iconKey: "metal",
		defaultState: "bar",
		tier: MaterialTier.bronze,
		tintFromTier: true
	}),
	[CraftingItemId.rawMeat]: Object.freeze({
		itemId: CraftingItemId.rawMeat,
		key: "uncooked-venison",
		displayName: "Uncooked Venison",
		description: "Fresh deer meat. Cook at an oven or campfire.",
		seedValue: 3,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.ingredient,
		iconKey: "meat",
		defaultState: "raw",
		cookable: true,
		cookState: "raw",
		cookedItemId: CraftingItemId.cookedMeat,
		burntItemId: CraftingItemId.burntMeat,
		tint: VENISON_COOK_COLORS.raw
	}),
	[CraftingItemId.cookedMeat]: Object.freeze({
		itemId: CraftingItemId.cookedMeat,
		key: "cooked-venison",
		displayName: "Cooked Venison",
		description: "Roasted deer meat, still warm from the fire.",
		seedValue: 4,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.food,
		iconKey: "meat",
		defaultState: "cooked",
		cookable: true,
		cookState: "cooked",
		tint: VENISON_COOK_COLORS.cooked,
		healing: 5,
		eatStatusEffect: Object.freeze({
			type: StatusEffectType.healthRegen,
			amountPerSecond: 1,
			durationMs: 5_000
		})
	}),
	[CraftingItemId.burntMeat]: Object.freeze({
		itemId: CraftingItemId.burntMeat,
		key: "burnt-venison",
		displayName: "Burnt Venison",
		description: "Charred beyond saving — little more than a bitter lump.",
		seedValue: 1,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.food,
		iconKey: "meat",
		defaultState: "burnt",
		cookable: true,
		cookState: "burnt",
		tint: VENISON_COOK_COLORS.burnt
	}),
	[CraftingItemId.rawMutton]: Object.freeze({
		itemId: CraftingItemId.rawMutton,
		key: "uncooked-mutton",
		displayName: "Uncooked Mutton",
		description: "Fresh sheep meat. Cook at an oven or campfire.",
		seedValue: 3,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.ingredient,
		iconKey: "meat",
		defaultState: "raw",
		cookable: true,
		cookState: "raw",
		cookedItemId: CraftingItemId.cookedMutton,
		burntItemId: CraftingItemId.burntMutton,
		tint: MUTTON_COOK_COLORS.raw
	}),
	[CraftingItemId.cookedMutton]: Object.freeze({
		itemId: CraftingItemId.cookedMutton,
		key: "cooked-mutton",
		displayName: "Cooked Mutton",
		description: "Roasted mutton with a hearty, pastoral flavour.",
		seedValue: 4,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.food,
		iconKey: "meat",
		defaultState: "cooked",
		cookable: true,
		cookState: "cooked",
		tint: MUTTON_COOK_COLORS.cooked,
		healing: 5,
		eatStatusEffect: Object.freeze({
			type: StatusEffectType.healthRegen,
			amountPerSecond: 1,
			durationMs: 5_000
		})
	}),
	[CraftingItemId.burntMutton]: Object.freeze({
		itemId: CraftingItemId.burntMutton,
		key: "burnt-mutton",
		displayName: "Burnt Mutton",
		description: "Blackened mutton — tough and bitter.",
		seedValue: 1,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.food,
		iconKey: "meat",
		defaultState: "burnt",
		cookable: true,
		cookState: "burnt",
		tint: MUTTON_COOK_COLORS.burnt
	}),
	[CraftingItemId.rawGoat]: Object.freeze({
		itemId: CraftingItemId.rawGoat,
		key: "uncooked-goat",
		displayName: "Uncooked Goat",
		description: "Lean goat meat. Cook at an oven or campfire.",
		seedValue: 4,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.ingredient,
		iconKey: "meat",
		defaultState: "raw",
		cookable: true,
		cookState: "raw",
		cookedItemId: CraftingItemId.cookedGoat,
		burntItemId: CraftingItemId.burntGoat,
		tint: GOAT_COOK_COLORS.raw
	}),
	[CraftingItemId.cookedGoat]: Object.freeze({
		itemId: CraftingItemId.cookedGoat,
		key: "cooked-goat",
		displayName: "Cooked Goat",
		description: "Roasted goat, lean and savoury.",
		seedValue: 5,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.food,
		iconKey: "meat",
		defaultState: "cooked",
		cookable: true,
		cookState: "cooked",
		tint: GOAT_COOK_COLORS.cooked,
		healing: 6,
		eatStatusEffect: Object.freeze({
			type: StatusEffectType.healthRegen,
			amountPerSecond: 1,
			durationMs: 5_000
		})
	}),
	[CraftingItemId.burntGoat]: Object.freeze({
		itemId: CraftingItemId.burntGoat,
		key: "burnt-goat",
		displayName: "Burnt Goat",
		description: "Overcooked goat — dry and charred.",
		seedValue: 1,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.food,
		iconKey: "meat",
		defaultState: "burnt",
		cookable: true,
		cookState: "burnt",
		tint: GOAT_COOK_COLORS.burnt
	}),
	[CraftingItemId.salt]: Object.freeze({
		itemId: CraftingItemId.salt,
		key: "rock-salt",
		displayName: "Rock salt",
		description: "Coarse crystals mined from salt rock, sharp on the tongue.",
		seedValue: 2,
		stackable: true,
		maxStackSize: DEFAULT_STACK,
		uniqueInstance: false,
		category: ItemCategory.ingredient,
		iconKey: "ash",
		defaultState: "ash",
		tier: GatherMaterialId.salt,
		tintFromTier: true,
		npcShopAllowed: true
	})
});
