(function () {

    var app = angular.module('store', []);

    app.factory('categoriesDir', function () {
        var categories = {};

        categories.parentCategories = {};
        categories.childCategories = {};
        categories.all = {};

        return categories;
    });

    app.filter('itemNameLength', function () {
        return function (input) {
            if (input.length > 25) {
                return input.substring(0, 25) + "...";
            }

            return input;
        };
    });

    app.controller('navController', function ($rootScope, $scope, $http, categoriesDir) {
        $rootScope.title = "Loading...";

        $scope.selectCategory = function ($event, category) {
            $rootScope.$broadcast('categorySelect', category);
            $(".navtab").removeClass("active");
            $($event.target).addClass("active");
        };

        $scope.navInit = function (first, index) {
            if (first) {
                setTimeout(function () {
                    $("#navtab-" + index).click();
                    $("#navtab-" + index).addClass("active");
                }, 500);
            }
        };

        $http.get('/user/info/').success(function (data) {
            $scope.user = data.data;
            $rootScope.userCoins = data.data.store_points;

            $http.get('/categories/categories/').success(function (data) {
                var parentCategories = {};
                var childCategories = {};
                var all = {};

                $.each(data.data, function (key, val) {
                    if (val.store_category_parent === null) {
                        parentCategories[val.store_category_id] = val;
                    } else {
                        if (typeof (childCategories[val.store_category_parent]) === 'undefined') {
                            childCategories[val.store_category_parent] = {};
                        }

                        childCategories[val.store_category_parent][val.store_category_id] = val;
                    }

                    all[val.store_category_id] = val;
                });

                $scope.parentCategories = parentCategories;
                $scope.childCategories = childCategories;
                categoriesDir.parentCategories = parentCategories;
                categoriesDir.childCategories = childCategories;
                categoriesDir.all = all;
            });
        }).error(function (data, status, headers, config) {
            window.location.href = 'http://forums.playlunaris.com/';

            return false;
        });

        $scope.donate = function (userID) {
            window.open("https://extrinsicpay.com/widget/?widget=9a6daef1cff230791570271916e6681c53445375&user=" + userID, "_blank", "width=700, height=450")
        };
    });

    app.controller('bodyController', function ($rootScope, $scope, $http, categoriesDir) {
        $scope.childCategories = null;
        $scope.items = {};
        $scope.category = null;

        $scope.$on('categorySelect', function (event, category) {
            $scope.items = {};

            if (typeof (categoriesDir.childCategories[category]) !== 'undefined') {

                $scope.childCategories = categoriesDir.childCategories[category];
            } else {
                $scope.childCategories = null;

                $http.get('/category/items/' + category).success(function (data) {
                    $scope.items = data.data;

                    $scope.category = category;

                    $rootScope.title = categoriesDir.parentCategories[category].store_category_name;
                }).error(function (data, status, headers, config) {
                    alert(data.message);

                    return false;
                });
            }
        });

        $scope.loadCategory = function ($event, category) {
            $event.preventDefault();

            if ($scope.category === category) {
                return false;
            }

            $http.get('/category/items/' + category).success(function (data) {
                $scope.items = data.data;

                $(".cattabs").removeClass("active");

                $($event.target).parent().addClass("active");

                $rootScope.title = categoriesDir.all[category].store_category_name;

                $scope.category = category;
            }).error(function (data, status, headers, config) {
                alert(data.message);

                return false;
            });
        };

        $scope.catInit = function (first, category) {
            if (first) {
                $http.get('/category/items/' + category).success(function (data) {
                    $scope.items = data.data;

                    $scope.category = category;

                    $rootScope.title = categoriesDir.all[category].store_category_name;
                }).error(function (data, status, headers, config) {
                    alert(data.message);

                    return false;
                });
            }
        };

        $scope.setItem = function (item) {
            $rootScope.$broadcast('itemBuy', item);
        };
    });

    app.controller('modalController', function ($scope, $rootScope, $http) {
        $scope.item = {};

        $scope.$on('itemBuy', function (event, item) {
            $scope.item = item;
        });

        $scope.buyItem = function (item) {
            var data = $.param({ store_item_id: item });
            var config = {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                }
            }

            $http.post('/item/buy/' + item, data, config).success(function (data) {
                if (data.status === 0) {
                    $rootScope.userCoins = $rootScope.userCoins = data.data.store_points;
                }

                alert(data.message);
            }).error(function (data, status, headers, config) {
                alert(data.message);

                return false;
            });;
        };
    });
})();