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

const animations =[
    [
        {
            animation: "minced.scythe.attack_sprint",
            knockback: [-0.5,2],
            Player_Knockback: [3,0.25],
            type: "slash",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 2,
            stun_damage_multiplier: 0.5,
            next_attack_time: 15,
            Unbreakable: false
        },
        {
            animation: "minced.scythe.attack0",
            knockback: [-2,0.25],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 10,
            damage_multiplier: 1.5,
            stun_damage_multiplier: 0.75,
            next_attack_time: 15,
            Unbreakable: false
        },
        {
            animation: "minced.scythe.attack1",
            knockback: [1,0.25],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "long",
            range: 5,
            distance: 0,
            targetNum: 10,
            damage_multiplier: 0.3,
            stun_damage_multiplier: 0.3,
            next_attack_time: 15,
            Unbreakable: false
        },
        {
            animation: "minced.scythe.attack2",
            knockback: [-3,0.25],
            Player_Knockback: [0,0],
            type: "slash",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 1.5,
            stun_damage_multiplier: 0.75,
            next_attack_time: 15,
            Unbreakable: false
        }
    ]
]


const weapon = {
    name: 'scythe',
    display_name: "Scythe",
    dual_wield: ["scythe"],
    type: "polearm",
    can_block: false,
    block_damage: 50,
    block_power: 30,
    block_time: 4,
    block_cooldown: 10,
    clash_power: 50,
    front_dodge: [3, 0.25],
    back_dodge:[-3, 0.25],
    left_dodge:"none",
    right_dodge:"none",
    stylePrefixes: ["minced.scythe"],
    weaponSize: "large",
    cd: 8,
    available_moves: [],
    execute(player, weapon, effect, mode, burn) {
        const cd = this.cd
        const blckdmg = this.block_damage
        let atkspd
        let lavander = weaponDamageIndicator(weapon) ?? 4
        let stundamage = stunDamageIndicator(weapon.getTags()) ?? 4
        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; i > 0;i--) {
            if(player.getDynamicProperty("combo") % i == 0) {
                animChecker = i
                break;
            }
        }
        let attackEffect = []
        attackEffect.push(effect)
        switch(mode) {
            /*case "sprint":{
                if(AirCharges.get(player.id) > 0) {
                    AirCharges.set(player.id,(AirCharges.get(player.id) - 1))
                    let lavander = weaponDamageIndicator(weapon)
                    atkspd = 6
                    player.playAnimation(`animation.minced.scythe.attack_sprint_air_charged`)
                    let knockback = animations[0][0].knockback
                    system.runTimeout(()=>{
                        launchPlayer(player, 0, 2.3)
                    },(atkspd + 1))
                    let newSysStats = JSON.parse(player.getDynamicProperty("resetingStatus"))
                    newSysStats["AirTime"] = 11
                    player.setDynamicProperty("resetingStatus", JSON.stringify(newSysStats))
                    player.addEffect("slow_falling", 300, { amplifier: 3, showParticles: false })
                    attackPlayerMeikyu(player, 7, (lavander), blckdmg, cd, atkspd, distance, 1, "line_target", attackEffect, knockback, "on_air", "knock_player", "slash", "", stundamage, 10, true, "no cooldown",weapon, 1)
                } else {
                    let lavander = weaponDamageIndicator(weapon)
                    atkspd = 16
                    player.playAnimation(`animation.${animations[0][0].animation}`)
                    let knockback = [1,0]
                    attackPlayerMeikyu(player, 5, (lavander * 1.5), blckdmg, cd, atkspd, distance, 5, "line_target", attackEffect, knockback, "long", "", "slash", "", stundamage, 20, true, "no cooldown",weapon, 1)
                }   
            } 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.scythe.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, true)
            } break;
            /*case "on_air": {
                let lavander = weaponDamageIndicator(weapon)
                atkspd = 10
                player.playAnimation(`animation.minced.scythe.attack_on_air`)
                let knockback = [0.25,-1]
                plunge(player, 5, lavander, cd, atkspd, distance, 0, -2)
            } break;
            case "on_air_combo": {
                let lavander = weaponDamageIndicator(weapon)
                atkspd = 10
                player.playAnimation(`animation.minced.scythe.attack_on_air`)
                let knockback = [0.25,-6]
                let newSysStats = JSON.parse(player.getDynamicProperty("resetingStatus"))
                newSysStats["AirTime"] = 0
                player.setDynamicProperty("resetingStatus", JSON.stringify(newSysStats))
                attackPlayerMeikyu(player, 7, (lavander), blckdmg, cd, atkspd, 0, targetNum, "line_target", attackEffect, knockback, "knockdown", "", "slash", "", stundamage, 60, true, "no cooldown",weapon, 1)
            } break;*/
            case "execute": {
                assasinate(player, burn, (lavander * 200), 10, 61)
                player.playAnimation(`animation.minced.rapier.attack_execute`)
            } 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
                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, animations[0].length - 1, stundamage, NAT, true, "no cooldown",weapon, hitCount, true)
            }
            break;
        }
    }, 
    setCore(player, item) {
        let Abilities = [
            {
              Id: "S1",
            },
            {
              Id: "P3",
            },
            {
              Id: "O0",
            },
            {
              Id: "B3",
            },
            {
              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


