import { ItemStack, system, world } from "@minecraft/server";
import * as util from '../libs/serverUtils';
const Infinity_gems = new ItemStack('purerain_dino:infinity_gems', 1);
const OneConsume = new ItemStack('purerain_dino:decayed_pixie_powder', 1);
const TwoConsume = new ItemStack('purerain_dino:decayed_pixie_powder', 2);
const ThreeConsume = new ItemStack('purerain_dino:decayed_pixie_powder', 3);
function executeSkillWithInfinityGems(args, skillFunction, delayFunctions) {
    if (util.item.hasItem(args.source, Infinity_gems)) {
        args.source.runCommand(`function purerain_dino/purerain/skills/${skillFunction}`);
        delayFunctions.forEach(({ function: func, delay }) => {
            system.runTimeout(() => {
                args.source.runCommand(`function purerain_dino/purerain/skills/${func}`);
            }, delay);
        });
    }
}
function executeSkillWithConsume(args, skillFunction, consumeItem, delayFunctions) {
    if (util.item.hasItem(args.source, args.itemStack) && util.item.hasItem(args.source, Infinity_gems) == false) {
        if (util.item.consumeItem(args.source, consumeItem)) {
            args.source.runCommand(`function purerain_dino/purerain/skills/${skillFunction}`);
            delayFunctions.forEach(({ function: func, delay }) => {
                system.runTimeout(() => {
                    args.source.runCommand(`function purerain_dino/purerain/skills/${func}`);
                }, delay);
            });
        }
    }
}
const death_card_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'death_card', [
            { function: 'death_card_f', delay: 20 }
        ]);
        executeSkillWithConsume(args, 'death_card', OneConsume, [
            { function: 'death_card_f', delay: 20 }
        ]);
    }
};
const black_hole_staff_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'black_hole_staff', [
            { function: 'black_hole_staff_2', delay: 20 },
            { function: 'damage_entities', delay: 30 },
            { function: 'damage_entities', delay: 40 }
        ]);
        executeSkillWithConsume(args, 'black_hole_staff', OneConsume, [
            { function: 'black_hole_staff_2', delay: 20 },
            { function: 'damage_entities', delay: 30 },
            { function: 'damage_entities', delay: 40 }
        ]);
    }
};
const ice_age_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'ice_age', [
            { function: 'ice_age', delay: 20 }
        ]);
        executeSkillWithConsume(args, 'ice_age', OneConsume, [
            { function: 'ice_age', delay: 20 }
        ]);
    }
};
const ice_age_lv2_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'ice_age_lv2', [
            { function: 'ice_age_lv2', delay: 20 },
            { function: 'ice_age_lv2', delay: 40 },
            { function: 'ice_age_lv2', delay: 60 }
        ]);
        executeSkillWithConsume(args, 'ice_age_lv2', OneConsume, [
            { function: 'ice_age_lv2', delay: 20 },
            { function: 'ice_age_lv2', delay: 40 },
            { function: 'ice_age_lv2', delay: 60 }
        ]);
    }
};
const deadly_roar_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'deadly_roar', []);
        executeSkillWithConsume(args, 'deadly_roar', OneConsume, []);
    }
};
const buff_magic_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'buff_magic', []);
        executeSkillWithConsume(args, 'buff_magic', OneConsume, []);
    }
};
const buff_magic_lv2_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'buff_magic_lv2', []);
        executeSkillWithConsume(args, 'buff_magic_lv2', OneConsume, []);
    }
};
const buff_magic_lv3_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'buff_magic_lv3', []);
        executeSkillWithConsume(args, 'buff_magic_lv3', OneConsume, []);
    }
};
const dark_sword_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'dark_sword', [
            { function: 'dark_sword', delay: 10 },
            { function: 'dark_sword', delay: 20 },
            { function: 'dark_sword', delay: 30 },
            { function: 'dark_sword', delay: 40 },
            { function: 'dark_sword', delay: 50 },
            { function: 'remove_dark_attack', delay: 60 }
        ]);
        executeSkillWithConsume(args, 'dark_sword', OneConsume, [
            { function: 'dark_sword', delay: 10 },
            { function: 'dark_sword', delay: 20 },
            { function: 'dark_sword', delay: 30 },
            { function: 'dark_sword', delay: 40 },
            { function: 'dark_sword', delay: 50 },
            { function: 'remove_dark_attack', delay: 60 }
        ]);
    }
};
const black_coffin_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'black_coffin', [
            { function: 'black_coffin_attack', delay: 10 },
            { function: 'black_coffin_attack', delay: 20 },
            { function: 'black_coffin_attack', delay: 30 },
            { function: 'black_coffin_attack', delay: 40 },
            { function: 'black_coffin_attack', delay: 50 },
            { function: 'black_coffin_attack', delay: 60 },
            { function: 'black_coffin_attack', delay: 70 },
            { function: 'black_coffin_attack', delay: 80 }
        ]);
        executeSkillWithConsume(args, 'black_coffin', OneConsume, [
            { function: 'black_coffin_attack', delay: 10 },
            { function: 'black_coffin_attack', delay: 20 },
            { function: 'black_coffin_attack', delay: 30 },
            { function: 'black_coffin_attack', delay: 40 },
            { function: 'black_coffin_attack', delay: 50 },
            { function: 'black_coffin_attack', delay: 60 },
            { function: 'black_coffin_attack', delay: 70 },
            { function: 'black_coffin_attack', delay: 80 }
        ]);
    }
};
const black_coffin_lv2_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'black_coffin_lv2', [
            { function: 'black_coffin_attack_lv2', delay: 10 },
            { function: 'black_coffin_attack_lv2', delay: 20 },
            { function: 'black_coffin_attack_lv2', delay: 30 },
            { function: 'black_coffin_attack_lv2', delay: 40 },
            { function: 'black_coffin_attack_lv2', delay: 50 },
            { function: 'black_coffin_attack_lv2', delay: 60 },
            { function: 'black_coffin_attack_lv2', delay: 70 },
            { function: 'black_coffin_attack_lv2', delay: 80 },
            { function: 'black_coffin_attack_lv2', delay: 90 }
        ]);
        executeSkillWithConsume(args, 'black_coffin_lv2', OneConsume, [
            { function: 'black_coffin_attack_lv2', delay: 10 },
            { function: 'black_coffin_attack_lv2', delay: 20 },
            { function: 'black_coffin_attack_lv2', delay: 30 },
            { function: 'black_coffin_attack_lv2', delay: 40 },
            { function: 'black_coffin_attack_lv2', delay: 50 },
            { function: 'black_coffin_attack_lv2', delay: 60 },
            { function: 'black_coffin_attack_lv2', delay: 70 },
            { function: 'black_coffin_attack_lv2', delay: 80 },
            { function: 'black_coffin_attack_lv2', delay: 90 }
        ]);
    }
};
const black_coffin_lv3_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'black_coffin_lv3', [
            { function: 'black_coffin_attack_lv3', delay: 10 },
            { function: 'black_coffin_attack_lv3', delay: 20 },
            { function: 'black_coffin_attack_lv3', delay: 30 },
            { function: 'black_coffin_attack_lv3', delay: 40 },
            { function: 'black_coffin_attack_lv3', delay: 50 },
            { function: 'black_coffin_attack_lv3', delay: 60 },
            { function: 'black_coffin_attack_lv3', delay: 70 },
            { function: 'black_coffin_attack_lv3', delay: 80 },
            { function: 'black_coffin_attack_lv3', delay: 90 }
        ]);
        executeSkillWithConsume(args, 'black_coffin_lv3', OneConsume, [
            { function: 'black_coffin_attack_lv3', delay: 10 },
            { function: 'black_coffin_attack_lv3', delay: 20 },
            { function: 'black_coffin_attack_lv3', delay: 30 },
            { function: 'black_coffin_attack_lv3', delay: 40 },
            { function: 'black_coffin_attack_lv3', delay: 50 },
            { function: 'black_coffin_attack_lv3', delay: 60 },
            { function: 'black_coffin_attack_lv3', delay: 70 },
            { function: 'black_coffin_attack_lv3', delay: 80 },
            { function: 'black_coffin_attack_lv3', delay: 90 }
        ]);
    }
};
const red_moon_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'red_moon', [
            { function: 'red_moon_attack', delay: 10 },
            { function: 'red_moon_attack', delay: 20 },
            { function: 'red_moon_attack', delay: 30 },
            { function: 'red_moon_attack', delay: 40 },
            { function: 'red_moon_attack', delay: 50 },
            { function: 'red_moon_attack', delay: 60 }
        ]);
        executeSkillWithConsume(args, 'red_moon', OneConsume, [
            { function: 'red_moon_attack', delay: 10 },
            { function: 'red_moon_attack', delay: 20 },
            { function: 'red_moon_attack', delay: 30 },
            { function: 'red_moon_attack', delay: 40 },
            { function: 'red_moon_attack', delay: 50 },
            { function: 'red_moon_attack', delay: 60 }
        ]);
    }
};
const sword_rain_comp = {
    onUse(args) {
        executeSkillWithInfinityGems(args, 'sword_rain', [
            { function: 'damage_entities', delay: 10 },
            { function: 'damage_entities', delay: 40 },
            { function: 'damage_entities', delay: 50 },
            { function: 'damage_entities', delay: 60 },
            { function: 'damage_entities', delay: 70 },
            { function: 'damage_entities', delay: 80 },
            { function: 'damage_entities', delay: 90 },
            { function: 'damage_entities', delay: 100 }
        ]);
        executeSkillWithConsume(args, 'sword_rain', OneConsume, [
            { function: 'damage_entities', delay: 10 },
            { function: 'damage_entities', delay: 40 },
            { function: 'damage_entities', delay: 50 },
            { function: 'damage_entities', delay: 60 },
            { function: 'damage_entities', delay: 70 },
            { function: 'damage_entities', delay: 80 },
            { function: 'damage_entities', delay: 90 },
            { function: 'damage_entities', delay: 100 }
        ]);
    }
};
world.beforeEvents.worldInitialize.subscribe(args => {
    /*注册名为'purerain_dino:example'的自定义组件，该组件需要在对应item数据驱动文件下挂载
    数驱注册格式：
    "components": {
        ...
        "minecraft:custom_components": [
            "purerain_dino:example"
        ],
        //需要配合该组件达成使用的效果
        "minecraft:use_modifiers": {
                "use_duration": 999999, //可使用时长，按秒计算
                "movement_modifier": 0.4 //使用时移动速度倍率，0.4为原版食物效果，1.0为正常速度
        },
        ...
    }
    注：对应物品的格式版本需在1.21.10以上！建议直接使用1.21.50
    */
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:death_card', death_card_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:ice_age', ice_age_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:ice_age_lv2', ice_age_lv2_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:black_coffin', black_coffin_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:black_coffin_lv2', black_coffin_lv2_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:black_coffin_lv3', black_coffin_lv3_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:red_moon', red_moon_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:buff_magic', buff_magic_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:buff_magic_lv2', buff_magic_lv2_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:buff_magic_lv3', buff_magic_lv3_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:sword_rain', sword_rain_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:deadly_roar', deadly_roar_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:dark_sword', dark_sword_comp);
    args.itemComponentRegistry.registerCustomComponent('purerain_dino:black_hole_staff', black_hole_staff_comp);
});
