import { world, system, EquipmentSlot} from "@minecraft/server";
import { launchPlayer } from "./../../util/movements.js";
import { weaponDamageIndicator, generateUniqueId, stunDamageIndicator } from "./../../util/utils.js";
import { attackPlayerMeikyu, plunge } from "./../../util/attackMethods.js";

const animations =[ 
    [
        {
            animation: "minced.mace.attack_sprint",
            knockback: [3,0.25],
            Player_Knockback: [1,0.25],
            type: "blunt",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "medium",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 0.85,
            stun_damage_multiplier: 1.25,
            next_attack_time: 5,
            Unbreakable: false
        },
        {
            animation: "minced.mace.attack0",
            knockback: [0,0],
            Player_Knockback: [1,0],
            type: "blunt",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 0.95,
            stun_damage_multiplier: 1.35,
            next_attack_time: 5,
            Unbreakable: false
        },
        {
            animation: "minced.mace.attack1",
            knockback: [0.25,0.25],
            Player_Knockback: [1,0],
            type: "blunt",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 0.5,
            stun_damage_multiplier: 1.5,
            next_attack_time: 5,
            Unbreakable: false
        },
        {
            animation: "minced.mace.attack2",
            knockback: [0.5,0.25],
            Player_Knockback: [1,0],
            type: "blunt",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 0.75,
            stun_damage_multiplier: 1.25,
            next_attack_time: 5,
            Unbreakable: false
        }
    ]
]

const weapon = {
    name: 'mace',
    display_name: "Mace",
    dual_wield: ["shield","mace"],
    type: "mace",
    can_block: false,
    block_damage: 40,
    block_power: 20,
    block_time: 3,
    clash_power: 30,
    front_dodge: [2, 0.2],
    back_dodge:[-2, 0.25],
    left_dodge:"none",
    right_dodge:"none",
    stylePrefixes: ["minced.mace"],
    weaponSize: "small",
    cd: 12,
    available_moves: [],
    execute(player, weapon, effect, mode, burn) {
        const cd = this.cd
        const blckdmg = this.block_damage
        let atkspd
        let lavander = weaponDamageIndicator(weapon)
        let stundamage = stunDamageIndicator(weapon.getTags())
        let critChance = Math.floor(Math.random()*9)
        let critDamage = 0
        critChance == 2 ? critDamage = Math.floor(Math.random() * lavander) : critDamage = 0
        lavander = JSON.parse(lavander) + critDamage
        let animChecker
        for(let i = animations[0].length - 1; i > 0; i--) {
            if(player.getDynamicProperty("combo") % i == 0) {
                animChecker = i
                break;
            }
        }
        let attackEffect = []
        attackEffect.push(effect)
        switch(mode) {
            case "sprint":{
                let propCheck = animations[0][0]
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let Pknockback = propCheck.Player_Knockback 
                let effects = propCheck.type
                let stun = propCheck.stun_type
                let ranger = propCheck.range
                let distil = propCheck.distance
                let tgtnm = propCheck.targetNum
                let NAT = propCheck.next_attack_time
                let stunD = stundamage * propCheck.stun_damage_multiplier
                let breakable = propCheck.Unbreakable
                let hitCount = propCheck.hit_number
                system.runTimeout(()=>{
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },atkspd)
                if(animChecker == 3) {
                    player.addEffect("slow_falling", 16, { amplifier: 3, showParticles: false })
                }
                attackEffect.push("armorBreaker")
                attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, (animations[0].length - 1), stunD, NAT, breakable, "no cooldown",weapon, hitCount, true)
            } break;
            case "jump": {
                if(world.scoreboard.getObjective("exhaust").getScore(player) < 20) {
                    player.setDynamicProperty("attacking",0)
                    world.scoreboard.getObjective("combatCooldown").setScore(player, 10)
                    world.scoreboard.getObjective("combatCheck").setScore(player, 0)
                    player.runCommandAsync("tag @s remove attack")
                    return;
                }
                let propCheck = {
                    animation: "minced.mace.attack_jump",
                    knockback: [1,0.75],
                    Player_Knockback: [0,0.7],
                    type: "blunt",
                    attack_speed: 5,
                    hit_number: 1,
                    stun_type: "medium",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 1.5,
                    stun_damage_multiplier: 1,
                    next_attack_time: 40,
                    Unbreakable: false
                }
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let distil = propCheck.distance
                let Pknockback = propCheck.Player_Knockback
                world.scoreboard.getObjective("exhaust").addScore(player, -20)
                world.scoreboard.getObjective("exhregen").setScore(player, 40)
                system.runTimeout(()=>{
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                })
                plunge(player, 3, lavander, cd, atkspd, distil, 1, -1, this.name)
            } break; 
            case "execute": {
                let propCheck = {
                    animation: "minced.mace.attack_execute",
                    knockback: [1,0.75],
                    Player_Knockback: [0,0.25],
                    type: "blunt",
                    attack_speed: 13,
                    hit_number: 1,
                    stun_type: "medium",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 1.5,
                    stun_damage_multiplier: 1,
                    next_attack_time: 5,
                    Unbreakable: false
                }
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let Pknockback = propCheck.Player_Knockback 
                let effects = propCheck.type
                let stun = propCheck.stun_type
                let ranger = propCheck.range
                let distil = propCheck.distance
                let tgtnm = propCheck.targetNum
                let NAT = propCheck.next_attack_time
                let stunD = stundamage * propCheck.stun_damage_multiplier
                let breakable = propCheck.Unbreakable
                let hitCount = propCheck.hit_number
                system.runTimeout(()=>{
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },atkspd)
                attackPlayerMeikyu(player, ranger, ((lavander * propCheck.damage_multiplier)), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, 1, stunD, NAT, breakable, "no cooldown",weapon, hitCount, true)
            } break;
            default: {
                let propCheck = animations[0][animChecker]
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let Pknockback = propCheck.Player_Knockback 
                let effects = propCheck.type
                let stun = propCheck.stun_type
                let ranger = propCheck.range
                let distil = propCheck.distance
                let tgtnm = propCheck.targetNum
                let NAT = propCheck.next_attack_time
                let stunD = stundamage * propCheck.stun_damage_multiplier
                let breakable = propCheck.Unbreakable
                let hitCount = propCheck.hit_number
                system.runTimeout(()=>{
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },atkspd)
                if(animChecker == 3) {
                    player.addEffect("slow_falling", 16, { amplifier: 3, showParticles: false })
                }
                attackEffect.push("armorBreaker")
                attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, (animations[0].length - 1), stunD, NAT, breakable, "no cooldown",weapon, hitCount, true)
            }
            break;
        }
    },
    setCore(player, item) {
        let Abilities = [
            {
              Id: "B1",
            },
            {
              Id: "P3",
            },
            {
              Id: "O0",
            },
            {
              Id: "S2",
            },
            {
              Id: "O0",
            }
          ]
          item.setDynamicProperty("abilities", JSON.stringify(Abilities))
          item.setDynamicProperty("fightStyle", 1)
        player.getComponent('equippable')?.setEquipment(EquipmentSlot.Mainhand, item)
    },
    getCurrentAtkspd(player) {
        let animChecker
        for(let i = (animations[0].length - 1); i > 0; i--) {
            if(player.getDynamicProperty("combo") % i == 0) {
                animChecker = i
                break;
            }
        }
        return animations[0][animChecker].attack_speed
    } 
}

export default weapon


