import {getDiceSettings, getSetting, overlayUrl} from "./utils";

export class Service {

    register() {
        if (getSetting("sendToDiceTower") as Boolean) {
            let url = getSetting("diceTowerUrl") as String;
            let room = getSetting("diceTowerRoom") as String;
            let httpRequest = new XMLHttpRequest();
            httpRequest.open('POST', url + '/dice/' + room + '/register')
            httpRequest.setRequestHeader('Content-Type', 'application/json')
            httpRequest.send(JSON.stringify({
                name: game.user?.name,
                overlay: overlayUrl(game.userId as string),
                id: room + ':' + game.userId
            }))
        }
    }

    roll(value: { name: string, command: string, results: any[] }) {
        this.register();
        let url = getSetting("diceTowerUrl") as String;
        let room = getSetting("diceTowerRoom") as String;
        let payload = getDiceSettings() as any;
        payload.name = value.name;
        payload.command = value.command;
        payload.results = value.results;
        let httpRequest = new XMLHttpRequest();
        httpRequest.open('POST', url + '/dice/' + room + ':' + game.userId)
        httpRequest.setRequestHeader('Content-Type', 'application/json')
        httpRequest.send(JSON.stringify(payload))
    }

}
