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

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

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

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

            //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);
                }
            }

            $rootScope.goToPanel = function (dataItem) {
                console.log("goToPanel");
                scope.instance.selectTab(dataItem);
            }

            scope.instance.selectTab = function (dataItem) {
                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];
                    } else {
                        tabs[i].active = false;
                    }
                }

                if (scope.instance.onSelectTab) {
                    scope.instance.onSelectTab({
                        dataItem: dataItem
                    });
                }
            }

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

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

            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");
                }
            }

            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();
                            }
                        }, 200, false);
                    }
                }
            }

            waitUntilInitialized();
        }
    };
});