import { world, system } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
import { setTimeout } from "./timers.js"

world.afterEvents.playerSpawn.subscribe(
  event => {
    const { initialSpawn, player } = event;
    if (!initialSpawn) return;
    if (player.hasTag('firsttimekum')) return;
    player.runCommand("scoreboard objectives add frtp dummy");
    player.runCommand("title @a title §4ПОРНО-АДДОН СКАЧАН С ТГК @SNEYPMC");
    player.sendMessage("§4System > АДДОН НА ПОРНО СКАЧАН С ТГК @SNEYPMC");
    player.runCommand("tag @s add firsttimekum");
  }
);

world.afterEvents.entityDie.subscribe((eventData) => {
	if (eventData.deadEntity.typeId == "kum:jenny" && eventData.deadEntity.hasTag("1tp")) {
		eventData.deadEntity.runCommand("inputpermission set @p[r=3] movement enabled");
		}
	else if (eventData.deadEntity.typeId == "kum:ellie" && eventData.deadEntity.hasTag("1tp")) {
		eventData.deadEntity.runCommand("inputpermission set @p[r=3] movement enabled");
		}
	else if (eventData.deadEntity.typeId == "kum:bia" && eventData.deadEntity.hasTag("1tp")) {
		eventData.deadEntity.runCommand("inputpermission set @p[r=3] movement enabled");
		}
	else if (eventData.deadEntity.typeId == "kum:luna" && eventData.deadEntity.hasTag("1tp")) {
		eventData.deadEntity.runCommand("inputpermission set @p[r=3] movement enabled");
		}
	else if (eventData.deadEntity.typeId == "kum:slime_girl" && eventData.deadEntity.hasTag("1tp")) {
		eventData.deadEntity.runCommand("inputpermission set @p[r=3] movement enabled");
		}
	else return;
	}
  );

system.runInterval(() => {
    for (const entity of world.getDimension('overworld').getEntities()) {
	try {
        if (entity.hasTag('luna_pat')) {
            entity.removeTag('luna_pat')
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§9huh~?"}]}`)
            }, 10);
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§9nya~"}]}`)
            }, 10);
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§9Hehehe~"}]}`)
            }, 10);
        }

        if (entity.hasTag('payment')) {
            entity.removeTag('payment')
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§9huh~?"}]}`)
            }, 10);
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§9nyyyaaaa~ :D"}]}`)
            }, 10);
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§9tankuuuu owowowo"}]}`)
            }, 10);
        }

        const luna_form = new ActionFormData()
            .title('Luna')
            .button('Sx')
            .button('Touch B°°bs')
            .button('Pat')

        if (entity.hasTag('luna_form')) {
            entity.removeTag('luna_form')
            luna_form.show(entity).then(result => {
                const l = result.selection;
                if (l == 0) {
                    entity.runCommandAsync(`playanimation @s animation.cowgirl_humanoid.luna_sx`)
                    entity.runCommandAsync(`event entity @e[type=kum:luna,r=2,c=1] kum:luna_sx`)
                }
                else if (l == 1) {
                    entity.runCommandAsync(`playanimation @s animation.cowgirl_humanoid.luna_touch`)
                    entity.runCommandAsync(`event entity @e[type=kum:luna,r=2,c=1] kum:luna_touch`)
                }
                else if (l == 2) {
                    entity.runCommandAsync(`playanimation @s animation.cowgirl_humanoid.luna_pat`)
                    entity.runCommandAsync(`event entity @e[type=kum:luna,r=2,c=1] kum:pat`)
                }
                else {
                    entity.runCommandAsync(`event entity @e[type=kum:luna,r=2,c=1] kum:spawn`)
                }
            });
        }

        if (entity.hasTag('dialogue_ellie')) {
            entity.removeTag('dialogue_ellie')
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§cHey!"}]}`)
            }, 10);
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§cMommy is horny"}]}`)
            }, 10);
            setTimeout(() => {
                entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§cSo... what am I gonna do with you now, darling~ ?"}]}`)
            }, 10);
        }
        const bia_form2 = new ActionFormData()
            .title('Bia')
            .button('An4l')
        if (entity.typeId == 'minecraft:player' && entity.hasTag('bia_form2')) {
            entity.removeTag('bia_form2')
            bia_form2.show(entity).then(result => {
                const x = result.selection;
                if (x == 0) {
                    entity.runCommandAsync(`playanimation @s animation.bia_sx_humanoid.new`)
                    entity.runCommandAsync(`event entity @e[type=kum:bia,r=2,c=1] kum:bia_sx`)
                }
                else {
                    entity.addTag('bia_form2')
                }
            });
        }
        const bia_form = new ActionFormData()
            .title('Bia')
            .button('Pat')
            .button('Talk')

        if (entity.typeId == 'minecraft:player' && entity.hasTag('bia_form')) {
            entity.removeTag('bia_form')
            bia_form.show(entity).then(result => {
                const z = result.selection;
                if (z == 0) {
                    entity.runCommandAsync(`playanimation @s animation.pat_humanoid.new`)
                    entity.runCommandAsync(`event entity @e[type=kum:bia,r=2,c=1] kum:pat`)
                }
                else if (z == 1) {
                    entity.runCommandAsync(`event entity @e[type=kum:bia,r=2,c=1] kum:talk`)
                    setTimeout(() => {
                        entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§bHeyaaa~"}]}`)
                    }, 10);
                    setTimeout(() => {
                        entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§bI am hornyyyyy~"}]}`)
                    }, 10);
                    setTimeout(() => {
                        entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§bSo..."}]}`)
                    }, 10);
                    setTimeout(() => {
                        entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§bAre we gonna have some fun nyaa?"}]}`)
                    }, 10);
                }
                else {
                    entity.runCommandAsync(`event entity @e[type=kum:bia,r=2,c=1] kum:normal`)
                    entity.runCommandAsync(`effect @e[type=kum:bia,r=2,c=1] clear`)
			}
                }
            );
        }
        const ellie_form = new ActionFormData()
            .title('Ellie')
            .button('Cowgirl')
            .button('Missionary')

        if (entity.typeId == 'minecraft:player' && entity.hasTag('ellie_form')) {
            entity.removeTag('ellie_form')
            ellie_form.show(entity).then(result => {
                const s = result.selection;
                if (s == 0) {
                    entity.runCommandAsync(`playanimation @s animation.cowgirl_humanoid.new cowgirl 10000000000`)
                    entity.runCommandAsync(`event entity @e[type=kum:ellie,r=2,c=1] kum:cowgirl`)
                    setTimeout(() => {
                        entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§cGood boy... hehe... goodd~"}]}`)
                    }, 10);
                }
                else if (s == 1) {
                    entity.runCommandAsync(`playanimation @s animation.missionary_humanoid.new`)
                    entity.runCommandAsync(`event entity @e[type=kum:ellie,r=2,c=1] kum:missionary`)
                    setTimeout(() => {
                        entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§cGood boy... hehe... goodd"}]}`)
                    }, 10);
                }
                else {
                    entity.runCommandAsync(`event entity @e[type=kum:ellie,r=2,c=1] kum:normal`)
                    entity.runCommandAsync(`tellraw @s {"rawtext":[{"text":"§cWell, you miss it honey."}]}`)
                }
            });
        }
        const newForm = new ActionFormData()
            .title('Jenny')
            .button('Bl0w-J0b')
            .button('Sex')
            .button('B00b-Jb')
            .button('Follow Me')
            .button('Stop Follow Me')
        if (entity.typeId == 'minecraft:player' && entity.hasTag('jenny_owner')) {
            entity.removeTag('jenny_owner');
            newForm.show(entity).then(result => {
                const r = result.selection;
                if (r == 0) {
                    entity.addTag(`jenny_action`)
                    entity.runCommandAsync(`event entity @e[type=kum:jenny,r=2,c=1] kum:sck`)
                }
                else if (r == 1) {
                    entity.addTag(`jenny_action`);
                    entity.runCommandAsync(`event entity @e[type=kum:jenny,r=2,c=1] kum:search`);
                }
                else if (r == 2) {
                    entity.addTag(`jenny_action`)
                    entity.runCommandAsync(`event entity @e[type=kum:jenny,r=2,c=1] kum:bb_job`);
                }
                else if (r == 3) {
                    entity.runCommandAsync(`event entity @e[type=kum:jenny,r=2,c=1] kum:follow`);
                }
                else if (r == 4) {
                    entity.runCommandAsync(`event entity @e[type=kum:jenny,r=2,c=1] kum:no_follow`);
                }
            });
        }
    }
        catch(e) {
            console.warn("error executing command for playery " + playery.name + " Error:" + e);
        }
       }
}, 2);