const toast = require('izitoast');
const cbor = require('borc');

const SOCKET_URL = process.env.NODE_ENV === 'production' ? 'wss://mnml.gg/ws' : 'ws://localhost:40000';

function errorToast(err) {
    console.error(err);
    return toast.error({
        title: 'BEEP BOOP',
        message: err,
        position: 'bottomCenter',
    });
}

function createSocket(events) {
    let ws;

    // handle account auth within the socket itself
    // https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html
    let account;
    try {
        account = JSON.parse(localStorage.getItem('account'));
    } catch (e) {
        localStorage.removeItem('account');
    }

    // -------------
    //   Outgoing
    // -------------
    function send(msg) {
        if (msg.method !== 'ping') console.log('outgoing msg', msg);
        msg.token = account && account.token && account.token;
        ws.send(cbor.encode(msg));
    }

    let ping;
    function sendPing() {
        ping = Date.now();
        send({ method: 'ping', params: {} });
    }

    function sendAccountLogin(name, password) {
        send({ method: 'account_login', params: { name, password } });
    }

    function sendAccountCreate(name, password) {
        send({ method: 'account_create', params: { name, password } });
    }

    function sendAccountConstructs() {
        send({ method: 'account_constructs', params: {} });
    }

    function sendAccountInstances() {
        send({ method: 'account_instances', params: {} });
    }

    function sendAccountZone() {
        send({ method: 'account_zone', params: {} });
    }

    function sendConstructSpawn(name) {
        send({ method: 'construct_spawn', params: { name } });
    }

    function sendGameState(id) {
        send({ method: 'game_state', params: { id } });
    }

    function sendGameReady(id) {
        send({ method: 'game_ready', params: { id } });
    }

    function sendSpecForget(id, spec) {
        send({ method: 'construct_unspec', params: { id, spec } });
    }

    function sendPlayerMmConstructsSet(constructIds) {
        send({ method: 'player_mm_constructs_set', params: { construct_ids: constructIds } });
    }

    function sendInstanceState(instanceId) {
        send({ method: 'instance_state', params: { instance_id: instanceId } });
    }

    function sendVboxAccept(instanceId, group, index) {
        send({ method: 'player_vbox_accept', params: { instance_id: instanceId, group, index } });
    }

    function sendVboxApply(instanceId, constructId, index) {
        send({ method: 'player_vbox_apply', params: { instance_id: instanceId, construct_id: constructId, index } });
        events.setActiveItem(null);
    }

    function sendVboxUnequip(instanceId, constructId, target) {
        send({ method: 'player_vbox_unequip', params: { instance_id: instanceId, construct_id: constructId, target } });
        events.clearInfo();
    }

    function sendVboxDiscard(instanceId) {
        send({ method: 'player_vbox_discard', params: { instance_id: instanceId } });
    }

    function sendVboxCombine(instanceId, indices) {
        send({ method: 'player_vbox_combine', params: { instance_id: instanceId, indices } });
        events.clearCombiner();
    }

    function sendVboxReclaim(instanceId, index) {
        send({ method: 'player_vbox_reclaim', params: { instance_id: instanceId, index } });
    }

    function sendItemInfo() {
        send({ method: 'item_info', params: {} });
    }

    function sendGameSkill(gameId, constructId, targetConstructId, skill) {
        send({
            method: 'game_skill',
            params: {
                game_id: gameId, construct_id: constructId, target_construct_id: targetConstructId, skill,
            },
        });
        events.setActiveSkill(null);
    }

    function sendGameTarget(gameId, constructId, skillId) {
        send({ method: 'game_target', params: { game_id: gameId, construct_id: constructId, skill_id: skillId } });
        events.setActiveSkill(null);
    }

    function sendZoneCreate() {
        send({ method: 'zone_create', params: {} });
    }

    function sendZoneJoin(zoneId, nodeId, constructIds) {
        send({ method: 'zone_join', params: { zone_id: zoneId, node_id: nodeId, construct_ids: constructIds } });
    }

    function sendZoneClose(zoneId) {
        send({ method: 'zone_close', params: { zone_id: zoneId } });
    }

    function sendInstanceJoin(instanceId, constructs) {
        send({ method: 'instance_join', params: { instance_id: instanceId, construct_ids: constructs } });
    }

    function sendInstanceNew(constructs, name, players) {
        send({ method: 'instance_new', params: { construct_ids: constructs, name, players } });
    }

    function sendInstanceReady(instanceId) {
        send({ method: 'instance_ready', params: { instance_id: instanceId } });
    }

    function sendInstanceScores(instanceId) {
        send({ method: 'instance_scores', params: { instance_id: instanceId } });
    }


    // -------------
    //   Incoming
    // -------------
    function accountLogin(res) {
        const [struct, login] = res;

        account = login;
        localStorage.setItem('account', JSON.stringify(login));
        events.setAccount(login);
        sendAccountConstructs();
        sendAccountInstances();
    }

    function accountInstanceList(res) {
        const [struct, playerList] = res;
        events.setInstanceList(playerList);
    }

    function accountConstructs(response) {
        const [structName, constructs] = response;
        events.setConstructList(constructs);
    }

    function gameState(response) {
        const [structName, game] = response;
        events.setGame(game);
    }

    let gameStateTimeout;
    function startGameStateTimeout(id) {
        clearTimeout(gameStateTimeout);
        gameStateTimeout = setTimeout(() => sendGameState(id), 1000);
        return true;
    }

    function clearGameStateTimeout() {
        clearTimeout(gameStateTimeout);
    }

    function constructSpawn(response) {
        const [structName, construct] = response;
    }

    function zoneState(response) {
        const [structName, zone] = response;
        events.setZone(zone);
    }

    let instanceStateTimeout;
    function startInstanceStateTimeout(id) {
        clearTimeout(instanceStateTimeout);
        instanceStateTimeout = setTimeout(() => sendInstanceState(id), 1000);
        return true;
    }

    function instanceState(response) {
        const [structName, i] = response;
        events.setInstance(i);
        return true;
    }

    function clearInstanceStateTimeout() {
        clearTimeout(instanceStateTimeout);
    }

    function itemInfo(response) {
        const [structName, info] = response;
        events.setItemInfo(info);
    }

    function pong() {
        events.setPing(Date.now() - ping);
        setTimeout(sendPing, 1000);
    }

    // -------------
    //   Setup
    // -------------

    // when the server sends a reply it will have one of these message types
    // this object wraps the reply types to a function
    const handlers = {
        construct_spawn: constructSpawn,
        game_state: gameState,
        account_login: accountLogin,
        account_create: accountLogin,
        account_constructs: accountConstructs,
        account_instances: accountInstanceList,
        zone_create: res => console.log(res),
        zone_state: zoneState,
        zone_close: res => console.log(res),
        instance_state: instanceState,
        item_info: itemInfo,
        pong,
    };

    function logout() {
        localStorage.removeItem('account');
        account = null;
        event.setAccount(null);
    }

    function errHandler(error) {
        switch (error) {
            case 'invalid token': return logout();
            case 'no active zone': return sendZoneCreate();
            case 'no constructs selected': return events.errorPrompt('select_constructs');
            case 'node requirements not met': return events.errorPrompt('complete_nodes');
            case 'construct at max skills (4)': return events.errorPrompt('max_skills');

            default: return errorToast(error);

        }
    }

    // decodes the cbor and
    // calls the handlers defined above based on message type
    function onMessage(event) {
        // decode binary msg from server
        const blob = new Uint8Array(event.data);
        const res = cbor.decode(blob);
        const { method, params } = res;

        if (method !== 'pong' ) console.log(res);

        // check for error and split into response type and data
        if (res.err) return errHandler(res.err);
        if (!handlers[method]) return errorToast(`${method} handler missing`);
        return handlers[method](params);
    }

    function connect() {
        ws = new WebSocket(SOCKET_URL);
        ws.binaryType = 'arraybuffer';

        // Connection opened
        ws.addEventListener('open', () => {
            toast.info({
                message: 'connected',
                position: 'bottomCenter',
            });

            sendPing();
            sendItemInfo();

            if (account) {
                events.setAccount(account);
                sendAccountInstances();
                sendAccountConstructs();
            }

            return true;
        });

        // Listen for messages
        ws.addEventListener('message', onMessage);

        ws.addEventListener('error', (event) => {
            console.error('WebSocket error', event);
            // account = null;
            // return setTimeout(connect, 5000);
        });

        ws.addEventListener('close', (event) => {
            console.error('WebSocket closed', event);
            toast.warning({
                message: 'disconnected',
                position: 'bottomCenter',
            });
            return setTimeout(connect, 5000);
        });

        return ws;
    }

    return {
        clearGameStateTimeout,
        clearInstanceStateTimeout,
        sendAccountLogin,
        sendAccountCreate,
        sendAccountConstructs,
        sendAccountInstances,
        sendAccountZone,
        sendGameState,
        sendGameReady,
        sendGameSkill,
        sendGameTarget,
        sendConstructSpawn,
        sendSpecForget,
        sendZoneCreate,
        sendZoneJoin,
        sendZoneClose,
        sendInstanceJoin,
        sendInstanceReady,
        sendInstanceNew,
        sendInstanceScores,
        sendPlayerMmConstructsSet,
        sendInstanceState,
        sendVboxAccept,
        sendVboxApply,
        sendVboxReclaim,
        sendVboxCombine,
        sendVboxDiscard,
        sendVboxUnequip,
        sendItemInfo,
        startInstanceStateTimeout,
        startGameStateTimeout,
        connect,
    };
}

module.exports = createSocket;
