// -*- coding: utf-8 -*-

//////////////////////////////////////////////////
// La macro todo
// cette macro affiche une case à cocher qu'il est possible de cocher pour signifier que l'action
// associée à la case à cocher est effectuée
// arguments: text done= dispdate=

var lastTodoId;

var displayTiddler_patchedByTodo = window.displayTiddler;
window.displayTiddler = function(src, title, state, highlightText, highlightCaseSensitive, animate, slowly) {
    // réinitialiser le numéro du dernier todo à chaque affichage
    lastTodoId = 0;
    displayTiddler_patchedByTodo(src, title, state, highlightText, highlightCaseSensitive, animate, slowly);
}

config.macros.todo = {};
config.macros.todo.handler = function(place, macroName, params) {
    var tiddler = findContainingTiddler(place);
    if (!tiddler) return;

    var title = tiddler.id.substr(7);
    var todoId = lastTodoId++;
    var cbId = "todo" + todoId + "@" + title;
    var wrapperId = "todoWrapper" + todoId + "@:" + title;

    var options = new Options(params);
    var done = options.done? true: false;
    var dispdate = Bool.valueOf(options.get("dispdate"));
    var text = (options.args[0]? options.args[0]: "todo #" + todoId) + (done && dispdate? " (fait le " + options.done + ")": "");
    var onClick = function(e) {
        if (!e) var e = window.event;

        if (!readOnly) {
            // lire les infos sur le tiddler
            var body = store.getTiddlerText(title);
            var tags = Tiddlers.getTags(title);

            // trouver la macro <<todo qui correspond à todoId
            var id = 0;
            var re_macro = new RegExp("<<todo(?:\\s*)([^>]*)>>", "mg");
            do {
                var mo = re_macro.exec(body);
                if (mo) {
                    if (id++ == todoId) break;
                }
            } while (mo);

            // changer la valeur
            if (mo) {
                var now = new Date();

                options = new Options(mo[1].readMacroParams());
                var done = options.done? false: true; // logique inversée!
                if (done) {
                    options.done = now.formatFr();
                } else {
                    delete options.done;
                }
                body = body.substr(0, mo.index) + "<<todo" + options.toParams() + ">>" + body.substr(mo.index + mo[0].length);

                lastTodoId = 0;
                store.saveTiddler(title, title, body, config.options.txtUserName, now, tags);
                if (config.options.chkAutoSave) saveChanges();
            }
        }

        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        return false;
    };

    var span = createTiddlyElement(place, "span");
    var cb = createTiddlyElement(span, "input");
    cb.type = "checkbox";
    cb.id = cbId;
    cb.checked = done;
    cb.onclick = onClick;
    var label = createTiddlyElement(span, "label");
    label.setAttribute("for", cbId);
    var wrapper = createTiddlyElement(label, "span", wrapperId);
    if (done) {
        wrapper.setAttribute("style", "text-decoration: line-through;");
    }
    createTiddlyText(wrapper, text);
}
