'use strict';

var SettingsCtrl = ['$scope', '$http', 'dateFilter', '$modal', 'lockInfo', 'Product', function ($scope, $http, dateFilter, $modal, lockInfo, Product) {
    $scope.lockInfo = lockInfo.data;
    $scope.rebaseDate = '';
    $scope.setLockDate = function () {
        if ($scope.lockInfo.Start.Locked) {
            if ($scope.lockInfo.Start.Rolling) {
                $scope.lockInfo.Start.Days = parseInt($scope.lockInfo.Start.Days, 10);
            } else if (angular.isDate($scope.lockInfo.Start.Date)) {
                $scope.lockInfo.Start.Date = dateFilter($scope.lockInfo.Start.Date, 'dd-MMM-yyyy');
            }
        }

        if ($scope.lockInfo.Finish.Locked) {
            if ($scope.lockInfo.Finish.Rolling) {
                $scope.lockInfo.Finish.Days = parseInt($scope.lockInfo.Finish.Days, 10);
            } else if (angular.isDate($scope.lockInfo.Finish.Date)) {
                $scope.lockInfo.Finish.Date = dateFilter($scope.lockInfo.Finish.Date, 'dd-MMM-yyyy');
            }
        }
        return $http.post('/api/LockInfo', $scope.lockInfo).
            success(function (data) {
                $scope.lockInfo = data;
                $scope.toasts.push({Type: 'Success', Message: ''});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };

    $scope.clearLockDate = function () {
        return $http.delete('/api/LockInfo').
            success(function (data) {
                $scope.lockInfo = data;
                $scope.toasts.push({Type: 'Success', Message: ''});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };

    $scope.confirmRebase = function () {
        var modalInstance = $modal.open({
            backdrop: true,
            templateUrl: '/template/modal/confirm.html',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.title = "Rebase Data";
                $scope.body = "Are you sure? This cannot be undone.";
                $scope.isDelete = true;
                $scope.ok = function () {
                    $modalInstance.close();
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }]
        });
        modalInstance.result.then(function () {
            $scope.rebaseData();
        });
    };
    $scope.rebaseData = function () {
        if ($scope.rebaseDate === '') {
            return;
        }
        if (angular.isDate($scope.rebaseDate)) {
            $scope.rebaseDate = dateFilter($scope.rebaseDate, 'dd-MMM-yyyy');
        }
        $http({method: 'POST', url: '/api/Rebase/' + $scope.rebaseDate}).
            success(function (data) {
                $scope.toasts.push({Type: 'Success', Message: 'Data rebased!'});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };

    $scope.confirmResetStock = function () {
        var modalInstance = $modal.open({
            backdrop: true,
            templateUrl: '/template/modal/confirm.html',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.title = "Reset Stock";
                $scope.body = "Are you sure? This cannot be undone.";
                $scope.isDelete = true;
                $scope.ok = function () {
                    $modalInstance.close();
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }]
        });
        modalInstance.result.then(function () {
            $scope.resetStock();
        });
    };
    $scope.resetStock = function () {
        if (angular.isUndefined($scope.resetDate) || $scope.resetDate === '') {
            return;
        }
        if (angular.isUndefined($scope.stockDate) || $scope.stockDate === '') {
            return;
        }
        if (angular.isUndefined($scope.product) || $scope.product === '') {
            return;
        }
        if (angular.isUndefined($scope.qty) || $scope.qty === '') {
            return;
        }
        var resetDate = angular.isDate($scope.resetDate) ? dateFilter($scope.resetDate, 'dd-MMM-yyyy') : $scope.resetDate;
        var stockDate = angular.isDate($scope.stockDate) ? dateFilter($scope.stockDate, 'dd-MMM-yyyy') : $scope.stockDate;
        var qty = Number($scope.qty);
        $http({method: 'POST', url: '/api/ResetStock/' + $scope.product.ProductID, params: {StockDate: stockDate, ResetDate: resetDate, Quantity: qty}}).
            success(function (data) {
                $scope.toasts.push({Type: 'Success', Message: 'Data rebased!'});
            }).
            error(function (errorMessage) {
                $scope.toasts.push({Type: 'Danger', Message: errorMessage});
            });
    };
    $scope.products = function ($viewValue) {
        return Product.autocomplete({term: $viewValue, count: 20}).$promise;
    };
}];

SettingsCtrl.resolve = {
    lockInfo: ['$http', function ($http) {
        return $http.get('/api/LockInfo', {}).then(function (data, status, headers, config) {
            return data;
        });
    }]
};

