import { Player, world } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
import { namespace } from "../../namespace";

/**
 * 
 * @param {Player} player 
 */
export function hackerClock(player) {
    const form = new ModalFormData()
    form.title("Hacker Clock")
    form.slider("Set the time in hours", 0, 24, 1, world.getTimeOfDay() / 1000)
    form.dropdown("Set the weather", ["Clear", "Rain", "Thunder"])
    form.toggle("Do daylight cycle", true)
    form.toggle("Do weather cycle", true)

    player.dimension.playSound(`${namespace}:hacker_table_place`, player.location)

    form.show(player).then(data => {

        player.dimension.playSound(`${namespace}:ui_select`, player.location)

        if (data.canceled) return
        const weatherTypes = ["clear", "rain", "thunder"]
        /**
         * @type {number}
         */
        const time = data.formValues[0]
        /**
         * @type {string}
         */
        const weatherIndex = data.formValues[1]
        /**
         * @type {boolean}
         */
        const dayCycle = data.formValues[2]
        /**
         * @type {boolean}
         */
        const weatherCycle = data.formValues[3]

        player.runCommand(`time set ${time * 1000}`)
        player.runCommand(`weather ${weatherTypes[weatherIndex]}`)
        player.runCommand(`gamerule doDaylightCycle ${dayCycle}`)
        player.runCommand(`gamerule doWeatherCycle ${weatherCycle}`)
    })
}