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

let extraMap = new Map([])

const animations = [
    [
        {
            animation: "minced.glaive.attack_sprint",
            knockback: [0,0],
            Player_Knockback: [1,0],
            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: 11,
            animation: "minced.glaive.attack0_new",
            ShowFrames: false,
            next_attack_time: 6,
            attackFrameNumber: 3,
            Player_Knockback: [1,0], 
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,0],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 9,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 11,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 13,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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: 20
                },
            ] 
        },
        {
            attackTime: 10,
            animation: "minced.glaive.attack1_new",
            ShowFrames: false,
            next_attack_time: 3,
            attackFrameNumber: 3,
            Player_Knockback: [1,0],
            frames: [
               {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,0],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 9,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 11,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 13,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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: 20
                },
            ] 
        },
        {
            attackTime: 11,
            animation: "minced.glaive.attack2_new",
            ShowFrames: false,
            attackFrameNumber: 3,
            next_attack_time: 2,
            Player_Knockback: [1,0], 
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,0],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 10,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 12,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 14,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2,
                    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: 20
                },
            ] 
        }
    ]
]
const animationser =[ 
    [
        {
            animation: "minced.glaive.attack_sprint",
            knockback: [3,0],
            Player_Knockback: [2,0],
            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: false
        },
        {
            animation: "minced.glaive.attack0",
            knockback: [0,0],
            Player_Knockback: [1,0],
            type: "slash",
            attack_speed: 12,
            hit_number: 1,
            stun_type: "medium",
            range: 3,
            distance: 2,
            targetNum: 5,
            damage_multiplier: 1.25,
            stun_damage_multiplier: 0.95,
            next_attack_time: 15,
            Unbreakable: false
        },
        {
            animation: "minced.glaive.attack1",
            knockback: [1,0],
            Player_Knockback: [1,0],
            type: "pierce",
            attack_speed: 14,
            hit_number: 1,
            stun_type: "medium",
            range: 3,
            distance: 2,
            targetNum: 5,
            damage_multiplier: 1.75,
            stun_damage_multiplier: 0.65,
            next_attack_time: 15,
            Unbreakable: false
        },
        {
            animation: "minced.glaive.attack2",
            knockback: [1,0],
            Player_Knockback: [1,0],
            type: "slash",
            attack_speed: 14,
            hit_number: 1,
            stun_type: "medium",
            range: 3,
            distance: 2,
            targetNum: 5,
            damage_multiplier: 1.35,
            stun_damage_multiplier: 0.85,
            next_attack_time: 15,
            Unbreakable: false
        }
    ]
]

const yaziAnims = [
    [
        {
            animation: "minced.glaive.attack_sprint",
            knockback: [0,0],
            Player_Knockback: [1,0],
            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: 11,
            animation: "minced.glaive.attack_xiao0",
            ShowFrames: false,
            next_attack_time: 6,
            attackFrameNumber: 3,
            Player_Knockback: [1,0], 
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,0],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "pierce",
                    attack_timeFrame: 9,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:-1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "pierce",
                    attack_timeFrame: 11,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "pierce",
                    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: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 20
                },
            ] 
        },
        {
            attackTime: 10,
            animation: "minced.glaive.attack_xiao1",
            ShowFrames: false,
            next_attack_time: 3,
            attackFrameNumber: 3,
            Player_Knockback: [1,0],
            frames: [
               {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,0],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 9,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 11,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    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: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 20
                },
            ] 
        },
        {
            attackTime: 11,
            animation: "minced.glaive.attack_xiao2",
            ShowFrames: false,
            attackFrameNumber: 3,
            next_attack_time: 5,
             Player_Knockback: [1,0], 
            frames: [
                {
                    timeStamp: "checkParry",
                    FrameMode: "normal",
                    knockback: [0,0],
                    attack_timeFrame: 1
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 10,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:-1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 12,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:0, y: 1, z: 1},
                    targetNum: 10,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "add_frame",
                    FrameMode: "normal",
                    knockback: [0.25,0],
                    Player_Knockback: [1,0],
                    type: "slash",
                    attack_timeFrame: 14,
                    hit_number: 1,
                    stun_type: "short",
                    knockCheck: "mp",
                    range: 2.5,
                    distanceOffset: {x:1, y: 1, z: -0.5},
                    targetNum: 10,
                    damage_multiplier: 1.5,
                    stun_damage_multiplier: 1,
                    Unbreakable: true
                },
                {
                    timeStamp: "restore_movement",
                    FrameMode: "normal",
                    attack_timeFrame: 20
                },
            ] 
        }
    ]
]


const weapon = {
    name: 'glaive',
    display_name: "Glaive",
    dual_wield: [],
    type: "polearm",
    can_block: true,
    block_damage: 30,
    block_time: 5,
    block_power: 20,
    clash_power: 65,
    front_dodge: [3, 0.5],
    back_dodge:[-2, 0.25],
    left_dodge: "none",
    right_dodge:"none",
    stylePrefixes: ["minced.glaive"],
    weaponSize: "medium",
    cd: 13,
    available_moves: [],
    execute(player, weapon, effect, mode, burn) {
        const cd = this.cd
        const blckdmg = this.block_damage
        let yazi = player.getProperty("part:special_model") == 1
        let atkspd
        let lavander = weaponDamageIndicator(weapon)
        let stundamage = stunDamageIndicator(weapon.getTags())
        let critChance = Math.floor(Math.random()*6)
        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 = []
        if(yazi) attackEffect.push("searing")
        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 })
                }
                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.glaive.attack_jump",
                    knockback: [1,0.75],
                    Player_Knockback: [0,-0.5],
                    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-1)
                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 "execute": {
                executer(player, burn, (lavander * 200), this.name, 30)
            } break;
            default: {
                let propCheck = animations[0][animChecker]
                yazi != true ? propCheck = animations[0][animChecker] : propCheck = yaziAnims[0][animChecker]
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attackTime
                let Pknockback = propCheck.Player_Knockback 
                let NAT = propCheck.next_attack_time
                 system.runTimeout(()=>{
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },atkspd-1)
                attackFramerMeikyu(player, propCheck, lavander, blckdmg, cd, attackEffect, ((yazi != true ? animations[0].length : yaziAnims[0].length) - 1), stundamage, NAT, weapon, true)
            }
            break;
        }
    },
    setCore(player, item) {
        let Abilities = [
            {
              Id: "S1",
            },
            {
              Id: "P3",
            },
            {
              Id: "O0",
            },
            {
              Id: "T1",
            },
            {
              Id: "Y1",
            }
          ]
          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


