import { copyToClipboard } from './utils/copy-to-clipboard';

var Wick = window.Wick || {};

Wick.ShareProjectList = (function () {
    const SELECTORS = {
        input: '#share-list-modal #shopping-list-link',
        linkContainer: '#copy-link',
        link: 'a',
    };

    const CLASSES = {
        copied: 'copy-link_hovered',
    };

    function logActiveElement() {
        const active = document.activeElement;

        if (!active) {
            return;
        }
    }

    function getElements() {
        const $input = $(SELECTORS.input);
        const $linkContainer = $(SELECTORS.linkContainer);
        const $link = $linkContainer.find(SELECTORS.link);

        return {
            $input: $input,
            $linkContainer: $linkContainer,
            $link: $link,
        };
    }

    function enableFocusAndSelect($input, source) {
        if (!$input.length) {
            return;
        }

        const input = $input.get(0);
        const value = input.value || '';
        const valueLength = value.length;

        logActiveElement('[ShareProjectList] before removeAttr disabled');

        $input.removeAttr('disabled');

        try {
            input.focus();
        } catch (error) {
            console.error('[ShareProjectList] input.focus() failed', {
                source: source,
                error: error,
            });
        }

        logActiveElement('[ShareProjectList] after input.focus()');

        try {
            if (typeof input.setSelectionRange === 'function') {
                input.setSelectionRange(0, valueLength);
            } else if (typeof input.select === 'function') {
                input.select();
            }
        } catch (error) {
            console.error('[ShareProjectList] selection failed', {
                source: source,
                error: error,
            });
        }

        logActiveElement('[ShareProjectList] after selection');

        setTimeout(function () {
            logActiveElement('[ShareProjectList] after 0ms timeout');
        }, 0);

        setTimeout(function () {
            logActiveElement('[ShareProjectList] after 50ms timeout');
        }, 50);

        setTimeout(function () {
            logActiveElement('[ShareProjectList] after 150ms timeout');
        }, 150);
    }

    function getInputValue($input) {
        const value = String($input.val() || '').trim();

        return value;
    }

    function bindEvents(elements) {
        const $input = elements.$input;
        const $linkContainer = elements.$linkContainer;
        const $link = elements.$link;

        $input.on('focus', function () {
            logActiveElement('[ShareProjectList] on input focus');
        });

        $input.on('blur', function () {
            logActiveElement('[ShareProjectList] on input blur');
        });

        $link.on('click', function (event) {
            event.preventDefault();
        });

        $linkContainer.on('mousedown', function () {
            logActiveElement('[ShareProjectList] on mousedown before focus');
        });

        $linkContainer.on('click', function (event) {
            let value;

            logActiveElement('[ShareProjectList] before click preventDefault');

            event.preventDefault();

            enableFocusAndSelect($input, 'linkContainer click');

            value = getInputValue($input);
            if (!value) {
                return;
            }

            copyToClipboard(value, $linkContainer.get(0), { noUpperCase: true })
                .then(function () {
                    $link.addClass(CLASSES.copied);

                    logActiveElement('[ShareProjectList] after copy success before refocus');

                    enableFocusAndSelect($input, 'copy success refocus');

                    setTimeout(function () {
                        enableFocusAndSelect($input, 'copy success refocus timeout 0');
                    }, 0);

                    setTimeout(function () {
                        enableFocusAndSelect($input, 'copy success refocus timeout 50');
                    }, 50);
                })
                .catch(function (error) {
                    console.error('[ShareProjectList] Failed to copy shopping list link', error);

                    logActiveElement('[ShareProjectList] after copy error before refocus');

                    enableFocusAndSelect($input, 'copy error refocus');
                });
        });

        $input.on('click', function () {
            enableFocusAndSelect($input, 'input click');
            $link.removeClass(CLASSES.copied);
        });
    }

    function init() {
        var elements = getElements();

        if (!elements.$input.length || !elements.$linkContainer.length || !elements.$link.length) {
            return;
        }

        bindEvents(elements);
    }

    return {
        init: init,
    };
})();

Wick.ShareProjectList.init();
