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

const animations =[ 
    [
        {
            animation: "minced.dagger.attack_sprint",
            knockback: [1,0.25],
            Player_Knockback: [2,0],
            type: "slash",
            attack_speed: 4,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 10,
            Unbreakable: false,
        },
        {
            animation: "minced.dagger.attack",
            knockback: [0,0.25],
            Player_Knockback: [1,0],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "shortest",
            range: 3,
            distance: 0.5,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack2",
            knockback: [0,0.25],
            Player_Knockback: [3,0],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "shortest",
            range: 3,
            distance: 0.5,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 3,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack3",
            knockback: [0,0.25],
            Player_Knockback: [3,0],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 0.5,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 3,
            Unbreakable: false
        }
    ]
]
const dual_wield =[ 
    [
        {
            animation: "minced.dagger.attack_sprint",
            knockback: [2,0.75],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 10,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack_dual_wield",
            knockback: [0,0.25],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack_dual_wield2",
            knockback: [0,0.25],
            Player_Knockback: [3,0],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack_dual_wield3",
            knockback: [0,0.25],
            Player_Knockback: [3,0],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 8,
            Unbreakable: false
        }
    ]
]
const offhand =[ 
    [
        {
            animation: "minced.dagger.attack_sprint",
            knockback: [2,0.75],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 10,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack_offhand",
            knockback: [0,0.25],
            Player_Knockback: [1,0.25],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 4,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack_offhand2",
            knockback: [0,0.25],
            Player_Knockback: [3,0],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 4,
            Unbreakable: false
        },
        {
            animation: "minced.dagger.attack_offhand3",
            knockback: [0,0.25],
            Player_Knockback: [3,0],
            type: "slash",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 4,
            Unbreakable: false
        }
    ]
]

const weapon = {
    name: 'dagger',
    display_name: "Dagger",
    dual_wield: ["dagger","sword","longsword","rapier","katar","grand_cross"],
    type: "dagger",
    can_block: true,
    block_damage: 5,
    block_power: 30,
    block_time: 2,
    clash_power: 35,
    front_dodge: [3, 0.5],
    back_dodge:[-2, 0.5],
    left_dodge:[3, 0.2],
    right_dodge:[3, 0.2],
    stylePrefixes: ["minced.dagger"],
    weaponSize: "small",
    available_moves: ["assasinate", "flying"],
    cd: 8,
    execute(player, weapon, effect, mode, burn) {
        const cd = this.cd
        const blckdmg = this.block_damage
        let atkspd
        let blocking = player.hasTag("blocking")
        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 = 0
        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_attack":{
                let propCheck = {
                    animation: "minced.dagger.attack_backward",
                    knockback: [0,2],
                    Player_Knockback: [3,2],
                    attack_speed: 8,
                    hit_number: 1,
                    type: "slash",
                    stun_type: "short",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 1,
                    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
                system.runTimeout(() => {
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },3)
                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 * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, "", stunD, NAT, breakable, "normal",weapon, hitCount, false)
            } break;
            case "offhand":{
                let propCheck = offhand[0][animChecker]
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let Pknockback = propCheck.Player_Knockback
                system.runTimeout(() => {
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },3)
                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 * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, offhand[0].length - 1, stunD, NAT, breakable, "normal",weapon, hitCount, false)
            } break;
            case "dual_wield":{
                let propCheck = dual_wield[0][animChecker]
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                let Pknockback = propCheck.Player_Knockback
                system.runTimeout(() => {
                    launchPlayer(player, Pknockback[0], Pknockback[1])
                },3)
                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 * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, dual_wield[0].length - 1, stunD, NAT, breakable, "normal",weapon, hitCount, false)
            } 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 
                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))
                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;
            case "jump": {
               let propCheck = {
                    animation: "minced.dagger.attack_jump",
                    knockback: [0,0],
                    Player_Knockback: [0,-0.5],
                    type: "pierce",
                    attack_speed: 10,
                    hit_number: 1,
                    stun_type: "medium",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 2,
                    stun_damage_multiplier: 1,
                    next_attack_time: 15,
                    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, (animations[0].length - 1), stunD, NAT, breakable, "no cooldown",weapon, hitCount, false)
            } break; 
            case "execute": {
                let propCheck =  {
                    animation: "minced.dagger.attack_execute",
                    knockback: [2,0.5],
                    Player_Knockback: [0,0],
                    type: "slash",
                    attack_speed: 14,
                    hit_number: 1,
                    stun_type: "medium",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 2.5,
                    stun_damage_multiplier: 0.25,
                    next_attack_time: 20,
                    Unbreakable: false
                }
                let knockback = propCheck.knockback
                player.playAnimation(`animation.${propCheck.animation}`)
                atkspd = propCheck.attack_speed
                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 * propCheck.damage_multiplier) * 1.5), blckdmg, cd, atkspd, distil, tgtnm, "line_target", "lashes", knockback, stun, "", effects, 1, stunD, NAT, breakable, "no cooldown",weapon, hitCount, false)
            } break;
            /*case "assasinate": {
                assasinate(player, burn, (lavander * 200), 10, 61)
            } break;*/
            default: {
                let propCheck = animations[0][animChecker]
                let knockback = propCheck.knockback

                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
                if(blocking) {
                  player.playAnimation(`animation.minced.dagger.blocking`,{
                    blendOutTime: 0.1,
                    stopExpression: "(1.0)",
                    nextState: `animation.${propCheck.animation}`
                  })
                  system.runTimeout(()=>{
                    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, "normal",weapon, hitCount, false)
                  },4)
                } else {
                  player.playAnimation(`animation.${propCheck.animation}`)
                  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, "normal",weapon, hitCount, false)
                }
            }
            break;
        }
    },
    setCore(player, item, storage) {
        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


