angular
        .module('carfriend')
        .controller('WildshieldCtrl', WildshieldCtrl);

function WildshieldCtrl($scope, $rootScope, $filter, $ionicPopup, $cordovaCamera, $cordovaGeolocation, $interval, $timeout, $window, roundProgressService) {
    $scope.current = 27;
    $scope.max = 50;
    $scope.offset = 0;
    $scope.timerCurrent = 0;
    $scope.uploadCurrent = 0;
    $scope.stroke = 45;
    $scope.radius = 90;
    $scope.isSemi = false;
    $scope.rounded = false;
    $scope.responsive = false;
    $scope.clockwise = false;
    $scope.currentColor = '#45ccce';
    $scope.bgColor = '#eaeaea';
    $scope.duration = 800;
    $scope.currentAnimation = 'easeOutCubic';
    $scope.animationDelay = 0;

    $scope.options = {
        loop: false,
        speed: 500,
//                effect: 'fade',
//                fade: {
//                    crossFade: true
//                },
        paginationHide: true,
        direction: 'horizontal',
        onSlideChangeStart: function () {
            console.log($scope.slider.activeIndex);
        },
    }
    $scope.data = {};
    $scope.$watch('data.slider', function (nv, ov) {
        $scope.slider = $scope.data.slider;
    })

    $scope.animations = [];

    angular.forEach(roundProgressService.animations, function (value, key) {
        $scope.animations.push(key);
    });

//            $scope.getStyle = function () {
//                var transform = ($scope.isSemi ? '' : 'translateY(-50%) ') + 'translateX(-50%)';
//
//                return {
//                    'top': $scope.isSemi ? 'auto' : '50%',
//                    'bottom': $scope.isSemi ? '5%' : 'auto',
//                    'left': '50%',
//                    'transform': transform,
//                    '-moz-transform': transform,
//                    '-webkit-transform': transform,
//                    'font-size': $scope.radius / 3.5 + 'px'
//                };
//            };

//            $scope.getColor = function () {
//                return $scope.gradient ? 'url(#gradient)' : $scope.currentColor;
//            };

    var getPadded = function (val) {
        return val < 10 ? ('0' + val) : val;
    };

    $scope.startSecure = false;

    $scope.secureMe = function () {
        $scope.startSecure = true;
        getCurrentAddress(function (addr) {
            console.log(addr);
            $scope.parking = addr;
        });
        $scope.hours = 2;
        $scope.minutes = 59;
        $scope.time = getPadded($scope.hours) + ':' + getPadded($scope.minutes);
        $interval(function () {
            if ($scope.minutes == 0) {
                $scope.hours -= 1;
                $scope.minutes = 59;
            }
            $scope.minutes -= 1;
            $scope.time = getPadded($scope.hours) + ':' + getPadded($scope.minutes);
        }, 500);

    }
    function getCurrentAddress(callback) {
        console.log("getCurrentAddress");
        var options = {
            timeout: 10000,
            enableHighAccuracy: true
        };
        // var latlng;
        var geocoder = new google.maps.Geocoder;

        $cordovaGeolocation.getCurrentPosition(options).then(function (position) {
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            geocoder.geocode({'location': latlng}, function (results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        callback(results[1].formatted_address);

                    } else {
                        window.alert('No results found');
                    }
                } else {
                    window.alert('Geocoder failed due to: ' + status);
                }
            });
        }, function (error) {
            console.log("Could not get location. GPS is disabled");
        });
    }
}