angular.module("app").directive("quantTabs", function ($timeout, AppService) {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "../app/directives/quantTabs/quantTabs.html",
        scope: {
            instance: '=',
            translateHeaders: '@',
            showNumberOfRecords: '='
        },
        link: function (scope, element, attrs) {

            //init tabs
            var activeTab = false;
            var tabs = scope.instance.tabs;

            scope.clickedTab = null;

            for (var i = 0; i < tabs.length; i++) {
                if (tabs[i].active) {
                    activeTab = true;
                    break;
                }

                if(!tabs[i].htmlId) {
                    tabs[i].htmlId = tabs[i].dataItem;

                    if ("idPrefix" in scope.instance && scope.instance.idPrefix != "") {
                        tabs[i].htmlId = scope.instance.idPrefix + "-" + tabs[i].htmlId;
                    }
                }
            }

            if (!activeTab && tabs.length > 0) {
                tabs[0].active = true;
            }

            scope.hideMenu = function () {
                $("#" + scope.instance.dropdownMenuId).removeClass("open");
            }

            scope.showMenu = function (e, tab) {
                var dropdownMenu = $("#" + scope.instance.dropdownMenuId);
                var thisOpened = dropdownMenu.hasClass("open");
                if (!thisOpened) {
                    scope.instance.clickedTab = tab;
                    var parentElement = $(e.target).parent().position();
                    dropdownMenu.addClass("open").css("top", (parentElement.top + 33) + "px").css("left", (parentElement.left + $(e.target).parent().width() + 10) + "px");
                }
                else {
                    dropdownMenu.removeClass("open");
                }
            };

            //functions 
            scope.onSelectTab = function (tab) {
                var tabs = scope.instance.tabs;

                var activeTab = null;

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i] == tab) {
                        tabs[i].active = true;

                        activeTab = tabs[i];
                    } else {
                        tabs[i].active = false;
                    }
                }

                if (scope.instance.onSelectTab) {
                    scope.instance.onSelectTab(activeTab);
                }

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i].tabChanged) {
                        tabs[i].tabChanged();
                    }
                }

                $timeout(function () {
                    window.dispatchEvent(new Event('resize'));
                }, 0, false);
            }

            scope.instance.selectTab = function (dataItem, switchData) {
                var tab = getItem(scope.instance.tabs, 'dataItem', dataItem);
                if (!tab || tab.display === false) {
                    return;
                }

                var tabs = scope.instance.tabs;

                var activeTab = null;

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i].dataItem == dataItem) {
                        tabs[i].active = true;

                        activeTab = tabs[i];
                        activeTab.switchData = switchData;
                    } else {
                        tabs[i].active = false;
                    }
                }

                if (scope.instance.onSelectTab) {
                    scope.instance.onSelectTab(activeTab);
                }

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i].tabChanged) {
                        tabs[i].tabChanged();
                    }
                }

                $timeout(function () {
                    window.dispatchEvent(new Event('resize'));
                }, 0, false);
            }

            scope.instance.getActiveTab = function () {
                var tabs = scope.instance.tabs;

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i].active) {
                        var tab = tabs[i];
                        tab.index = i;

                        return tab;
                    }
                }
            }

            scope.instance.switchToNextTab = function () {
                var tabs = scope.instance.tabs;

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i].active && i < tabs.length - 1) {
                        scope.onSelectTab(tabs[i + 1]);
                        return;
                    }
                }
            }

            scope.instance.switchToPrevTab = function () {
                var tabs = scope.instance.tabs;

                for (var i = 0; i < tabs.length; i++) {
                    if (tabs[i].active && i > 0) {
                        scope.onSelectTab(tabs[i - 1]);
                        return;
                    }
                }
            }

            scope.instance.switchToTab = function (tabTitle) {
                var tab = getItem(scope.instance.tabs, 'title', tabTitle);
                if (tab) {
                    scope.onSelectTab(tab);
                } else {
                    console.error("Tab '" + tabTitle + "' doesn't exist");
                }
            }

            scope.instance.displayTab = function (dataItem, display) {
                var tabs = scope.instance.tabs;

                for (var i = 0; i < tabs.length; i++) {
                    var tab = tabs[i];

                    if (tab.dataItem == dataItem) {
                        tab.display = display;

                        if (display) {
                            //scope.onSelectTab(tab); //nepouzivat !!!
                        } else if (tab.active) {
                            scope.instance.switchToNextTab();
                        }
                    }
                }
            }

            scope.$on('$destroy', function () {

            });

            function waitUntilInitialized() {
                if (scope.$$phase) { //$digest or $apply running
                    $timeout(function () {
                        waitUntilInitialized();
                    }, 0, false);
                } else {
                    if (scope.instance.onSelectTab && scope.instance.tabs) {
                        $timeout(function () {
                            scope.onSelectTab(scope.instance.tabs[0]); //automatically select the first tab

                            if (scope.instance.afterInit) {
                                scope.instance.afterInit();
                            }
                            $(element).children(".tabs-header").on("click", '.help', function (e) {
                                $(element).children(".tabs-header").find('.help').not(this).tooltip("destroy");
                                $(this).tooltip({
                                    "trigger": "manual",
                                    "html": true,
                                    "template": '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner tooltip-databank-help"></div></div>',
                                }).tooltip("show");
                                e.preventDefault();
                                e.stopPropagation();
                            })
                            /* $(element).children(".tabs-header").find('.help').tooltip({
                                "trigger": "manual",
                                "html": true,
                                "template": '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner tooltip-databank-help"></div></div>',
                            }); */
                            $("body,.sqn-databanks").click(function () {
                                $(element).children(".tabs-header").find('.help').tooltip("destroy");
                            });
                            $("body").on("click", ".tooltip-databank-help a", function () {
                                AppService.openHelp($(this).data("helpurl"));
                            })
                        }, 200, false);
                    }
                }
            }

            waitUntilInitialized();

        }
    };
});