import { world, system, EntityDamageCause} from "@minecraft/server";
import { launchPlayer } from "./../../util/movements.js";
import { spawnProjectile } from "./../../util/attackMethods.js";
import { cooldowns } from "./../../util/utils.js";

const weaponBoost = new Map([
        ["dagger", 1],
        ["spear", 2.65],
    ])

const ability = {
    name: 'T2',
    display_name: "Throw",
    button_icon: "textures/items/diamond",
    registry_name: "throw",
    description: 'Swings in front of.you to damage enemies',
    indexNum: 20,
    level: "basic",
    cooldown: 10,
    damages: 15,
    charge_type: "charge_till_end",
    charge_count: 3,
    charge_interval: 10,
    anim_prefix: "throw",
    canSwitch : false,
    coodname: "thrwcd",
    filters: ["spear", "dagger", "sword"],
    slots: ["ability","charge"],
    horizontal: 0.25,
    vertical: 0,
    execute(player, weaponType, mode, weapon) {
        const cooldown = this.cooldown
        const damage = this.damages
        const range = this.ranges
        const dist = this.distance
        const cdname = this.coodname
        const launch_power = this.horizontal
        const upwards = this.vertical
        const targetNum = 1
        const multiplier = weaponBoost.get(weaponType)
        const atkspd = 1
        let equipment = player.getComponent("minecraft:equippable");
        let mainhand = equipment?.getEquipmentSlot(`Mainhand`)?.getItem()
        let attackEffect = []
        switch(mode) {
            case ("ultimate"): {
              if(checker){
                  launchPlayer(player, launch_power, upwards)
                  player.playAnimation(`animation.fast_paced_${weaponType}.ultimate_throw_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: "out_bounce",
                      },
                      {
                          times: 0.3,
                          timeStamp: "attack_start",
                          posX: 0.0,
                          posY: 1.0,
                          posZ: -1.0,
                          cameraType: "minecraft:free",
                          easing: 0.1,
                          transitionType: "out_bounce",
                      },
                      {
                          times: 0.5,
                          timeStamp: "default",
                          posX: 0.0,
                          posY: 1.5,
                          posZ: -1.5,
                          cameraType: "minecraft:free",
                          easing: 0.1,
                          transitionType: "out_bounce",
                      },
                      {
                          times: 0.7,
                          timeStamp: "default",
                          posX: 0.0,
                          posY: 2.0,
                          posZ: -2.25,
                          cameraType: "minecraft:free",
                          easing: 0.1,
                          transitionType: "out_bounce",
                      },
                      {
                          times: 0.9,
                          timeStamp: "default",
                          posX: 0.0,
                          posY: 2.0,
                          posZ: -3.25,
                          cameraType: "minecraft:free",
                          easing: 0.1,
                          transitionType: "out_bounce",
                      },
                      {
                          times: 1.0,
                          timeStamp: "restore_movement",
                          posX: 0.0,
                          posY: 3.0,
                          posZ: -4.0,
                          cameraType: "minecraft:free",
                          easing: 0.1,
                          transitionType: "out_bounce",
                      }
                  ]
                  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.fast_paced_${weaponType}.ultimate_throw`)
              }
              cooldowns(player,this.name,cooldown,cooldown)
            } break;
            case ("charge"): {
                let charger = world.scoreboard.getObjective("chargecount").getScore(player)
                let chargeTest = Math.floor(charger / 10)
                console.warn(chargeTest)
                player.playAnimation(`animation.minced.${weaponType}.trigger_${this.anim_prefix}`)
                switch(chargeTest) {
                    case 1: {
                        player.runCommand(`camerashake add @s 0.25 1 positional`)
                        spawnProjectile(player, `minced:spear_thrown`, 0.75, 1, 1, "normal", "", 10, 1, 30, 1, 1, 1, "line_target", attackEffect, [1, 0], "short", "", "blunt", 10, "normal", 1)
                    } break;
                    case 2: {
                        player.runCommand(`camerashake add @s 0.5 1 positional`)
                        spawnProjectile(player, `minced:spear_thrown`, 0.75, 1, 2, "normal", "", 15, 1, 30, 1, 1, 1, "line_target", attackEffect, [2, 0], "short", "", "blunt", 10, "normal", 1)
                    } break;
                    case 3: {
                        player.runCommand(`camerashake add @s 0.75 1 positional`)
                        spawnProjectile(player, `minced:spear_thrown`, 0.75, 1, 3, "normal", "", 20, 1, 30, 1, 1, 1, "line_target", attackEffect, [3, 0], "short", "", "blunt", 10, "normal", 1)
                    } break;
                } 
              cooldowns(player,this.name,cooldown,cooldown)
            } break;
            default: {
                launchPlayer(player, launch_power, upwards)
                player.playAnimation(`animation.minced.${weaponType}.ability_${this.anim_prefix}`)
                system.runTimeout(()=>{},16)
                player.runCommand(`camerashake add @s 0.25 1 positional`)
                spawnProjectile(player, `minced:spear_thrown`, 0.75, 1, 1, "normal", "", 10, 1, 30, 1, 1, 1, "line_target", attackEffect, [1, 0], "short", "", "blunt", 10, "normal", 1)
              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