let SACostMap = Utils.newMap();
// id -> 固定值， 百分比
SACostMap.put("slashblade:none", [0, 0]);

SACostMap.put("slashblade:judgement_cut", [100, 0.05]);
SACostMap.put("slashblade:judgement_cut_slash_air", [150, 0.05]);
SACostMap.put("slashblade:judgement_cut_slash_just", [200, 0.05]);

SACostMap.put("slashblade_sendims:chaotic_judgement_cut", [100, 0.05]);
SACostMap.put("slashblade_sendims:chaotic_judgement_cut_slash_air", [150, 0.05]);
SACostMap.put("slashblade_sendims:chaotic_judgement_cut_slash_just", [200, 0.05]);

SACostMap.put("foxextra:voidslashplus", [500, 0.2]);

SACostMap.put("slashblade:wave_edge_vertical", [200, 0.05]);
SACostMap.put("slashblade:drive_horizontal", [200, 0.05]);
SACostMap.put("slashblade:drive_vertical", [200, 0.05]);
SACostMap.put("slashblade_sendims:wave_edge_ammo", [400, 0.2]);

SACostMap.put("slashblade:heavens_slash_start", [200, 0.15]);

SACostMap.put("slashblade:void_slash", [200, 0.1]);
SACostMap.put("slashblade_sendims:void_slash_ammo", [400, 0.15]);

SACostMap.put("sjap_adder:illusion_drive", [400, 0.2]);

SACostMap.put("slashblade:sakura_end_left", [200, 0.05]);
SACostMap.put("slashblade:sakura_end_left_air", [200, 0.05]);
SACostMap.put("slashblade_sendims:sakura_end_ammo", [200, 0.05]);

SACostMap.put("slashblade_sendims:golden_crux", [400, 0.2]);

SACostMap.put("pseudoedge_break_dawn:black_hole", [300, 0.15]);

SACostMap.put("slashblade:circle_slash", [300, 0.15]);

SACostMap.put("slashblade:storm_bias_start", [400, 0.1]);

SACostMap.put("slashblade:piercing_just", [300, 0.05]);

SACostMap.put("sjap_adder:explosive_dawn", [300, 0.2]);

SACostMap.put("sjap_adder:wave_edge_super", [300, 0.2]);

SACostMap.put("slashblade_addon:spiral_edge", [300, 0.15]);

SACostMap.put("sjap_adder:super_blood_cut", [300, 0.15]);

SACostMap.put("slashblade_addon:fire_spiral", [300, 0.1]);

SACostMap.put("last_smith:odachi_combo_a1", [300, 0.2]);

SACostMap.put("slashblade_sendims:frenzied_burst", [200, 0.15]);

SACostMap.put("slashblade_sendims:mahakala", [400, 0.2]);

SACostMap.put("pseudoedge_break_dawn:dragon_boost", [300, 0.15]);

SACostMap.put("last_smith:fushigiri_left", [100, 0.15]);

SACostMap.put("last_smith:sakura_blistering_swords", [200, 0.2]);

SACostMap.put("slashblade_addon:rapid_blistering_swords", [100, 0.15]);

SACostMap.put("slashblade_sendims:explosive_dawn_ammo", [600, 0.5]);

SACostMap.put("slashblade_sendims:golden_crux_ex", [1000, 0.5]);

SACostMap.put("slashblade_addon:blistering_terra_swords", [600, 0.3]);

SACostMap.put("slashblade_sendims:chaotic_rapid_blistering_swords", [300, 0.25]);

SACostMap.put("sjap_adder:cold_drive", [300, 0.2]);

SACostMap.put("sjap_adder:burning_dance", [600, 0.2]);

SACostMap.put("slashblade_addon:lighting_swords", [400, 0.2]);

SACostMap.put("foxextra:thrust_ex", [200, 0.05]);

SACostMap.put("slashblade:regression_start", [200, 0.01]);

SACostMap.put("last_smith:iai_cross_slash", [150, 0.01]);

SACostMap.put("slashblade:quick_draw_attack", [300, 0.2]);

let defaultCost = [200, 0.2];
let superSlashArtCost = [400, 0.3];

/**
 * 
 * @param {Internal.LivingEntity | Internal.Player} entity 
 * @param {Internal.LivingEvent} event 
 * @param {number} cost 
 * @param {number} cost_persentage 
 * @returns {boolean}
 */
let consumeAPForSA = (entity, event, cost, cost_persentage) => {
    if (cost == 0 && cost_persentage == 0) return true;

    if (entity.isPlayer() && entity.isCreative()) return true;

    let attributeInstance = entity.getAttribute("slashblade_sendims:ap_reduce_amount");
    if (attributeInstance) {
        cost = Math.max(0, cost - attributeInstance.getValue());
    }

    let soul = $UmapyoiAPI.getUmaSoul(entity);
    if (!soul || soul.isEmpty()) {
        entity.tell(Text.translatable("text.slashblade_sendims.no_ap"));
        event.setCanceled(true);
        return false;
    }

    let currentAP = $UmaSoulUtils.getActionPoint(soul);
    let maxAP = $UmaSoulUtils.getMaxActionPoint(soul);
    let total_cost = cost + (maxAP * cost_persentage);

    let hasEnoughAP = currentAP >= total_cost;

    if (!hasEnoughAP && total_cost > 0) {
        let mainHandItem = entity.getMainHandItem();
        if (mainHandItem && !mainHandItem.isEmpty()) {
            entity.addItemCooldown(mainHandItem, 20);
        }
        entity.tell(Text.translatable("text.slashblade_sendims.no_ap"));
        event.setCanceled(true);
        return false;
    } else {
        // entity.tell(total_cost)
        $UmaSoulUtils.addActionPoint(soul, -total_cost);
        return true;
    }
}

NativeEvents.onEvent($PerformSlashArtEvent, event => {
    let entity = event.getEntityLiving();
    if (!entity.isPlayer()) return;

    let saId = String(event.getComboState());
    let costs = defaultCost;

    if (!SACostMap.containsKey(saId)) {
        console.log(`玩家 ${entity.username} 正在释放的 SA [${saId}] 没有被配置消耗！请向制作组反馈这个SA！`);
        entity.tell(
            Text.of(`玩家 ${entity.username} 正在释放的 SA [`)
                .append(Text.of(saId).underlined().clickCopy(saId))
                .append(Text.of(`] 没有被配置消耗！请向制作组反馈这个SA！`))
        );
    } else {
        costs = SACostMap.get(saId);
    }

    let cost = costs[0];
    let cost_persentage = costs[1];

    consumeAPForSA(entity, event, cost, cost_persentage);
});

NativeEvents.onEvent($SuperSlashArtsReleaseEvent, event => {
    let player = event.getPlayer();

    let cost = superSlashArtCost[0];
    let cost_persentage = superSlashArtCost[1];

    consumeAPForSA(player, event, cost, cost_persentage);

})

