// Copyright (c) 2015, Xhiveframework Technologies Pvt. Ltd. and Contributors
// MIT License. See license.txt

xhiveframework.breadcrumbs = {
    all: {},

    preferred: {
        "File": "",
        "Dashboard": "Customization",
        "Dashboard Chart": "Customization",
        "Dashboard Chart Source": "Customization"
    },

    module_map: {
        'Core': 'Settings',
        'Email': 'Settings',
        'Custom': 'Settings',
        'Workflow': 'Settings',
        'Printing': 'Settings',
        'Setup': 'Settings',
        'Event Streaming': 'Tools',
        'Automation': 'Tools',
    },

    set_doctype_module(doctype, module) {
        localStorage["preferred_breadcrumbs:" + doctype] = module;
    },

    get_doctype_module(doctype) {
        return localStorage["preferred_breadcrumbs:" + doctype];
    },

    add(module, doctype, type) {
        let obj;
        if (typeof module === 'object') {
            obj = module;
        } else {
            obj = {
                module: module,
                doctype: doctype,
                type: type
            }
        }

        let breadcrumb = xhiveframework.breadcrumbs.current_page();
        breadcrumb = breadcrumb.split('/');
        this.all[xhiveframework.breadcrumbs.current_page()] = obj;

        this.update();
    },

    current_page() {
        return xhiveframework.get_route_str();
    },

    update() {
        var breadcrumbs = this.all[xhiveframework.breadcrumbs.current_page()];

        this.clear();
        if (!breadcrumbs) return this.toggle(false);

        if (breadcrumbs.type === 'Custom') {
            this.set_custom_breadcrumbs(breadcrumbs);
        } else {
            // workspace
            this.set_workspace_breadcrumb(breadcrumbs);

            // form / print
            let view = xhiveframework.get_route()[0];
            view = view ? view.toLowerCase() : null;
            if (breadcrumbs.doctype && ["print", "form"].includes(view)) {
                this.set_list_breadcrumb(breadcrumbs);
                this.set_form_breadcrumb(breadcrumbs, view);
            } else if (breadcrumbs.doctype && view === 'list') {
                this.set_list_breadcrumb(breadcrumbs);
            }
        }

        this.toggle(true);
    },

    set_custom_breadcrumbs(breadcrumbs) {
        const html = `<li><a href="${breadcrumbs.route}">${breadcrumbs.label}</a></li>`;
        this.$breadcrumbs.append(html);
    },

    set_workspace_breadcrumb(breadcrumbs) {
        // get preferred module for breadcrumbs, based on sent via module

        if (!breadcrumbs.workspace) {
            this.set_workspace(breadcrumbs);
        }

        if (breadcrumbs.workspace) {
            if (!breadcrumbs.module_info.blocked && xhiveframework.visible_modules.includes(breadcrumbs.module_info.module)) {
                $(`<li><a href="/app/${xhiveframework.router.slug(breadcrumbs.workspace)}">${__(breadcrumbs.workspace)}</a></li>`)
                    .appendTo(this.$breadcrumbs);
            }
        }

    },

    set_workspace(breadcrumbs) {
        // try and get module from doctype or other settings
        // then get the workspace for that module

        this.setup_modules();
        var from_module = this.get_doctype_module(breadcrumbs.doctype);

        if (from_module) {
            breadcrumbs.module = from_module;
        } else if (this.preferred[breadcrumbs.doctype] !== undefined) {
            // get preferred module for breadcrumbs
            breadcrumbs.module = this.preferred[breadcrumbs.doctype];
        }

        if (breadcrumbs.module) {
            if (this.module_map[breadcrumbs.module]) {
                breadcrumbs.module = this.module_map[breadcrumbs.module];
            }

            breadcrumbs.module_info = xhiveframework.get_module(breadcrumbs.module);

            // set workspace
            if (breadcrumbs.module_info && xhiveframework.boot.module_page_map[breadcrumbs.module]) {
                breadcrumbs.workspace = xhiveframework.boot.module_page_map[breadcrumbs.module];
            }
        }
    },

    set_list_breadcrumb(breadcrumbs) {
        const doctype = breadcrumbs.doctype;
        const doctype_meta = xhiveframework.get_doc('DocType', doctype);
        if ((doctype === "User" && !xhiveframework.user.has_role('System Manager'))
            || (doctype_meta && doctype_meta.issingle)) {
            // no user listview for non-system managers and single doctypes
        } else {
            let route;
            const doctype_route = xhiveframework.router.slug(xhiveframework.router.doctype_layout || doctype);
            if (xhiveframework.boot.treeviews.indexOf(doctype) !== -1) {
                let view = xhiveframework.model.user_settings[doctype].last_view || 'Tree';
                route = `${doctype_route}/view/${view}`;
            } else {
                route = doctype_route;
            }
            $(`<li><a href="/app/${route}">${__(doctype)}</a></li>`).appendTo(this.$breadcrumbs);
        }
    },

    set_form_breadcrumb(breadcrumbs, view) {
        const doctype = breadcrumbs.doctype;
        const docname = xhiveframework.get_route()[2];
        let form_route = `/app/${xhiveframework.router.slug(doctype)}/${docname}`;
        $(`<li><a href="${form_route}">${this.limit_string(__(docname))}</a></li>`).appendTo(this.$breadcrumbs);
        if (view === "form") {
            let last_crumb = this.$breadcrumbs.find('li').last();
            last_crumb.addClass('disabled');
            last_crumb.css("cursor", "copy");
            last_crumb.click((event) => {
                event.stopImmediatePropagation();
                xhiveframework.utils.copy_to_clipboard(last_crumb.text());
            });
        }
    },

    setup_modules() {
        if (!xhiveframework.visible_modules) {
            xhiveframework.visible_modules = $.map(xhiveframework.boot.allowed_workspaces, (m) => {
                return m.module;
            });
        }
    },

    rename(doctype, old_name, new_name) {
        var old_route_str = ["Form", doctype, old_name].join("/");
        var new_route_str = ["Form", doctype, new_name].join("/");
        this.all[new_route_str] = this.all[old_route_str];
        delete xhiveframework.breadcrumbs.all[old_route_str];
        this.update();
    },

    clear() {
        this.$breadcrumbs = $("#navbar-breadcrumbs").empty();
    },

    toggle(show) {
        if (show) {
            $("body").addClass("no-breadcrumbs");
        } else {
            $("body").removeClass("no-breadcrumbs");
        }
    },

    limit_string(string, limit = 25) {
        if (string && string.length >= limit) {
            return string.substring(0, limit) + ' ...';
        } else {
            return string;
        }
    }

}
