/**
 * @date 20130405 Leo Eibler <dokuwiki@sprossenwanne.at> \n
 *                replace old sack() method with new jQuery method and use post instead of get - see https://www.dokuwiki.org/devel:jqueryfaq \n
 * @date 20130407 Leo Eibler <dokuwiki@sprossenwanne.at> \n
 *                use jQuery for finding the elements \n
 * @date 20130408 Christian Marg <marg@rz.tu-clausthal.de> \n
 *                change only the clicked todoitem instead of all items with the same text \n
 * @date 20130408 Leo Eibler <dokuwiki@sprossenwanne.at> \n
 *                migrate changes made by Christian Marg to current version of plugin (use jQuery) \n
 * @date 20130410 by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at \n
 *                bugfix: encoding html code (security risk <todo><script>alert('hi')</script></todo>) - bug reported by Andreas \n
 * @date 20130413 Christian Marg <marg@rz.tu-clausthal.de> \n
 *                bugfix: chk.attr('checked') returns checkbox state from html - use chk.is(':checked') - see http://www.unforastero.de/jquery/checkbox-angehakt.php \n
 * @date 20130413 by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at \n
 *                bugfix: config option Strikethrough \n
 */

/**
 * html-layout:
 *
 * +input[checkbox].todocheckbox
 * +span.todotext
 * -del
 * --span.todoinnertext
 * ---anchor with text or text only
 */

var ToDoPlugin = {
    /**
     * lock to prevent simultanous requests
     */
    locked: false,

    /**
     * @brief onclick method for input element
     *
     * @param {jQuery} $chk the jQuery input element
     */
    todo: function ($chk) {
        //skip when locked
        if (ToDoPlugin.locked) {
            return;
        }

        //set lock
        ToDoPlugin.locked = true;

        var $spanTodoinnertext = $chk.nextAll("span.todotext:first").find("span.todoinnertext"),
            param = $chk.data(), // contains: index, pageid, date, strikethrough
            checked = !$chk.is(':checked');

        // Capturar el <a> relacionado dentro del mismo contenedor R**G
        // Buscar en: mismo <p>, contenedor padre, o siguiente elemento hermano
        var href = '';
        var $link = $chk.closest('p').find('a');
        if ($link.length && $link.attr('href')) {
            href = $link.attr('href');
        } else {
            // Fallback: buscar en el contenedor más cercano
            $link = $chk.closest('div, td, span.todo').parent().find('a');
            if ($link.length && $link.attr('href')) {
                href = $link.attr('href');
            }
        }

        // if the data-index attribute is set, this is a call from the page where the todos are defined
        if (param.index === undefined) param.index = -1;

        if ($spanTodoinnertext.length) {
            /**
             * Callback function update the todoitem when save request succeed
             *
             * @param {Array} data returned by ajax request
             */
            var whenCompleted = function (data) {
                //update date after edit and show alert when needed
                if (data.date) {
                    jQuery('input.todocheckbox').data('date', data.date);
                }
                if (data.message) {
                    alert(data.message);
                }
                //apply styling, or undo checking checkbox
                if (data.succeed) {
                    $chk.prop('checked', checked);

                    if (checked) {
                        if (param.strikethrough && !$spanTodoinnertext.parent().is("del")) {
                            $spanTodoinnertext.wrap("<ins></ins>");
                        }
                    } else {
                        if ($spanTodoinnertext.parent().is("ins")) {
                            $spanTodoinnertext.unwrap();
                        }
                    }
                }

                //release lock
                ToDoPlugin.locked = false;
            };

            // Obtener el token CSRF de DokuWiki
            var sectok = '';
            if (typeof JSINFO !== 'undefined' && JSINFO.sectok) {
                sectok = JSINFO.sectok;
            } else {
                var $sectokInput = jQuery('input[name="sectok"]');
                if ($sectokInput.length) {
                    sectok = $sectokInput.val();
                }
            }

            // Se envia parametro nuevo R**G + sectok para CSRF
            jQuery.post(
                DOKU_BASE + 'lib/exe/ajax.php',
                {
                    call: 'plugin_todo',
                    sectok: sectok,
                    mode: 'checkbox',
                    index: param.index,
                    pageid: param.pageid,
                    rutaid: href,
                    checked: checked ? "1" : "0",
                    date: param.date
                },
                whenCompleted,
                'json'
            ).fail(function (jqXHR, textStatus, errorThrown) {
                // Liberar lock en caso de error AJAX
                console.error("Error en checkbox todo:", textStatus, errorThrown);
                alert("Error al guardar el cambio. Recargue la página e intente de nuevo.");
                ToDoPlugin.locked = false;
            });
        } else {
            alert("Appropriate javascript element not found.\nReverting checkmark.");
            ToDoPlugin.locked = false;
        }
    },

    uncheckall: function () {
        if (ToDoPlugin.locked) {
            return;
        }
        ToDoPlugin.locked = true;

        var whenCompleted = function () {
            jQuery('input.todocheckbox').each(function() {
                jQuery(this).prop('checked', false);
            });

            jQuery('span.todoinnertext').each(function () {
                if (jQuery(this).parent().is("del")) {
                    jQuery(this).unwrap();
                }
            });

            jQuery('span.todouser').each(function () {
                jQuery(this).remove();
            });

            ToDoPlugin.locked = false;
        };

        // Obtener el token CSRF de DokuWiki
        var sectok = '';
        if (typeof JSINFO !== 'undefined' && JSINFO.sectok) {
            sectok = JSINFO.sectok;
        } else {
            var $sectokInput = jQuery('input[name="sectok"]');
            if ($sectokInput.length) {
                sectok = $sectokInput.val();
            }
        }

        jQuery.post(
            DOKU_BASE + 'lib/exe/ajax.php',
            {
                call: 'plugin_todo',
                sectok: sectok,
                mode: 'uncheckall',
                pageid: jQuery('input.todocheckbox:first').data().pageid
            },
        whenCompleted, 'json').fail(function () {
            console.error("Error en uncheckall todo");
            ToDoPlugin.locked = false;
        });
    }
};

jQuery(function () {
    // add handler to checkbox
    jQuery('input.todocheckbox').click(function (e) {
        e.preventDefault();
        e.stopPropagation();

        var $this = jQuery(this);
        // undo checking the checkbox
        $this.prop('checked', !$this.is(':checked'));

        ToDoPlugin.todo($this);
    });

    // add click handler to todotext spans when marked with 'clickabletodo'
    jQuery('span.todotext.clickabletodo').click(function () {
        //Find the checkbox node we need
        var $chk = jQuery(this).prevAll('input.todocheckbox:first');

        ToDoPlugin.todo($chk);
    });

    // add click handler to button to uncheck all todos on its page
    jQuery('button.todouncheckall').click(function () {
        ToDoPlugin.uncheckall();
    });
});