(function () {
    'use strict';
    angular
        .module('controleApp')
        .controller('configController', configController);

    configController.$inject = ['$scope', 'Consulta', 'Comando', 'paginationService'];

    function configController($scope, Consulta, Comando, paginationService) {
        /*jshint validthis:true*/
        this.toggleModal = {};
        this.notificationMessages = {};

        var configCtrl = this;

        angular.extend(configCtrl, {
            initialize: LimparFiltro,
            limparFiltro: LimparFiltro,
            filterToGrid: filterToGrid,
            removerConfiguracao: removerConfiguracao
        });

        $scope.take = 10;
        $scope.skip = 0;
        $scope.type = "DataCriacao";
        $scope.orderAsc = false;

        $scope.OrderBy = OrderBy;
        paginationService.Consultar = Consultar;
        paginationService.successCallBack = successCallBack;
        paginationService.filterSuccessCallBack = filterSuccessCallBack;

        function Consultar(take, skip, callback) {
            $(".loading-div").fadeIn();
            $scope.take = take;
            $scope.skip = skip;
            var obj = {
                take: take,
                skip: skip,
                typeOrder: $scope.type,
                orderAsc: $scope.orderAsc
            };
            Consulta.ConsultaConfiguracoes(obj, callback, errorCallBack);
        }

        function LimparFiltro() {
            paginationService.Skip = 0;
            var obj = {
                take: $scope.take,
                typeOrder: $scope.type
            };
            Consulta.LimparFiltro(obj, successCallBack, errorCallBack);
        }

        function successCallBack(data) {
            $scope.Configs = data.data.Grid.Data;
            paginationService.Total = data.data.QuantidadeTotal;
            paginationService.AtualizarRodapePaginacao();
            $(".loading-div").fadeOut();


            configCtrl.data = data.data.Grid.Data;
            configCtrl.colunas = data.data.Grid.columns;
            configCtrl.itensFiltro = data.data.Filtro.ItensFiltro;

            configCtrl.arrayFilter = [];
            configCtrl.currentFilter = [];
            configCtrl.currentPathTemplate = '';

            if (localStorage.MensagemSucesso) {
                $scope.showSuccess(localStorage.MensagemSucesso);
                localStorage.removeItem("MensagemSucesso");
            }
        }

        function filterSuccessCallBack(data) {
            $scope.Configs = data.data.Grid.Data;
            paginationService.Total = data.data.QuantidadeTotal;
            paginationService.AtualizarRodapePaginacao();
            $(".loading-div").fadeOut();
        }

        function filterToGrid() {
            //TODO: consultar de acordo com os par�metros
            paginationService.Skip = 0;
            var obj = {
                take: $scope.take,
                skip: skip,
                typeOrder: $scope.type,
                orderAsc: $scope.orderAsc,
                ItensFiltro: configCtrl.arrayFilter
            };
            Comando.AplicarFiltro(obj, filterSuccessCallBack, errorCallBack);
            //do obj vm.arrayFilter
            //console.log(configCtrl.arrayFilter);
            // depois de filtrar, esconde o componente de grid
            configCtrl.toggleTool();
        }

        function errorCallBack(error) {
            $scope.showError(error.statusText);
            $(".loading-div").fadeOut();
        }

        function OrderBy(type) {
            $(".loading-div").fadeIn();
            $scope.type = type;
            $scope.orderAsc = !$scope.orderAsc;
            var obj = {
                take: $scope.take,
                skip: $scope.skip,
                typeOrder: $scope.type,
                orderAsc: $scope.orderAsc
            };
            Consulta.ConsultaConfiguracoes(obj, successCallBack, errorCallBack);
        }

        function removerConfiguracao() {
            $(".loading-div").fadeIn();
            var obj = { codigo: this.toggleModal.codigoConfiguracao };
            this.toggleModal.hideModal();

            Comando.ExcluirConfiguracao(obj, (data) => {
                if (data.data.IsSucesso) {
                    $scope.showSuccess(data.data.Mensagem);
                    LimparFiltro();
                } else {
                    $scope.showError(data.data.Mensagem);
                    $(".loading-div").fadeOut();
                }
            }, (error) => {
                $scope.showError(error.statusText);
                $(".loading-div").fadeOut();
            });
        }
    }

})();
