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

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

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

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[0] !== 'Ping') console.log('outgoing msg', msg);
        ws.send(cbor.encode(msg));
    }

    let ping;
    function sendPing() {
        ping = Date.now();
        send(['Ping', {}]);
    }

    function sendAccountConstructs() {
        send(['AccountConstructs', {}]);
    }

    function sendAccountInstances() {
        send(['AccountInstances', {}]);
    }

    function sendAccountSetTeam(ids) {
        send(['AccountSetTeam', { ids }]);
    }

    function sendGameState(id) {
        send(['GameState', { id }]);
    }

    function sendGameReady(id) {
        send(['GameReady', { id }]);
    }

    function sendInstanceState(instanceId) {
        send(['InstanceState', { instance_id: instanceId }]);
    }

    function sendVboxAccept(instanceId, group, index) {
        send(['VboxAccept', { instance_id: instanceId, group, index }]);
        events.clearInstance();
    }

    function sendVboxApply(instanceId, constructId, index) {
        send(['VboxApply', { instance_id: instanceId, construct_id: constructId, index }]);
        events.clearInstance();
    }

    function sendVboxUnequip(instanceId, constructId, target) {
        send(['VboxUnequip', { instance_id: instanceId, construct_id: constructId, target }]);
        events.clearInstance();
    }

    function sendVboxDiscard(instanceId) {
        send(['VboxDiscard', { instance_id: instanceId }]);
        events.clearInstance();
    }

    function sendVboxCombine(instanceId, indices) {
        send(['VboxCombine', { instance_id: instanceId, indices }]);
        events.clearCombiner();
    }

    function sendVboxReclaim(instanceId, index) {
        send(['VboxReclaim', { instance_id: instanceId, index }]);
        events.clearInstance();
    }

    function sendItemInfo() {
        send(['ItemInfo', {}]);
    }

    function sendGameSkill(gameId, constructId, targetConstructId, skill) {
        send(['GameSkill',
            { game_id: gameId, construct_id: constructId, target_construct_id: targetConstructId, skill },
        ]);
        events.setActiveSkill(null);
    }

    function sendGameTarget(gameId, constructId, skillId) {
        send(['GameTarget', { game_id: gameId, construct_id: constructId, skill_id: skillId }]);
        events.setActiveSkill(null);
    }

    function sendInstancePractice() {
        send(['InstancePractice', {}]);
    }

    function sendInstanceQueue() {
        send(['InstanceQueue', {}]);
    }

    function sendInstanceReady(instanceId) {
        send(['InstanceReady', { instance_id: instanceId }]);
    }

    function sendMtxApply(constructId, mtx, name) {
        send(['MtxConstructApply', { construct_id: constructId, mtx, name }]);
        if (mtx === 'Rename') {
            events.clearMtxActive();
            events.clearConstructRename();
        }
    }

    function sendMtxBuy(mtx) {
        send(['MtxBuy', { mtx }]);
    }

    function sendMtxConstructSpawn() {
        send(['MtxConstructSpawn', {}]);
    }
    // -------------
    //   Incoming
    // -------------
    function onAccount(login) {
        events.setAccount(login);
    }

    function onAccountShop(shop) {
        events.setShop(shop);
    }

    function onAccountInstances(list) {
        events.setAccountInstances(list);
    }

    function onAccountConstructs(constructs) {
        events.setConstructList(constructs);
    }

    function onAccountTeam(constructs) {
        events.setTeam(constructs);
    }

    function onConstructSpawn(construct) {
        events.setNewConstruct(construct);
    }

    function onGameState(game) {
        events.setGame(game);
    }

    function onInstanceState(instance) {
        events.setInstance(instance);
    }

    function onItemInfo(info) {
        events.setItemInfo(info);
    }

    let pongTimeout;
    function onPong() {
        events.setPing(Date.now() - ping);
        // pongTimeout = 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 = {
        AccountState: onAccount,
        AccountConstructs: onAccountConstructs,
        AccountTeam: onAccountTeam,
        AccountInstances: onAccountInstances,
        AccountShop: onAccountShop,
        ConstructSpawn: onConstructSpawn,
        GameState: onGameState,
        InstanceState: onInstanceState,
        ItemInfo: onItemInfo,
        Pong: onPong,

        QueueRequested: () => console.log('pvp queue request received'),
        QueueJoined: () => console.log('you have joined the pvp queue'),

        Error: errHandler,
    };

    function logout() {
        window.location.reload(true);
    }

    function errHandler(error) {
        switch (error) {
            case 'invalid token': return logout();
            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 [msgType, params] = res;
        if (msgType !== 'Pong') console.log(res);

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

    // Connection opened
    function onOpen() {
        toast.info({
            message: 'connected',
            position: 'topRight',
        });

        sendPing();
        sendItemInfo();

        return true;
    }

    function onError(event) {
        console.error('WebSocket error', event);
    }

    function onClose(event) {
        console.error('WebSocket closed', event);
        toast.warning({
            message: 'disconnected',
            position: 'topRight',
        });
        return setTimeout(connect, 5000);
    }

    function connect() {
        if (ws) {
            clearTimeout(pongTimeout);
            ws.removeEventListener('open', onOpen);
            ws.removeEventListener('message', onMessage);
            ws.removeEventListener('error', onError);
            ws.removeEventListener('close', onClose);
            ws = null;
        }

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

        // Listen for messages
        ws.addEventListener('open', onOpen);
        ws.addEventListener('message', onMessage);
        ws.addEventListener('error', onError);
        ws.addEventListener('close', onClose);
        return ws;
    }

    return {
        sendAccountConstructs,
        sendAccountInstances,
        sendAccountSetTeam,

        sendGameState,
        sendGameReady,
        sendGameSkill,
        sendGameTarget,

        sendInstanceReady,
        sendInstancePractice,
        sendInstanceQueue,
        sendInstanceState,

        sendVboxAccept,
        sendVboxApply,
        sendVboxReclaim,
        sendVboxCombine,
        sendVboxDiscard,
        sendVboxUnequip,

        sendItemInfo,

        sendMtxApply,
        sendMtxBuy,
        sendMtxConstructSpawn,

        connect,
    };
}

module.exports = createSocket;
