import { world, system, EntityDamageCause, EquipmentSlot} from "@minecraft/server";
import { launchPlayer } from "./../../util/movements.js";
import { attackPlayerMeikyu, plunge } from "./../../util/attackMethods.js";
import {JsonDatabase} from "./../../util/con-database.js";

let AirTime = new JsonDatabase("AirTime")
const animations =[
    [
        {
            animation: "minced.unarmed.attack_sprint",
            knockback: [5,0],
            Player_Knockback: [2,0.25],
            type: "blunt",
            attack_speed: 4,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 3,
            stun_damage_multiplier: 1.5,
            next_attack_time: 2,
            Unbreakable: false
        },
        {
            animation: "minced.unarmed.attack_new0",
            knockback: [1,0.25],
            Player_Knockback: [1,0],
            type: "blunt",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 2,
            distance: 1.5,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1.5,
            next_attack_time: 3,
            Unbreakable: false
        },
        {
            animation: "minced.unarmed.attack_new1",
            knockback: [0.25,0.25],
            Player_Knockback: [1,0],
            type: "blunt",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 2,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1.5,
            next_attack_time: 3,
            Unbreakable: false
        }
    ]
]



const weapon = {
    name: 'unarmed',
    display_name: "Unarmed",
    dual_wield: "both",
    type: "hands",
    can_block: false,
    block_damage: 5,
    block_power: 2,
    block_time: 2,
    block_cooldown: 10,
    clash_power: 10,
    front_dodge: [3, 0.25],
    back_dodge:[-3, 0.25],
    left_dodge:[3, 0.2],
    right_dodge:[3, 0.2],
    stylePrefixes: ["minced.unarmed"],
    weaponSize: "small",
    cd: 100,
    available_moves: [],
    execute(player, weapon, effect, mode, burn) {
        const cd = this.cd
        const blckdmg = this.block_damage
        let atkspd
        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 = 10
                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 })
                }
                attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", [], knockback, stun, "", effects, 999, stundamage, NAT, true, "no cooldown","", hitCount)
            } 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.unarmed.attack_jump",
                    knockback: [1,0.75],
                    Player_Knockback: [0,0],
                    type: "pierce",
                    attack_speed: 8,
                    hit_number: 1,
                    stun_type: "knockdown",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 1.5,
                    stun_damage_multiplier: 2,
                    next_attack_time: 16,
                    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-5)
                player.addEffect("slow_falling", 16, { amplifier: 3, showParticles: false })
                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, false)
            } break;
            default: {
                let lavander = 1
                let stundamage = 8
                let propCheck = animations[0][animChecker]
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let Pknockback = propCheck.Player_Knockback
                launchPlayer(player, Pknockback[0], Pknockback[1])
                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 hitCount = propCheck.hit_number
                attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, 999, stundamage, NAT, true, "no cooldown","", hitCount)
            }
            break;
        }
    },
    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


