import { world } from '@minecraft/server';
import { ActionFormData } from '@minecraft/server-ui'

export class McfStereoRegister {
	static registerEvent() {
		world.afterEvents.dataDrivenEntityTrigger.subscribe(({ entity }) => {
			if (!entity) return;
			const lastSound = entity.getDynamicProperty("mr:sound");
			if (!lastSound) return;
			entity.runCommand(`stopsound @a[r=20] ${lastSound}`)
		}, {
			entityTypes: [ "mr:temp" ],
			eventTypes: [ "mcf:event_stereo" ]
		})
	}
}
const sounds = [
	{ id: "record.13", texture: "textures/items/record_13" },
	{ id: "record.cat", texture: "textures/items/record_cat" },
	{ id: "record.blocks", texture: "textures/items/record_blocks" },
	{ id: "record.chirp", texture: "textures/items/record_chirp" },
	{ id: "record.far", texture: "textures/items/record_far" },
	{ id: "record.mall", texture: "textures/items/record_mall" },
	{ id: "record.mellohi", texture: "textures/items/record_mellohi" },
	{ id: "record.stal", texture: "textures/items/record_stal" },
	{ id: "record.strad", texture: "textures/items/record_strad" },
	{ id: "record.ward", texture: "textures/items/record_ward" },
	{ id: "record.11", texture: "textures/items/record_11" },
	{ id: "record.wait", texture: "textures/items/record_wait" },
	{ id: "record.otherside", texture: "textures/items/record_otherside" },
	{ id: "record.5", texture: "textures/items/record_5" },
	{ id: "record.pigstep", texture: "textures/items/record_pigstep" },
	{ id: "record.relic", texture: "textures/items/music_disc_relic" },
	{ id: "record.creator", texture: "textures/items/music_disc_creator" },
	{ id: "record.creator_music_box", texture: "textures/items/music_disc_creator_music_box" },
	{ id: "record.precipice", texture: "textures/items/music_disc_precipice" },
	{ id: "record.tears", texture: "textures/items/music_disc_tears" },
	{ id: "record.lava_chicken", texture: "textures/items/music_disc_lava_chicken" },
];
export class McfStereo {
	constructor() {
		this.onPlayerInteract = this.onPlayerInteract.bind(this);
	}
	showRadioUI(player, radioEntity, block) {
		const radioUI = new ActionFormData().title({ translate: 'mc.title.sound_list' });
		sounds.forEach(sound => {
			radioUI.button({ rawtext: [{ text: '§l' }, { translate: `item.${sound.id.replace(".", "_")}.desc` }] }, sound.texture);
		});
		radioUI.show(player).then(response => {
			if (response.canceled) return;
			const selectedSound = sounds[response.selection].id;
			const oldSound = radioEntity?.getDynamicProperty("mr:sound");
			if (oldSound) player.runCommand(`stopsound @a[r=20] ${oldSound}`);
			if (radioEntity) {
				radioEntity.setDynamicProperty("mr:sound", selectedSound);
			} else {
				const newRadio = player.dimension.spawnEntity("mr:temp", block.center());
				newRadio.setDynamicProperty("mr:sound", selectedSound);
			}
			player.dimension.playSound(selectedSound, block.center());
			player.onScreenDisplay.setActionBar({ translate: "%%1%%2", with: { rawtext: [ { translate: "mc.nowPlaying" }, { translate: `item.${selectedSound.replace(".", "_")}.desc` } ] } });
		});
	}
	onPlayerInteract(e) {
		const { player, block, dimension } = e;
		const stereo = dimension.getEntitiesAtBlockLocation(block.location).find(entity => entity.typeId === "mr:temp");
		if (player.isSneaking && stereo) {
			const oldSound = stereo.getDynamicProperty("mr:sound");
			if (oldSound) player.runCommand(`stopsound @a[r=20] ${oldSound}`);
			dimension.playSound("block.itemframe.remove_item", block.center());
			stereo.remove();
		} else {
			this.showRadioUI(player, stereo, block);
		}
	}
}