import { ActionAPI } from "../../../ActionAPI";
import { EditorAPI, EditorAPI_Field } from "../../../EditorAPI";
import { MoorenTV } from "../MoorenTv";

module.exports = (ActionAPI: ActionAPI) => {
    ActionAPI.handle((properties, status) => {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none'

        if (connectionID != 'none') {
            var connection = ActionAPI.getConnection('moorentv', connectionID)

            if (connection) {
                var mtv: MoorenTV = connection.instance;

                mtv.stopGame().then(() => {

                }).catch((error) => {
                    status(error, 'error')
                })
            } else status(`Connection doesn't exist`, 'error')
        } else status('No connection specified', 'error')

    })


    ActionAPI.onOpenEditor((EditorAPI: EditorAPI, properties) => {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none'

        var connectionField: EditorAPI_Field = {
            id: 'connectionID',
            name: "Connection",
            type: "connection",
            connectionType: 'moorentv',
            value: connectionID
        }

        EditorAPI.setFields([connectionField])

        EditorAPI.onFieldChanges((fields) => {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields)
            EditorAPI.saveProperties({ connectionID: fieldObject.connectionID })
        })
    })
}