import { world } from "@minecraft/server";
import { Vector3 } from "./../../util/vector3.js";
import { ultimatum } from "./../../util/cameraMoves.js";
import { coordExtractor, cooldowns } from "./../../util/utils.js";

const weaponBoost = new Map([
        ["katana", 2.5],
        ["wakizashi", 2.25],
    ])

const ability = {
    name: 'I1',
    display_name: "Iaido",
    button_icon: "textures/items/diamond",
    registry_name: "iaido",
    description: 'Swings in front of.you to damage enemies',
    indexNum: 5,
    level: "advanced",
    cooldown: 100,
    damages: 15,
    charge_type: "charge_till_end",
    charge_count: 3,
    charge_interval: 10,
    canSwitch : false,
    filters: ["katana", "odachi"],
    slots: ["ability"],
    coodname: "iaicd",
    horizontal: 0,
    vertical: 0,
    execute(player, weaponType, mode, weapon) {
        const cooldown = this.cooldown

        let checker = true
        player.runCommand("scoreboard players remove @s exhaust 60")
        player.playAnimation(`animation.minced.${weaponType}.ability_iaido`)
        let equipment = player.getComponent("minecraft:equippable");
        let mainhand = equipment?.getEquipmentSlot(`Mainhand`)?.getItem()
        switch(mode) {
            case ("ultimate"): {
                if(checker){
                    player.playAnimation(`animation.minced.${weaponType}.ultimate_slash_air`)
                    //player.runCommand(`camerashake add @s 0.25 1 positional`)
                    let cameras = [
                        {
                            times: 0.1,
                            timeStamp: "default",
                            posX: 0.0,
                            posY: 1.0,
                            posZ: -0.25,
                            cameraType: "minecraft:free",
                            easing: 0.1,
                            transitionType: "Linear",
                        },
                        {
                            times: 0.3,
                            timeStamp: "attack_start",
                            posX: 0.0,
                            posY: 1.0,
                            posZ: -1.0,
                            cameraType: "minecraft:free",
                            easing: 0.1,
                            transitionType: "Linear",
                        },
                        {
                            times: 0.5,
                            timeStamp: "default",
                            posX: 0.0,
                            posY: 1.5,
                            posZ: -1.5,
                            cameraType: "minecraft:free",
                            easing: 0.1,
                            transitionType: "Linear",
                        },
                        {
                            times: 0.7,
                            timeStamp: "default",
                            posX: 0.0,
                            posY: 2.0,
                            posZ: -2.25,
                            cameraType: "minecraft:free",
                            easing: 0.1,
                            transitionType: "Linear",
                        },
                        {
                            times: 0.9,
                            timeStamp: "default",
                            posX: 0.0,
                            posY: 2.0,
                            posZ: -3.25,
                            cameraType: "minecraft:free",
                            easing: 0.1,
                            transitionType: "Linear",
                        },
                        {
                            times: 1.0,
                            timeStamp: "restore_movement",
                            posX: 0.0,
                            posY: 3.0,
                            posZ: -4.0,
                            cameraType: "minecraft:free",
                            easing: 0.1,
                            transitionType: "Linear",
                        }
                    ]
                    ultimatum(player, "S1", weaponType, 10, cameras, 5, (10 * 1.5), 20, 10, 20, 1, 5, "line_target", "", [0,0], "long", "", "slash", "", 20, 10, true, "normal",mainhand, 1)
                } else{
                    player.playAnimation(`animation.minced.${weaponType}.ultimate_iaido`)
                }
                cooldowns(player,this.name,cooldown,cooldown)
            } break;
            case ("charge"): {
                if(checker){
                    launchPlayer(player, launch_power, upwards)
                    player.playAnimation(`animation.minced.${weaponType}.ability_slash`)
                    player.runCommand(`camerashake add @s 0.25 1 positional`)
                    attackPlayerMeikyu(player, 5, (10 * 1.5), 20, 10, 20, 1, 5, "line_target", "", [0,0], "long", "", "slash", "", 20, 10, true, "normal",mainhand, 1, true)
                } else{
                    player.playAnimation(`animation.minced.${weaponType}.ability_slash`)
                }
                cooldowns(player,this.name,cooldown,cooldown)
            } break;
            default: {
                player.playAnimation(`animation.minced.${weaponType}.ability_iaido`)
                let tgt = player.getEntitiesFromViewDirection({maxDistance:12,}).filter((hit) => hit.entity.typeId != "pixel:hit_box")[0].entity;
                if (tgt == undefined) {
                    return;
                }
                world.scoreboard.getObjective("exhaust").addScore(player, -10)
                world.scoreboard.getObjective("exhregen").setScore(player, 60)
                let trg = coordExtractor(tgt, 1);
                const targetLoc = Vector3.Add(trg, Vector3.Scale(player.getViewDirection(), 2));
                tgt.addEffect("weakness", 25, { amplifier: 100, showParticle: false })
                tgt.addEffect("slowness", 25, { amplifier: 100, showParticle: false })
                player.addTag("attack")
                player.tryTeleport(targetLoc, player.dimension);
                cooldowns(player,this.name,cooldown,cooldown)
            } break;
        }
    }, 
    resetStats(player) {
        let statuser = JSON.parse(player.getDynamicProperty("resetingStatus"))
        statuser["Boost"] = statuser.Boost - 0.2
        player.setDynamicProperty("resetingStatus", JSON.stringify(statuser))
        player.setProperty("aura:color", 0)
    }
}

export default ability