﻿(function () {
    'use strict';

    angular
        .module('app.widgets')
        .directive('saveRate', saveRate);

    saveRate.$inject = ['$mdSidenav', '$http', 'endpoints', 'errorsService', '$rootScope', 'toastService', 'dateService'];

    function saveRate($mdSidenav, $http, endpoints, errorsService, $rootScope, toastService, dateService) {

        var directive = {
            link: link,
            restrict: 'E',
            replace: false,
            templateUrl: 'app/widgets/save-rate/save-rate.tpl.html',
            scope: {}
        };

        return directive;

        function link(scope, element, attrs) {

            var vm = scope.vm = {};
            vm.isUpdate = false;

            vm.rates = [
                { fuelType: 1, name: 'Diesel', hvOnPR: null, other: null },
                { fuelType: 2, name: 'Petrol', hvOnPR: null, other: null },
                { fuelType: 3, name: 'Biodiesel Blend B5', hvOnPR: null, other: null },
                { fuelType: 4, name: 'Biodiesel Blend B20', hvOnPR: null, other: null },
                { fuelType: 5, name: 'Biodiesel B100', hvOnPR: null, other: null },
                { fuelType: 6, name: 'Ethanol Blend E10', hvOnPR: null, other: null },
                { fuelType: 7, name: 'Ethanol Blend E85', hvOnPR: null, other: null },
                { fuelType: 8, name: 'LPG (duty paid)', hvOnPR: null, other: null },
                { fuelType: 9, name: 'LNG (duty paid)', hvOnPR: null, other: null },
                { fuelType: 10, name: 'CNG (duty paid)', hvOnPR: null, other: null },
                { fuelType: 11, name: 'Other', hvOnPR: null, other: null },
            ];

            scope.$on('save-rate', function (e, args) {
                vm.disableYear = false;
                vm.saving = false;
                vm.dateFrom = null;
                vm.dateTo = null;
                vm.periodId = null;
                vm.isUpdate = args.isUpdate;

                for (var i = 0; i < vm.rates.length; i++)
                {
                    vm.rates[i].hvOnPR = null;
                    vm.rates[i].other = null;
                }

                if (args.isUpdate && args.items && args.period) {

                    vm.dateFrom = new Date(args.period.dateFrom);
                    vm.dateTo = new Date(args.period.dateTo);
                    vm.periodId = args.period.ratePeriodId;

                    for(var i = 0; i < vm.rates.length; i++)
                    {
                        var currentI = vm.rates[i];

                        for (var j = 0; j < args.items.length; j++)
                        {
                            var currentJ = args.items[j]

                            if(currentI.fuelType == currentJ.fuelType)
                            {
                                currentI.hvOnPR = currentJ.hvOnPR;
                                currentI.other = currentJ.other;
                            }
                        }
                    }
                }
                else
                    vm.item = {};

                vm.errors = null;
                vm.toggle();
            });

            vm.toggle = function () {
                $mdSidenav('save-rate').toggle();
                cleanForms();
            };

         
            element.on('$destroy', function () {
                cleanup();
            });

            vm.save = function () {

                var rq = {
                    periodId: vm.periodId,
                    rates: vm.rates
                }

                var df = new Date(angular.copy(vm.dateFrom));
                var dt = new Date(angular.copy(vm.dateTo));

                rq.dateFrom = dateService.prepareDateStart(df);
                rq.dateTo = dateService.prepareDateEnd(dt);

                vm.saving = true;

                $http.post(endpoints.SaveRates, rq).then(
                    function (success) {
                        if (success.data.isSuccesful) {
                            toastService.show('Saved successfully');
                            $rootScope.$broadcast('owner_changed');
                            vm.toggle();
                        }
                        else {



                            vm.ratesForm = success.data.errorMessages;
                        }

                        vm.saving = false;
                    },
                    function (error) {
                        vm.saving = false;
                    })
            }

            function cleanup() {

            }

            function cleanForms() {
                if (scope.ratesForm) {
                    vm.rateFormErrors = null;
                    scope.ratesForm.$setPristine();
                    scope.ratesForm.$setUntouched();
                }
            }
        }
    }
})();
