'use strict';

(function (module) {

    module.service('BuildService', [
        '$http', '$q', 'Config',
        function ($http, $q, Config) {

            var getCurrentConfiguration = function() {
                var link = Config.BaseDirectory + '/ChromeDataService/GetCurrentBuildConfiguration';
                var deferred = $q.defer();

                var promise = $http({
                    method: 'GET',
                    url: link,
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': '*/*',
                        'Accept-Language': 'en-US,en;q=0.8'
                    }
                }).then(function (data) {
                    if (data.data) {
                        data = data.data;
                    }
                    deferred.resolve(data);
                }).catch(function (data) {
                    deferred.reject(data);
                });
                return deferred.promise;
            };

            return {
                GetCurrentConfiguration: getCurrentConfiguration
            }
        }
    ]);
})(angular.module('AutoSMART.Web.Build'));
"use strict";

(function (module) {

    module.component('builddealerselectioncomponent', {

        controller: ["$scope", "$location", "Config", "BuildService",
            function ($scope, $location, Config, BuildService) {
                var ctrl = this;
                ctrl.$onInit = function () {
                    document.title = "AutoSMART - Build";
                    BuildService.GetCurrentConfiguration().then(function (data) {
                        if (data.data) {
                            data = data.data;
                        }
                        ctrl.currentBuildConfig = data;
                    });
                };
            }],
        templateUrl: UrlContent('/App/Build/Views/BuildDealerSelectionComponent.html')
    });
})(angular.module('AutoSMART.Web.Build'));

"use strict";

(function (module) {

    module.component('buildvehiclemakemodelcomponent', {
        bindings: {
            currentbuild: '<'
        },
        controller: ["$scope", "$cookies", "$uibModal", "BuildEstimateCalcService", "NotificationService",
            function ($scope, $cookies, $uibModal, BuildEstimateCalcService,NotificationService) {
                
                var ctrl = this;

                var minMaxMonthly = function () {
                    ctrl.PaymentCalculatorSettings = BuildEstimateCalcService.getPaymentCalc();
                    ctrl.MinMonthlyPayment = BuildEstimateCalcService.CalcMonthlyPayment(ctrl.PaymentCalculatorSettings.minApr,
                        ctrl.BuildSelection.PurchasePrice,
                        ctrl.PaymentCalculatorSettings.CashRebate,
                        ctrl.PaymentCalculatorSettings.DownPayment,
                        ctrl.PaymentCalculatorSettings.TradeIn,
                        ctrl.PaymentCalculatorSettings.AmountOwned,
                        ctrl.PaymentCalculatorSettings.LoanDuration);
                    ctrl.MaxMonthlyPayment = BuildEstimateCalcService.CalcMonthlyPayment(ctrl.PaymentCalculatorSettings.maxApr,
                        ctrl.BuildSelection.PurchasePrice,
                        ctrl.PaymentCalculatorSettings.CashRebate,
                        ctrl.PaymentCalculatorSettings.DownPayment,
                        ctrl.PaymentCalculatorSettings.TradeIn,
                        ctrl.PaymentCalculatorSettings.AmountOwned,
                        ctrl.PaymentCalculatorSettings.LoanDuration);
                };

                ctrl.$onInit = function () {
                    ctrl.vehicle = ctrl.currentbuild;
                    ctrl.BuildSelection = JSON.parse($cookies.get("BuildSelection"));
                    minMaxMonthly();
                };

                 ctrl.openBuildEstimateCalculator = function() {
                    var buildEstimateCalc = $uibModal.open({
                        component: 'buildestimatepaymentcalculatorcomponent',
                        scope: $scope
                    });
                    $scope.buildEstimateClc = buildEstimateCalc;
                 };

                 NotificationService.subscribe("updatedMinMaxMonthly", $scope, minMaxMonthly);

            }],
        templateUrl: UrlContent('/App/Build/Views/BuildVehicleMakeModelComponent.html')
    });
})(angular.module('AutoSMART.Web.Build'));

"use strict";

(function (module) {

    module.component('builddealerlistcomponent', {
        bindings: {
            currentbuild: '<'
        },
        controller: ["$scope", "DealerSearchService", "DealerSearchFilterService", "Config", "$uibModal", "$cookies", "CalculatorService", "NotificationService",
            function ($scope, DealerSearchService, DealerSearchFilterService, Config, $uibModal, $cookies, CalculatorService, NotificationService) {
                var ctrl = this;
                ctrl.isLoading = true;
                ctrl.data = {};
                ctrl.vehicle = ctrl.currentbuild;

                ctrl.$onInit = function() {
                    DealerSearchFilterService.Init().then(function () {
                        angular.forEach(DealerSearchFilterService.selectedFilters.filters.MakeModel, function (makeModel) {
                            var obj = makeModel;
                            if (!obj.MakeId) {
                                obj.MakeId = ctrl.vehicle.MakeID;
                            }
                            if (!obj.ModelId) {
                                obj.ModelId = ctrl.vehicle.ModelID;
                            }
                        });

                        DealerSearchService.RetrieveDealers().then(function () {
                            ctrl.allDealers = DealerSearchService.allDealers;
                            ctrl.isLoading = false;
                        });

                        CalculatorService.Init().then(function () {
                            ctrl.calcContent = CalculatorService.CalcContent;
                        });
                    });

                    ctrl.svgcontent = {
                        TextColor: Config.SecondaryColor.toLowerCase(),
                        ShieldColor: Config.SecondaryColor.toLowerCase()
                    };

                    ctrl.buttonsetting = {
                        Title: "SELECT",
                        StyleClass: "btn btn-inverse btn-select"
                    };
                    
                    ctrl.BuildSelection = JSON.parse($cookies.get("BuildSelection"));
                    ctrl.vehicle.BaseMSRP = ctrl.BuildSelection.PurchasePrice;
                };

                ctrl.selectButtonClicked = function () {
                    var modalInstance = $uibModal.open({
                        component: "vehicleleadcomponent",
                        scope: $scope,
                        windowClass: 'vdplead-modal-content'
                    });

                    $scope.modalBuildLeadInstance = modalInstance;
                };

                ctrl.openRating = function (clientCode, isValid) {
                    if (isValid) {
                        ctrl.data.ClientCode = clientCode;
                        var ratingInstance = $uibModal.open({
                            component: 'dealerratercomponent',
                            scope: $scope,
                            windowClass: 'dealerrater-modal-content'
                        });

                        $scope.ratingInstance = ratingInstance;
                    }
                };

                ctrl.open = function() {
                    var interstitial = $uibModal.open({
                        animation: true,
                        ariaDescribedBy: 'interstitial-modal-body',
                        templateUrl: '/app/Common/views/_InterstitialModal.html',
                        component: 'interstitialmodal',
                        scope: $scope,
                        windowClass: 'interstitial-modal-content'
                    });

                    $scope.Interstitial = interstitial;
                };

                var doSearch = function() {
                    DealerSearchService.RetrieveDealers().then(function () {
                        ctrl.allDealers = DealerSearchService.allDealers;
                        ctrl.isLoading = false;
                    });
                }
                NotificationService.subscribe("saveZipCode", $scope, doSearch);
            }],
        templateUrl: UrlContent('/App/Build/Views/BuildDealerListComponent.html')
    });
})(angular.module('AutoSMART.Web.Build'));

