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

const animations = [
    [
        {
            animation: "minced.halberd.attack_sprint",
            knockback: [2,0],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 15,
            Unbreakable: true
        },
        {
            attackTime: 19,
            animation: "minced.halberd.attack0",
            ShowFrames: false,
            next_attack_time: 5,
            attackFrameNumber: 5,
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,1.5],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,0.12],
                    type: "slash",
                    attack_timeFrame: 13,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,0.12],
                    type: "slash",
                    attack_timeFrame: 14,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0.5, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,0.12],
                    type: "slash",
                    attack_timeFrame: 16,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0, y: 1, z: 2},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,0.12],
                    type: "slash",
                    attack_timeFrame: 17,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:-0.5, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,0.12],
                    type: "slash",
                    attack_timeFrame: 18,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:-1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 19
                },
            ] 
        },
        {
            attackTime: 20,
            animation: "minced.halberd.attack1",
            ShowFrames: false,
            next_attack_time: 5,
            attackFrameNumber: 4,
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,1.5],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 11,
                    hit_number: 1,
                    stun_type: "medium",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset: {x:-0.25, y: 1, z: 0},
                    targetNum: 1,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 12,
                    hit_number: 2,
                    stun_type: "medium",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset: {x:0, y: 1, z: 2},
                    targetNum: 2,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 14,
                    hit_number: 3,
                    stun_type: "medium",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset: {x:0.25, y: 1, z: 3},
                    targetNum: 5,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 16,
                    hit_number: 3,
                    stun_type: "medium",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0.5, y: 1, z: 2.5},
                    targetNum: 5,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 20
                },
            ] 
        },
        {
            attackTime: 20,
            animation: "minced.halberd.attack2",
            ShowFrames: false,
            attackFrameNumber: 2,
            next_attack_time: 5,
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,1.5],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "player_knockback",
                    attack_timeFrame: 6,
                    Pknockback: [1,0.25],
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "pierce",
                    attack_timeFrame: 7,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset: {x:0.5, y: 1, z: 0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "pierce",
                    attack_timeFrame: 8,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset:  {x:0.25, y: 1, z: 1.75},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 12
                },
            ]  
        },
        {
            attackTime: 20,
            animation: "minced.halberd.attack3",
            ShowFrames: false,
            attackFrameNumber: 2,
            next_attack_time: 5,
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,1.5],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "player_knockback",
                    attack_timeFrame: 6,
                    Pknockback: [1,0.25],
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "pierce",
                    attack_timeFrame: 7,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset: {x:0.5, y: 1, z: 0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "pierce",
                    attack_timeFrame: 8,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 1.5,
                    distanceOffset:  {x:0.25, y: 1.5, z: 1.75},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 12
                },
            ] 
        },
        {
            attackTime: 20,
            animation: "minced.halberd.attack4",
            ShowFrames: false,
            attackFrameNumber: 6,
            next_attack_time: 10,
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,1.5],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 9,
                    hit_number: 1,
                    stun_type: "long",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:-1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "player_knockback",
                    attack_timeFrame: 10,
                    Pknockback: [1,0.25],
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 11,
                    hit_number: 1,
                    stun_type: "long",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:-0.25, y: 1, z: 0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 12,
                    hit_number: 1,
                    stun_type: "long",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0.25, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "player_knockback",
                    attack_timeFrame: 13,
                    Pknockback: [3,0],
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 14,
                    hit_number: 1,
                    stun_type: "long",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_particle_effect",
                    FrameMode: "normal",
                    attack_timeFrame: 15,
                    distanceOffset: {x:0, y: 0, z: 0},
                    particle: "minced:slash_air"
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 16,
                    hit_number: 1,
                    stun_type: "long",
                    knockCheck: "mp",
                    range: 3.5,
                    distanceOffset: {x:0, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0,0],
                    Player_Knockback: [1,2],
                    type: "slash",
                    attack_timeFrame: 18,
                    hit_number: 1,
                    stun_type: "long",
                    knockCheck: "mp",
                    range: 3.5,
                    distanceOffset: {x:0, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 1,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 25
                },
            ] 
        },
    ]
]
const weapon = {
    name: 'halberd',
    display_name: "Halberd",
    dual_wield: ["shield","halberd","spear"],
    type: "polearm",
    can_block: false,
    block_damage: 33,
    block_power: 20,
    block_time: 5,
    block_cooldown: 10,
    clash_power: 60,
    front_dodge: [3, 0.25],
    back_dodge:[-3, 0.25],
    left_dodge:"none",
    right_dodge:"none",
    stylePrefixes: ["minced.halberd"],
    weaponSize: "medium",
    cd: 20,
    available_moves: ["back_dodge"],
    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()*5)
        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 "back_dodge":{
                let propCheck =  {
                    animation: "minced.halberd.attack_backdodge",
                    knockback: [7,0.25],
                    Player_Knockback: [-2,0.25],
                    type: "slash",
                    attack_speed: 10,
                    hit_number: 1,
                    stun_type: "medium",
                    range: 5,
                    distance: 2,
                    targetNum: 10,
                    damage_multiplier: 1.5,
                    stun_damage_multiplier: 2,
                    next_attack_time: 10,
                    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
                dodgedAttackPolearm(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "initial_knockback_to_target", effects, "", stunD, NAT, breakable, "normal",weapon, hitCount, true)
            } break;
            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
                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 stunD = stundamage * propCheck.stun_damage_multiplier
                let breakable = propCheck.Unbreakable
                let hitCount = propCheck.hit_number
                attackPlayerMeikyu(player, ranger, lavander, blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, "", stunD, NAT, breakable, "normal",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.halberd.attack_jump",
                    knockback: [1,0.75],
                    Player_Knockback: [0,0],
                    type: "slash",
                    attack_speed: 8,
                    hit_number: 1,
                    stun_type: "medium",
                    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": {
                atkspd = 8
                player.playAnimation(`animation.minced.halberd.attack_on_air`)
                player.addEffect("slow_falling", 100, { amplifier: 3, showParticles: false })
                player.applyKnockback(player.getViewDirection().x, player.getViewDirection().z, -3, 1.5)
                plunge(player, 3, ((lavander + (lavander * 0.7))*2), cd, atkspd, 1, 7, -1)
            } break;
            case "execute": {
                executer(player, burn, (lavander * 200), this.name, 44)
            } break;
            default: {
                let propCheck = animations[0][animChecker]
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let NAT = propCheck.next_attack_time
                attackFramerMeikyu(player, propCheck, lavander, blckdmg, cd, attackEffect, (animations[0].length - 1), stundamage, NAT, weapon, true)
            }
            break;
        }
    },
    setCore(player, item) {
        let Abilities = [
            {
              Id: "S1",
            },
            {
              Id: "P3",
            },
            {
              Id: "T1",
            },
            {
              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


