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

export const EVENTID = 6;

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

const DARKNESS_EVENT_REWARDS = [
	{ id: null, amount: 0, xp: 30 },
	{ id: null, amount: 0, xp: 50 }
];

export default class DarknessEvent {

	eventID = EVENTID;
	name = "Darkness Event";
	description = "text.blocklab_friend_exe:emoticon.darkness";
	progress_info = "text.blocklab_friend_exe:emoticon.darkness_info";
	progress = "§c0/1";
	completeText = "text.blocklab_friend_exe:emoticon.darkness_complete";
	failed = "text.blocklab_friend_exe:emoticon.darkness_failed";
	time = 12;
	total_time = 12;
	start_cooldown = 5;
	isActive = true;
	complete = false;
	constructor() { }

	activate(friend_exe) {
		mc.world.sendMessage({
			rawtext: [
				{ translate: this.description },
				{ text: " §c(" },
				{ translate: this.progress_info },
				{ text: " " },
				{ text: this.progress },
				{ text: "§c)" }
			]
		});
		friend_exe.runCommand("camerashake add @a")
		friend_exe.runCommand("execute as @a at @s run playsound mob.ai-friend.atmosphere.impact.unsettling_impact @s ~ ~ ~ 10000")
		// Apply darkness effect to players
		mc.world.getPlayers().forEach(player => {
			player.addEffect("darkness", (20 * this.time) + (20 * 12));
		});

		friend_exe.triggerEvent("blocklab_friend_exe:add_run_away");
	}

	tick(friend_exe, mood, moodStage) {
		if (!this.isActive) return;

		this.time--;

		const hurt = friend_exe.getProperty("blocklab_friend_exe:darkness_hurt_by_player");

		if (hurt === true) {
			const cooldownNotPassed = this.time > this.total_time - this.start_cooldown;

			if (cooldownNotPassed) {
				// Early hit: cooldown not passed
				friend_exe.setProperty("blocklab_friend_exe:darkness_hurt_by_player", false);
			} else {
				// Successful catch
				mc.world.sendMessage({
					rawtext: [
						{ translate: this.completeText }]
				});
				main.rewardPlayer(DARKNESS_EVENT_REWARDS);
				main.setMood(friend_exe, mood + 30);
				this.complete = true;
				this.removeEffect(20);
				friend_exe.setProperty("blocklab_friend_exe:darkness_hurt_by_player", false);
				friend_exe.runCommand("/playsound mob.ai-friend.longtwist_crawl @a ~ ~ ~ ");
				this.progress = "1/1";
				this.deactivate(friend_exe);
			}
		} else if (this.time <= 0) {
			// Time's up
			friend_exe.runCommand("playsound mob.ai-friend.atmosphere.impact.horror_impact @a");
			//main.setMood(friend_exe, mood - 20);
			mc.world.sendMessage({
				rawtext: [
					{ translate: this.failed }]
			});
			main.PlayEmoteSound("sad");
			this.removeEffect(220);
			if (mc.world.getDay() > 1 && main.randInt(0, 10) > 3) {
				mc.system.runTimeout(() => {
					main.setStage(friend_exe, 1);
				}, 20);
			}
			this.deactivate(friend_exe);
			//			if (main.randInt(0, 10) < 4) main.setStage(friend_exe, 1);
			//main.randInt(0, 10) < 4
			//if (1) friend_exe.triggerEvent("blocklab_friend_exe:addStage1Trafo");//main.setStage(friend_exe, 1);

		}
	}

	removeEffect(removelater) {
		// Remove darkness effect from players
		mc.system.runTimeout(() => {
			mc.world.getPlayers().forEach(player => {
				player.removeEffect("darkness");
			});
		}, removelater);
	}

	deactivate(friend_exe) {
		this.isActive = false;
		friend_exe.triggerEvent("blocklab_friend_exe:remove_run_away");
	}
}
