import { Player, ItemStack, BlockPermutation, ScriptEventCommandMessageAfterEvent } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
import { setScoreUtility } from "./scoreboard";
import { replaceItemInInventoryCombine, replaceAir, setItemInInventory, fillCommand, decrementStack, fillCommandDestroy, getEntitiesInRange, shiftLocation, applyVarious, caret, caretZ, getEntitiesDistance, ifStandingOn, cancelKnockback, applySelfKnockback, shootProjectile, comboConsume, spawnExplosion, clearAllSkill } from "./function";
import * as combat from "./combat";
import * as skill from "./skill";


/**
 * @param {ScriptEventCommandMessageAfterEvent} ev 
 * @returns 
 */
export function scriptevent(ev){

	if(!ev.id.startsWith("hxc:")) return;
	const eventId = ev.id.split("hxc:")[1];
	const eventEntity = ev.sourceEntity;
	if(eventId == "guard"){
		const isNen = eventEntity.getComponent("type_family").hasTypeFamily("nen");
		const particleLoc = caretZ(eventEntity.getHeadLocation(),eventEntity.getRotation(),[0,0.1,1]);
		eventEntity.clearVelocity();
		if(eventEntity.typeId == "minecraft:player"){
			eventEntity.playAnimation("animation.player.guard",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
		}else{
			eventEntity.playAnimation("animation.player.guard",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
		}
		if(isNen){
			eventEntity.dimension.spawnParticle("hxc:nen_guard_particle",particleLoc);
			eventEntity.dimension.spawnParticle("hxc:guard_particle",particleLoc);
		}
		else if(true){
			eventEntity.dimension.spawnParticle("hxc:guard_particle",particleLoc);
		}
		eventEntity.dimension.playSound("guard",caret(eventEntity.getHeadLocation(),eventEntity.getRotation(),[0,0,0]),{volume:2,pitch:1});
		for(const target of getEntitiesDistance(eventEntity,eventEntity.location,4,true)){
			if(isNen){
				applyVarious({
					target:target,
					damaging:eventEntity,
					knockback:{type:"sphere",knockbackLocation:eventEntity.location,horizontalStrength:4,verticalStrength:0.75}
				});
			}
			else{
				applyVarious({
					target:target,
					damaging:eventEntity,
					knockback:{type:"sphere",knockbackLocation:eventEntity.location,horizontalStrength:2,verticalStrength:0.5}
				});
			}
		}
	}
	else if(eventId == "guard2"){
		if(eventEntity.typeId == "minecraft:player"){
			eventEntity.playAnimation("animation.player.guard",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
		}else{
			eventEntity.playAnimation("animation.player.guard",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
		}
		eventEntity.dimension.playSound("guard",caret(eventEntity.getHeadLocation(),eventEntity.getRotation(),[0,0,0]),{volume:0.98,pitch:1.2});
		cancelKnockback(eventEntity);
	}
	else if(eventId == "mob_die"){
		if(eventEntity.typeId == "minecraft:player"){
			eventEntity.playAnimation("animation.addon_mob.die",{nextState:"third_person",blendOutTime:0.25,controller:"controller.animation.player.root",stopExpression:"q.any_animation_finished"});
		}else{
			eventEntity.playAnimation("animation.addon_mob.die",{nextState:"default",blendOutTime:0.25,controller:"controller.animation.mob",stopExpression:"q.any_animation_finished"});
		}
	}
	else if(eventId == "game_reset"){
		eventEntity.setProperty("hxc:exp",0);
		eventEntity.setProperty("hxc:level",0);
		eventEntity.setProperty("hxc:hatsu",0);
		clearAllSkill(eventEntity);
		eventEntity.triggerEvent("fix");
	}
	else if(eventId == "knockback_cancel"){
		cancelKnockback(eventEntity);
	}
	else if(eventId.startsWith("cooldown_")){
		eventEntity.startItemCooldown(eventId.split("_")[1],Number(eventId.split("_")[2])*20);
	}
	else if(eventId.startsWith("skill_")){
		if(eventId == "skill_punch"){
			combat.punch(eventEntity);
		}
		else if(eventId == "skill_punch_right"){
			combat.punch_right(eventEntity);
		}
		else if(eventId == "skill_punch_left"){
			combat.punch_left(eventEntity);
		}
		else if(eventId == "skill_kick"){
			combat.kick(eventEntity);
		}
		else if(eventId == "skill_kick_right"){
			combat.kick_right(eventEntity);
		}
		else if(eventId == "skill_kick_left"){
			combat.kick_left(eventEntity);
		}
		else if(eventId == "skill_punch2"){
			combat.punch2(eventEntity);
		}
		else if(eventId == "skill_punch2_final"){
			combat.punch2_final(eventEntity);
		}
		else if(eventId == "skill_punch2_end"){
			combat.punch2_end(eventEntity);
		}
		else if(eventId == "skill_slash"){
			combat.slash(eventEntity);
		}
		else if(eventId == "skill_slash_right"){
			combat.slash_right(eventEntity);
		}
		else if(eventId == "skill_slash_left"){
			combat.slash_left(eventEntity);
		}
		else if(eventId == "skill_slash_middle"){
			combat.slash_middle(eventEntity);
		}
		else if(eventId == "skill_slash2"){
			combat.slash2(eventEntity);
		}
		else if(eventId == "skill_slash2_final"){
			combat.slash2_final(eventEntity);
		}
		else if(eventId == "skill_slash2_end"){
			combat.slash2_end(eventEntity);
		}
		else if(eventId == "skill_slash3"){
			combat.slash3(eventEntity);
		}
		else if(eventId == "skill_slash_p"){
			combat.slash_p(eventEntity);
		}
		else if(eventId == "skill_slash4_charge"){
			combat.slash4_charge(eventEntity);
		}
		else if(eventId == "skill_slash4"){
			combat.slash4(eventEntity);
		}
		else if(eventId == "skill_smash_charge"){
			combat.smash_charge(eventEntity);
		}
		else if(eventId == "skill_smash"){
			combat.smash(eventEntity);
		}
		else if(eventId == "skill_dash"){
			combat.dash(eventEntity);
		}
		else if(eventId == "skill_thrust_charge"){
			combat.thrust_charge(eventEntity);
		}
		else if(eventId == "skill_thrust"){
			combat.thrust(eventEntity);
		}
		else if(eventId == "skill_thrust2"){
			combat.thrust2(eventEntity);
		}
		else if(eventId == "skill_thrust2_final"){
			combat.thrust2_final(eventEntity);
		}
		else if(eventId == "skill_thrust2_end"){
			combat.thrust2_end(eventEntity);
		}
		else if(eventId == "skill_thrust3"){
			combat.thrust3(eventEntity);
		}
		else if(eventId == "skill_trident_smash"){
			combat.trident_smash(eventEntity);
		}
		else if(eventId == "skill_trident_throw_charge"){
			combat.trident_throw_charge(eventEntity);
		}
		else if(eventId == "skill_trident_throw"){
			combat.trident_throw(eventEntity);
		}
		else if(eventId == "skill_trident_throw_p"){
			combat.trident_throw_p(eventEntity);
		}
		//gon//
		else if(eventId == "skill_gon_charge"){
			skill.gon_charge(eventEntity);
		}
		else if(eventId == "skill_rock"){
			skill.rock(eventEntity);
		}
		else if(eventId == "skill_rock2"){
			skill.rock2(eventEntity);
		}
		else if(eventId == "skill_rock_smash_charge"){
			skill.rock_smash_charge(eventEntity);
		}
		else if(eventId == "skill_rock_smash"){
			skill.rock_smash(eventEntity);
		}
		else if(eventId == "skill_scissor"){
			skill.scissor(eventEntity);
		}
		else if(eventId == "skill_scissor2"){
			skill.scissor2(eventEntity);
		}
		else if(eventId == "skill_paper"){
			skill.paper(eventEntity);
		}
		else if(eventId == "skill_paper2"){
			skill.paper2(eventEntity);
		}
		else if(eventId == "skill_paper_p"){
			skill.paper_p(eventEntity);
		}
		else if(eventId == "skill_paper2_p"){
			skill.paper2_p(eventEntity);
		}
		else if(eventId == "skill_gon_adult_start"){
			skill.gon_adult_start(eventEntity);
		}
		else if(eventId == "skill_gon_adult"){
			skill.gon_adult(eventEntity);
		}
		else if(eventId == "skill_gon_adult_end"){
			skill.gon_adult_end(eventEntity);
		}
		//killua//
		else if(eventId == "skill_killua_charge"){
			skill.killua_charge(eventEntity);
		}
		else if(eventId == "skill_lightning_punch"){
			skill.lightning_punch(eventEntity);
		}
		else if(eventId == "skill_lightning_palm"){
			skill.lightning_palm(eventEntity);
		}
		else if(eventId == "skill_lightning_bolt"){
			skill.lightning_bolt(eventEntity);
		}
		else if(eventId == "skill_lightning_bolt_p"){
			skill.lightning_bolt_p(eventEntity);
		}
		else if(eventId == "skill_lightning_speed"){
			skill.lightning_speed(eventEntity);
		}
		else if(eventId == "skill_killua_godspeed_start"){
			skill.killua_godspeed_start(eventEntity);
		}
		else if(eventId == "skill_killua_godspeed"){
			skill.killua_godspeed(eventEntity);
		}
		else if(eventId == "skill_killua_godspeed_end"){
			skill.killua_godspeed_end(eventEntity);
		}
		//uvogin//
		else if(eventId == "skill_uvogin_charge"){
			skill.uvogin_charge(eventEntity);
		}
		else if(eventId == "skill_bigbang_impact"){
			skill.bigbang_impact(eventEntity);
		}
		else if(eventId == "skill_bigbang_impact2"){
			skill.bigbang_impact2(eventEntity);
		}
		else if(eventId == "skill_bigbang_impact_p"){
			skill.bigbang_impact_p(eventEntity);
		}
		else if(eventId == "skill_bigbang_impact3_charge"){
			skill.bigbang_impact3_charge(eventEntity);
		}
		else if(eventId == "skill_bigbang_impact3"){
			skill.bigbang_impact3(eventEntity);
		}
		//meruem//
		else if(eventId == "skill_meruem_charge1"){
			skill.meruem_charge1(eventEntity);
		}
		else if(eventId == "skill_meruem_charge2"){
			skill.meruem_charge2(eventEntity);
		}
		else if(eventId == "skill_tail_attack"){
			skill.tail_attack(eventEntity);
		}
		else if(eventId == "skill_rush_attack"){
			skill.rush_attack(eventEntity);
		}
		else if(eventId == "skill_rage_blast"){
			skill.rage_blast(eventEntity);
		}
		else if(eventId == "skill_rush_wing"){
			skill.rush_wing(eventEntity);
		}
		else if(eventId == "skill_meruem_rush_wing"){
			skill.meruem_rush_wing(eventEntity);
		}
		else if(eventId == "skill_rage_blast_p"){
			skill.rage_blast_p(eventEntity);
		}
		else if(eventId == "skill_release"){
			skill.release(eventEntity);
		}
		else if(eventId == "skill_release_end"){
			skill.release_end(eventEntity);
		}
		//feitan//
		else if(eventId == "skill_feitan_angry_start"){
			skill.feitan_angry_start(eventEntity);
		}
		else if(eventId == "skill_feitan_angry"){
			skill.feitan_angry(eventEntity);
		}
		else if(eventId == "skill_feitan_angry_end"){
			skill.feitan_angry_end(eventEntity);
		}
		else if(eventId == "skill_rising_sun"){
			skill.rising_sun(eventEntity);
		}
		else if(eventId == "skill_rising_sun_p"){
			skill.rising_sun_p(eventEntity);
		}
		else if(eventId == "skill_rising_moon"){
			skill.rising_moon(eventEntity);
		}
		else if(eventId == "skill_rising_moon_p"){
			skill.rising_moon_p(eventEntity);
		}
		//nobunaga//
		else if(eventId == "skill_en_slash"){
			skill.en_slash(eventEntity);
		}
		else if(eventId == "skill_en_slash_end"){
			skill.en_slash_end(eventEntity);
		}
		//netero//
		else if(eventId == "skill_rose_bomb"){
			skill.rose_bomb(eventEntity);
		}
		else if(eventId == "skill_first_hand"){
			skill.first_hand(eventEntity);
		}
		else if(eventId == "skill_second_hand"){
			skill.second_hand(eventEntity);
		}
		else if(eventId == "skill_second_hand_p"){
			skill.second_hand_p(eventEntity);
		}
		else if(eventId == "skill_third_hand"){
			skill.third_hand(eventEntity);
		}
		else if(eventId == "skill_ninetyninth_hand_start"){
			skill.ninetyninth_hand_start(eventEntity);
		}
		else if(eventId == "skill_ninetyninth_hand"){
			skill.ninetyninth_hand(eventEntity);
		}
		else if(eventId == "skill_ninetyninth_hand_end"){
			skill.ninetyninth_hand_end(eventEntity);
		}
		else if(eventId == "skill_zero_hand"){
			skill.zero_hand(eventEntity);
		}
		else if(eventId == "skill_zero_hand_p"){
			skill.zero_hand_p(eventEntity);
		}
		//godWarrior//
		else if(eventId == "skill_god_magic_beam"){
			skill.god_magic_beam(eventEntity);
		}
		else if(eventId == "skill_god_magic_beam_p"){
			skill.god_magic_beam_p(eventEntity);
		}
		//ren//
		else if(eventId == "skill_ren"){
			combat.ren(eventEntity);
		}
	}
	else if(eventId.startsWith("setItemInInventory")){
		const array = eventId.split(".");
		setItemInInventory(eventEntity,Number(array[1]),`hxc:${array[2]}`,Number(array[3]),array[4]=="true",array[5]);
	}
	else if(eventId == "addon_mob_get_off"){
		const ridingComp = eventEntity.getComponent("riding");
		if(ridingComp==undefined) return;
		const rideEntity = ridingComp.entityRidingOn;
		const rideEntityId = rideEntity.typeId;
		const breakableEntitis = ["minecraft:boat","minecraft:chest_boat","minecraft:minecart"];
		if(breakableEntitis.includes(rideEntityId)){
			rideEntity.applyDamage(1000);
		}
		else{
			rideEntity.getComponent("rideable").ejectRiders();
		}
	}
};