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

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

export const EVENTID = 2;

const APPLE_REWARDS = [
	{ id: "minecraft:golden_apple", amount: 1, xp: 4 },
	{ id: "minecraft:cookie", amount: 10, xp: 4 },
	{ id: "minecraft:cookie", amount: 12, xp: 4 },
	{ id: "minecraft:pumpkin_pie", amount: 1, xp: 4 },
	{ id: "minecraft:pumpkin_pie", amount: 2, xp: 4 },
	{ id: null, amount: 0, xp: 9 },
	{ id: null, amount: 0, xp: 5 },
	{ id: null, amount: 0, xp: 5 }
];

export default class AppleEvent {

	eventID;
	name = "Cookie Craving";
	description = "text.blocklab_friend_exe:emoticon.apple";
	completeText = "text.blocklab_friend_exe:emoticon.apple_complete";
	failed = "text.blocklab_friend_exe:emoticon.apple_failed";
	progress = "";
	time = 120;
	isActive = true;
	complete = false;
	applesNeeded = main.randInt(1, 2);
	maxApples = this.applesNeeded;

	constructor() {
		this.eventID = EVENTID;
	}

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

	tick(friend_exe, mood, moodStage) {
		this.time--;
		if (this.time == 60 || this.time == 55 || this.time == 30 || this.time == 25) {
			main.playTimeclockSound();
		}
		if (this.time == 3 || this.time == 2 || this.time == 1) {
			friend_exe.runCommand(`execute as @a at @s run playsound note.bit @s ~ ~ ~ 1 1.5`);
			mc.world.sendMessage(`§c${this.time}...`);
		}
		if (this.applesNeeded <= 0) {
			main.setMood(friend_exe, mood + 20);
			mc.world.sendMessage({
				rawtext: [
					{ translate: this.completeText }]
			});
			main.PlayEmoteSound("happy");
			friend_exe.runCommand("replaceitem entity @s slot.weapon.mainhand 1 air");
			main.rewardPlayer(APPLE_REWARDS);
			this.complete = true;
			this.isActive = false;
		} else if (this.time == 0) {
			this.complete = false;
			main.setMood(friend_exe, mood - 10);
			mc.world.sendMessage({
				rawtext: [
					{ translate: this.failed }]
			});
			main.PlayEmoteSound("okay");
			this.isActive = false;
		}
	}

	deactivate(friend_exe) {
		this.isActive = false;
	}

	giveApple(friend_exe) {
		if (this.applesNeeded == 0) return;
		this.applesNeeded--;
		this.time += 5;
		this.progress = `${this.maxApples - this.applesNeeded}/${this.maxApples}`;
		friend_exe.runCommand(`particle minecraft:heart_particle ~ ~2.2 ~`);
	}

}