import { system } from "@minecraft/server";
import {JsonDatabase} from "../../util/con-database.js";
import weapons from './../../weaponTypes/import.js';

let dermensch = new JsonDatabase("dermensch")
let perf = new JsonDatabase("perf")

const weaponBoost = new Map([
        ["glaive", 1],
        ["rapier", 3],
    ])

const ability = {
    name: 'B3',
    display_name: "Block",
    button_icon: "textures/items/diamond",
    registry_name: "block",
    description: 'Block Attacks',
    level: "basic",
    cooldown: 2,
    damages: 10,
    charge_type: "block",
    charge_count: 3,
    charge_interval: 10,
    canSwitch : false,
    filters: ["all"],
    slots: ["charge", "ability"],
    coodname: "jmptkcd",
    execute(player, weaponType, mode, weapon) {
        switch(mode){
            case "charge": {
                system.runTimeout(()=>{
                    dermensch.set(player.id, player.getComponent("health").current);
                    perf.set(player.id, 1);
                    system.runTimeout(() => {
                        perf.set(player.id, 0);
                    },10)
                    player.addTag(`blocking`)
                    player.runCommand(`execute as @s run scoreboard players set @s blocking 1`)
                },weapons[weaponType].block_time)
            } break;
            default: {
                player.playAnimation(`animation.minced.${weaponType}.block`, 
                {
                    blendOutTime: 0.1,
                    stopExpression: "!query.is_using_item && query.all_animations_finished",
                    nextState: `animation.minced.${weaponType}.combat_stance`
                })
                system.runTimeout(()=>{
                    dermensch.set(player.id, player.getComponent("health").current);
                    perf.set(player.id, 1);
                    system.runTimeout(() => {
                        perf.set(player.id, 0);
                        player.removeTag("blocking")
                        player.runCommandAsync("tag @s remove attack")
                        player.runCommand(`execute as @s run scoreboard players set @s blocking 0`)
                        player.runCommand(`reload`)
                    },10)
                    player.runCommand(`execute as @s run tag @s add blocking`)
                    player.runCommand(`execute as @s run scoreboard players set @s blocking 1`)
                },weapons[weaponType].block_time)
            } break;
        }
    },
    resetStats(player) {
        let statuser = JSON.parse(player.getDynamicProperty("resetingStatus"))
        statuser["Boost"] = statuser.Boost - 0.5
        player.setDynamicProperty("resetingStatus", JSON.stringify(statuser))
        player.setProperty("aura:color", 0)
    }
}

export default ability