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'
        var game = properties.game != undefined ? properties.game : 'none'

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

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

                    mtv.startGame(game).then(() => {

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

    })


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

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

        var gameField: EditorAPI_Field = {
            id: 'game',
            name: 'Game',
            type: 'select',
            value: game,
            values: []
        }


        var sendFields = (clearGameField: boolean = false) => {
            if (clearGameField) gameField.values = [{ id: 'none', text: 'None' }]
            EditorAPI.setFields([connectionField, gameField])
        }

        var lastConnectionID = null;
        var validate = () => {
            if (lastConnectionID != connectionID) {
                lastConnectionID = connectionID;

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

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

                        mtv.getGames().then((games) => {
                            var values: { id: string, text: string }[] = [{ id: 'none', text: 'None' }]
                            for (var gameID in games)
                                values.push({ id: games[gameID].id, text: games[gameID].title })

                            gameField.values = values;
                            sendFields()
                        }).catch((error) => {
                            sendFields(true)
                        })
                    } else sendFields(true)
                } else sendFields(true)
            }
        }


        EditorAPI.onFieldChanges((fields) => {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields)

            connectionID = fieldObject.connectionID;
            game = fieldObject.game;

            connectionField.value = connectionID;
            gameField.value = game


            EditorAPI.saveProperties({ connectionID, game })

            validate()
        })

        validate()
    })
}