'use strict';

var HomeCtrl = ['$scope', '$location', '$http', 'dateFilter', 'messages', 'dashboard', 'Message', function ($scope, $location, $http, dateFilter, messages, dashboard, Message) {
    $scope.chosen = messages.Type;
    $scope.dashboard = dashboard;
    $scope.info = messages.Threads;
    $scope.tags = messages.Tags;
    $scope.getMessages = function (type) {
        Message.query({type: type}, function (result) {
            $scope.info = result.Threads;
            $scope.tags = result.Tags;
            $scope.chosen = result.Type;
        });
    };
    $scope.getTags = function (tag) {
        Message.query({type: $scope.Type, tag: tag}, function (result) {
            $scope.info = result.Threads;
            $scope.tags = result.Tags;
            $scope.chosen = result.Type;
        });
    };
    $scope.getDashboard = function () {
        var start_date = angular.isDate($scope.dashboard.StartDate) ? dateFilter($scope.dashboard.StartDate, 'dd-MMM-yyyy') : $scope.dashboard.StartDate;
        var finish_date = angular.isDate($scope.dashboard.FinishDate) ? dateFilter($scope.dashboard.FinishDate, 'dd-MMM-yyyy') : $scope.dashboard.FinishDate;
        return $http({ method: 'GET', url: '/api/Dashboard', params: {s: start_date, f: finish_date}}).then(function (data, status, headers, config) {
            $scope.dashboard = data.data;
            formatData();
        });
    };
    function formatData() {
        $scope.Data = {};
        if ('Cash' in $scope.dashboard) {
            $scope.Data.HasCash = true;
            $scope.Data.Cash = {
                Pie: _.chain($scope.dashboard.Cash.Details)
                    .reduce(function (accumulator, value, index, collection) {
                        if (accumulator.length < 4) {
                            accumulator.push(value);
                        } else if (accumulator.length === 4) {
                            accumulator.push({Name: 'Others', Amount: value.Amount});
                        } else {
                            accumulator[4].Amount += value.Amount;
                        }
                        return accumulator;
                    }, [])
                    .map(function (item) {
                        return {label: item.Name.substr(0, 5), value: item.Amount};
                    })
                    .value(),
                LinePlusBar: [
                    {
                        key: "Total Cash",
                        values: _.map($scope.dashboard.Cash.Daily, function (item) {
                            return {label: new Date(item.Date).getTime(), value: item.Amount};
                        })
                    },
                    {
                        key: "Cash Flow",
                        bar: true,
                        values: _.map($scope.dashboard.Cash.Changes, function (item) {
                            return {label: new Date(item.Date).getTime(), value: item.Amount};
                        })
                    }
                ],
                Total: _.last($scope.dashboard.Cash.Daily).Amount
            };
        }
        if ('Creditors' in $scope.dashboard) {
            $scope.Data.HasCreditors = true;
            $scope.Data.Creditors = {
                Pie: _.chain($scope.dashboard.Creditors.Details)
                    .reduceRight(function (accumulator, value, index, collection) {
                        if (accumulator.length < 4) {
                            accumulator.push(value);
                        } else if (accumulator.length === 4) {
                            accumulator.push({Name: 'Others', Amount: value.Amount});
                        } else {
                            accumulator[4].Amount += value.Amount;
                        }
                        return accumulator;
                    }, [])
                    .map(function (item) {
                        return {label: item.Name.substr(0, 5), value: item.Amount * -1};
                    })
                    .value(),
                LinePlusBar: [
                    {
                        key: "Total Creditors",
                        values: _.map($scope.dashboard.Creditors.Daily, function (item) {
                            return {label: new Date(item.Date).getTime(), value: item.Amount * -1};
                        })
                    },
                    {
                        key: "Change in Creditors",
                        bar: true,
                        values: _.map($scope.dashboard.Creditors.Changes, function (item) {
                            return {label: new Date(item.Date).getTime(), value: item.Amount * -1};
                        })
                    }
                ],
                Total: _.last($scope.dashboard.Creditors.Daily).Amount * -1
            };
        }
    }
    formatData();
}];
HomeCtrl.resolve = {
    messages: ['Message', function (Message) {
        return Message.query({type: 'open'}).$promise;
    }],
    dashboard: ['$http', function ($http) {
        return $http.get('/api/Dashboard', {}).then(function (data, status, headers, config) {
            return data.data;
        });
    } ]
};