import { world, EquipmentSlot } from "@minecraft/server";

//////////////////////////////////////////////

// Weapon List
const weaponDamages = {
	"tool:luminite_double_sword": 8,
	"sword:netherite_double_sword": 7,
	"sword:topaz_double_sword": 6,
	"sword:titanium_double_sword": 5,
	"sword:surge_double_sword": 11,
	"sword:pyro_double_sword": 11,
	"sword:ascendant_double_sword": 11,
	"sword:exodium_double_sword": 11,
	"sword:staballoy_double_sword": 5,
	"sword:rose_gold_double_sword": 4,
	"sword:scorpion_double_sword": 5,
	"sword:sapphire_double_sword": 6,
	"sword:ruby_double_sword": 7,
	"sword:souling_gold_double_sword": 6,
	"sword:resin_dipped_double_sword": 6,
	"sword:platinum_double_sword": 7,
	"sword:palladium_double_sword": 9,
	"sword:obsidian_double_sword": 6,
	"sword:nether_quartz_double_sword": 6,
	"sword:molybdenum_double_sword": 6,
	"sword:lapis_double_sword": 5,
	"sword:kyanite_double_sword": 6,
	"sword:jasper_double_sword": 7,
	"sword:ice_double_sword": 4,
	"sword:ghost_crystal_double_sword": 6,
	"sword:echo_shard_double_sword": 7,
	"sword:dragonite_double_sword": 9,
	"sword:deepslate_double_sword": 3,
	"sword:cobalt_double_sword": 5,
	"sword:citrine_double_sword": 6,
	"sword:bronze_double_sword": 5,
	"sword:brimstone_double_sword": 9,
	"sword:bone_double_sword": 4,
	"sword:blushie_double_sword": 6,
	"sword:blackstone_double_sword": 4,
	"sword:aurora_double_sword": 6,
	"sword:astral_double_sword": 9,
	"sword:astral_monolith_double_sword": 6,
	"sword:arkose_double_sword": 5,
	"sword:amethyst_double_sword": 7,
	"sword:amber_double_sword": 6,
	"sword:aetherium_double_sword": 6,
	"sword:flint_double_sword": 3,
	"sword:enduranium_double_sword": 7,
	"sword:emerald_double_sword": 5,
	"sword:echoed_copper_double_sword": 5,
	"sword:netherite_double_sword": 7,
	"sword:netherite_double_sword": 7,
	"sword:wither_bone_double_sword": 8,
	"sword:aquamarine_double_sword": 6,
	"sword:meteorite_double_sword": 7,
	"sword:solar_double_sword": 9,
	"sword:comet_double_sword": 11,
	"sword:lunar_double_sword": 7,
	"sword:starlite_double_sword": 6,
	"sword:diamond_double_sword": 6,
	"sword:iron_double_sword": 5,
	"sword:gold_double_sword": 3,
	"sword:copped_double_sword": 4,
	"sword:stone_double_sword": 4,
	"sword:wooden_double_sword": 3
};

// Knockback strength - Default: 0.2
const riptide_kb = 1

//Backwards Hit Range - Default: -1
const backwardsRange = -2

//Backwards Hit Radius - Default: 2
const backwardsRadius = 2

//Backwards Hit Hight - Default: 1
const backwardsHight = 2

//Backwards Hit Sweeping Edge Amount - Default: 1
const backwardsHitAmount = 1

//////////////////////////////////////////////

world.afterEvents.itemUse.subscribe((event) => {
	const { source, itemStack } = event;
	if (!source || !itemStack) return;

	const typeId = itemStack.typeId;
	if (!(typeId in weaponDamages)) return;

	const commands = [
		`execute at @s positioned ^^1^3 run damage @e[r=5,type=arrow] 0 entity_attack entity @s`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=snowball]`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=egg]`,
		`execute at @s positioned ^^1^3 run damage @e[r=5,type=wind_charge_projectile] 0 entity_attack entity @s`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=small_fireball]`,
		`execute at @s positioned ^^1^3 run damage @e[r=5,type=breeze_wind_charge_projectile] 0 entity_attack entity @s`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=ender_pearl]`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=thrown_trident]`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=fireworks_rocket]`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=splash_potion]`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,type=lingering_potion]`,
		`execute at @s positioned ^^1^3 run kill @e[r=2,type=area_effect_cloud]`,
		`execute at @s positioned ^^1^3 run damage @e[r=5,type=dragon_fireball] 0 entity_attack entity @s`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,family=projectile]`,
		`execute at @s positioned ^^1^3 run kill @e[r=5,family=inanimate,type=!item,type=!xp_orb,type=!boat,type=!armor_stand,type=!chest_boat,type=!chest_minecart,type=minecart,type=!tnt_minecart,type=!hopper_minecart,type=!command_block_minecart]`,
		`playanimation @s animation.double_sword_spin`
	];

	for (const cmd of commands) {
		try {
			source.runCommand(cmd);
		} catch (err) {

		}
	}
});

world.afterEvents.itemUse.subscribe((event) => {
	const { source, itemStack } = event;
	if (!source || !itemStack) return;

	if (!(itemStack.typeId in weaponDamages)) return;

	const block = source.dimension.getBlock(source.location);
	if (!block) return;

	const blockId = block.typeId;
	if (blockId !== "minecraft:water" && blockId !== "minecraft:lava") return;

	const viewDir = source.getViewDirection();
	const magnitude = Math.hypot(viewDir.x, viewDir.z);
	if (magnitude === 0) return;

	const knockbackX = viewDir.x / magnitude;
	const knockbackZ = viewDir.z / magnitude;

	try {
		source.applyKnockback(knockbackX, knockbackZ, riptide_kb, riptide_kb);
	} catch (e) {

	}
});

world.afterEvents.entityHitEntity.subscribe((event) => {
	const { damagingEntity } = event;
	if (!damagingEntity || damagingEntity.typeId !== "minecraft:player") return;

	const equippable = damagingEntity.getComponent("minecraft:equippable");
	if (!equippable) return;

	const handItem = equippable.getEquipment(EquipmentSlot.Mainhand);
	if (!handItem || !(handItem.typeId in weaponDamages)) return;

	const damage = weaponDamages[handItem.typeId];
	if (typeof damage !== "number") {
		return;
	}

	const cmd = `execute at @s positioned ^^${backwardsHight}^${backwardsRange} run damage @e[r=${backwardsRadius},c=${backwardsHitAmount},tag=!noDamage] ${damage} entity_attack entity @s`;

	try {
		damagingEntity.runCommand(`tag @s add noDamage`);
		damagingEntity.runCommand(cmd);
		damagingEntity.runCommand(`tag @s remove noDamage`);
	} catch (e) {

	}
})