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

let zenthCheck = new Map()
const animations =[ 
    [
        {
            animation: "minced.katana.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.katana.attack0",
            knockback: [0,0],
            Player_Knockback: [1,0],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.25,
            stun_damage_multiplier: 0.85,
            next_attack_time: 10,
            Unbreakable: false
        },
        {
            animation: "minced.katana.attack",
            knockback: [0,0.25],
            Player_Knockback: [1,0.1],
            type: "pierce",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.75,
            stun_damage_multiplier: 0.15,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.katana.attack2",
            knockback: [0,0.25],
            Player_Knockback: [1,0],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.25,
            stun_damage_multiplier: 0.85,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.katana.attack3",
            knockback: [0,0.25],
            Player_Knockback: [1,0],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.45,
            stun_damage_multiplier: 0.65,
            next_attack_time: 8,
            Unbreakable: false
        }
    ],
    [
        {
            animation: "minced.katana.attack_sprint_zenith",
            knockback: [2,0],
            Player_Knockback: [2,0],
            type: "slash",
            attack_speed: 7,
            hit_number: 2,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 10,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 20,
            Unbreakable: true
        },
        {
            animation: "minced.katana.attack_zenith",
            knockback: [2,0.15],
            Player_Knockback: [2,0.25],
            type: "slash",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.5,
            stun_damage_multiplier: 1,
            next_attack_time: 8,
            Unbreakable: true
        },
        {
            animation: "minced.katana.attack",
            knockback: [0,0],
            Player_Knockback: [1,0],
            type: "pierce",
            attack_speed: 8,
            hit_number: 1,
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.75,
            stun_damage_multiplier: 0.15,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.katana.attack2",
            knockback: [0,0],
            Player_Knockback: [1,0],
            attack_speed: 8,
            hit_number: 1,
            type: "slash",
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.25,
            stun_damage_multiplier: 0.85,
            next_attack_time: 8,
            Unbreakable: false
        },
        {
            animation: "minced.katana.attack3",
            knockback: [0.5,0],
            Player_Knockback: [1,0],
            attack_speed: 8,
            hit_number: 1,
            type: "slash",
            stun_type: "short",
            range: 3,
            distance: 1,
            targetNum: 1,
            damage_multiplier: 1.45,
            stun_damage_multiplier: 0.65,
            next_attack_time: 8,
            Unbreakable: false
        }
    ]
]

const weapon = {
    name: 'katana',
    display_name: "Katana",
    dual_wield: ["hand_cannon","greatsword","wakizashi","odachi","pistol"],
    type: "sword",
    can_block: true,
    block_damage: 30,
    block_power: 30,
    block_time: 2,
    clash_power: 70,
    front_dodge: [3, 0.1],
    back_dodge:[-3, 0.2],
    left_dodge:[3, 0.2],
    right_dodge:[3, 0.2],
    stylePrefixes: ["minced.katana"],
    weaponSize: "medium",
    available_moves: ["back_attack", "flying"],
    cd: 10,
    execute(player, weapon, effect, mode, burn) {
        let 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()*9)
        let critDamage = 0
        critChance == 2 ? critDamage = Math.floor(Math.random() * lavander) : critDamage = 0
        lavander = JSON.parse(lavander) + critDamage
        let animChecker
        let combatMode = player.getProperty("mode:combat")
        for(let i = animations[0].length - 1; i > 0;i--) {
            if(player.getDynamicProperty("combo") % i == 0) {
                animChecker = i
                break;
            }
        }
        if(combatMode == 0) {
            player.setDynamicProperty("combo", 0)
            zenthCheck.set(player.id,1)
            system.runTimeout(() => {
                zenthCheck.set(player.id,0)
            },60)
        }
        let attackEffect = []
        attackEffect.push(effect)
        switch(mode) {
            case "back_attack":{
                let propCheck = {
                    animation: "minced.katana.attack_backward",
                    knockback: [0,2],
                    Player_Knockback: [-1,0],
                    attack_speed: 8,
                    hit_number: 1,
                    type: "slash",
                    stun_type: "short",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 1.3,
                    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, true)
            } break;
            case "bide":{
                let propCheck = {
                    animation: "minced.katana.attack_bide",
                    knockback: [0,0],
                    Player_Knockback: [1,0],
                    attack_speed: 8,
                    hit_number: 1,
                    type: "blunt",
                    stun_type: "short",
                    range: 3,
                    distance: 1,
                    targetNum: 1,
                    damage_multiplier: 1.3,
                    stun_damage_multiplier: 3,
                    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, true)
            } break;
            case "sprint":{
                let propCheck
                let leCount
                if(zenthCheck.get(player.id) != 1) {
                    propCheck = animations[0][0]
                    leCount = 0
                } else {
                    propCheck = animations[1][0]
                    leCount = 1
                    system.runTimeout(()=>{
                        player.setProperty("mode:combat", 0)
                    },((propCheck.attack_speed * 2) + 3))
                    system.runTimeout(()=>{
                        launchPlayer(player, Pknockback[0], Pknockback[1])
                    },((propCheck.attack_speed * 2) - 1))
                }
                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 })
                }
                attackEffect.push("lashes")
                attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, 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.katana.attack_jump",
                    knockback: [1,0.75],
                    Player_Knockback: [0,0],
                    type: "pierce",
                    attack_speed: 8,
                    hit_number: 1,
                    stun_type: "short",
                    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;
            case "execute": {
                executer(player, burn, (lavander * 2), this.name, 40)
            } break;
            default: {
                if(animChecker == 0) {
                    system.runTimeout(() => {
                        launchPlayer(player, -2, 0.5)
                    },2)
                }
                let propCheck
                let leCount
                if(zenthCheck.get(player.id) != 1) {
                    propCheck = animations[0][animChecker]
                    leCount = 0
                } else {
                    propCheck = animations[1][animChecker]
                    leCount = 1
                }
                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
                attackEffect.push("lashes")
                attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, animations[leCount].length - 1, stunD, NAT, breakable, "normal",weapon, hitCount, true)
            }
            break;
        }
    },
    setCore(player, item) {
        let Abilities = [
          {
            Id: "S1",
          },
          {
            Id: "P3",
          },
          {
            Id: "S6",
          },
          {
            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


