const preact = require('preact');
const { connect } = require('preact-redux');

const { errorToast, infoToast } = require('../utils');

const addState = connect(
    function receiveState(state) {
        const {
            ws,
            instances,
            invite,
        } = state;

        function sendInstanceState(id) {
            ws.sendInstanceState(id);
        }

        function sendInstancePractice() {
            ws.sendInstancePractice();
        }

        function sendInstanceQueue() {
            ws.sendInstanceQueue();
        }

        function sendInstanceInvite() {
            ws.sendInstanceInvite();
        }

        return {
            instances,
            invite,

            sendInstanceState,
            sendInstanceQueue,
            sendInstancePractice,
            sendInstanceInvite,
        };
    }
);

function JoinButtons(args) {
    const {
        instances,
        invite,

        sendInstanceState,
        sendInstanceQueue,
        sendInstancePractice,
        sendInstanceInvite,
    } = args;

    const discordBtn = (
        <button
            class='discord-btn'
            onClick={() => window.open('https://discord.gg/YJJgurM') }>
                &nbsp;
        </button>
    );

    if (instances.length) {
        return (
            <footer id="footer">
                <div class="timer-container"></div>
                <button
                    class='pvp ready full'
                    onClick={() => sendInstanceState(instances[0].id)}
                    type="submit">
                    Rejoin
                </button>
            </footer>
        );
    }

    const inviteBtn = () => {
        if (!invite) {
            return (
                <button
                    class='pvp ready'
                    onClick={() => sendInstanceInvite()}
                    type="submit">
                    Invite
                </button>
            );
        }

        function copyClick(e) {
            const link = `${document.location.origin}#join=${invite}`;
            const textArea = document.createElement('textarea', { id: '#clipboard' });
            textArea.value = link;
            document.body.appendChild(textArea);
            textArea.focus();
            textArea.select();

            try {
                document.execCommand('copy');
                infoToast('Invite link copied.');
            } catch (err) {
                console.error('link copy error', err);
                errorToast('Invite link copy error.');
            }

            document.body.removeChild(textArea);
            return true;
        }

        return (
            <button
                class='pvp ready enabled'
                onClick={copyClick}
                type="submit">
                Copy Link
            </button>
        );
    };

    return (
        <footer id="footer">
            <div class="timer-container"></div>
            <button
                class='practice'
                onClick={() => sendInstancePractice()}
                type="submit">
                Learn
            </button>
            {inviteBtn()}
            <button
                class='pvp ready'
                onClick={() => sendInstanceQueue()}
                type="submit">
                PVP
            </button>
        </footer>
    );
}

module.exports = addState(JoinButtons);
