import * as mc from '@minecraft/server';

export const EVENTID = 3;

import * as main from '../main';

const SNEAK_DANCE_EVENT_REWARDS = [
	{ id: "minecraft:music_disc_11", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_13", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_5", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_blocks", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_cat", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_chirp", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_creator", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_creator_music_box", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_far", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_lava_chicken", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_mall", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_mellohi", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_otherside", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_pigstep", amount: 1, xp: 4 },
	{ id: "minecraft:music_disc_stal", amount: 1, xp: 4 },
	{ id: null, amount: 0, xp: 9 },
	{ id: null, amount: 0, xp: 5 },
	{ id: null, amount: 0, xp: 5 }
];

export default class SneakDanceEvent {

	eventID;
	name = "Dance";
	description = "text.blocklab_friend_exe:emoticon.dance";
	progress_info = "text.blocklab_friend_exe:emoticon.dance_info";
	completeText = "text.blocklab_friend_exe:emoticon.dance_complete";
	keepDancingText = "text.blocklab_friend_exe:emoticon.dance_keep";
	failed = "text.blocklab_friend_exe:emoticon.dance_failed";
	description_choice = main.randInt(1, 2);
	danceTotal = main.randInt(10, 17);
	time = 20;
	danceChoice = 0;
	isActive = true;
	complete = false;
	progress = "";
	danceTime = 10;
	dance = 0;
	danceTimeChoice = [0, 10, 10, 9, 8]; // 3 4 5 und 6
	constructor() {
		this.eventID = EVENTID;
	}

	activate(friend_exe) {
		this.progress = `${this.dance}/${this.danceTotal}`;
		this.description = this.description + String(this.description_choice);
		mc.world.sendMessage({
			rawtext: [
				{ translate: this.description },
				{ text: " §c(" },
				{ translate: this.progress_info },
				{ text: " " },
				{ text: this.progress },
				{ text: "§c)" }
			]
		});
	}

	dancePlayer(friend_exe) {
		this.dance++;
		if (this.dance == 2) { // start dancing on first jump
			this.danceChoice = main.randInt(1, 4);
			this.danceTime = this.danceTimeChoice[this.danceChoice];
			this.time += this.danceTime + 2;
			friend_exe.setProperty("blocklab_friend_exe:dance", this.danceChoice);
			friend_exe.runCommand(`effect @s slowness 2 7 true`);
			friend_exe.runCommand("playsound mob.ai-friend.dancemusic @a ~ ~1 ~");
		}
		this.progress = `${this.dance}/${this.danceTotal}`;
		friend_exe.runCommand("particle blocklab_friend_exe:dance ~ ~ ~");
	}

	deactivate(friend_exe) {
		this.isActive = false;
		friend_exe.setProperty("blocklab_friend_exe:dance", 0);
		friend_exe.runCommand(`effect @s clear slowness`);
	}

	tick(friend_exe, mood, moodStage) {
		this.time--;


		if (this.dance > 2) {
			//spieler hat angefangen zu tanzen
			this.danceTime--;
			if (this.dance >= this.danceTotal) {

				if (this.dance == this.danceTotal) {
					friend_exe.runCommand("particle blocklab_friend_exe:confetti ~ ~1 ~");
					friend_exe.runCommand("playsound firework.blast @a");
				}
				this.time = 30;
				if (this.danceTime <= 0) {//der tanz ist vorbei
					mc.world.sendMessage({
						rawtext: [
							{ translate: this.completeText }
						]
					});
					this.complete = true;
					main.setMood(friend_exe, mood + main.randInt(20, 30));
					friend_exe.runCommand(`playsound mob.ai-friend.friend_exe_party_troete @a ~ ~1 ~`);
					main.rewardPlayer(SNEAK_DANCE_EVENT_REWARDS);
					main.PlayEmoteSound("happy");
					this.deactivate(friend_exe);
				}
			} else {
				if (this.danceTime == 0) {
					friend_exe.runCommand(`playsound mob.ai-friend.sademote_happy @a ~ ~1 ~`);
					mc.world.sendMessage({
						rawtext: [
							{ translate: this.keepDancingText }
						]
					});
				}
			}

		}
		if (this.time == 6) {
			main.playTimeclockSound();
			if (Math.random() <= 0.6) main.PlayEmote("stage0_headshiver", 4);
		}
		if (this.time == 0) {//FAILED!
			main.setMood(friend_exe, mood - 15);
			main.feedBackSound();
			main.PlayEmoteSound("okay");
			mc.world.sendMessage({
				rawtext: [
					{ translate: this.failed }
				]
			});
			this.deactivate(friend_exe);
		}
		else if (friend_exe.getProperty("blocklab_friend_exe:dance")) {
			friend_exe.runCommand(`effect @s slowness 2 7 true`);
		}
	}
}