(function () {
    'use strict';

    var oneKeyApp = angular.module('oneKeyApp');

    oneKeyApp.directive('map', ['localize', 'dates' ,function (metLocalize, metDates) {
        return {
            restrict: 'E',
            replace: true,
            template: '<div></div>',
            scope: {
                item: '='
            },
            link: function (scope, element, attrs) {
                var latitude = scope.item.hasTickAttached ? scope.item.tick.latitude : scope.item.latitude;
                var longitude = scope.item.hasTickAttached ? scope.item.tick.longitude : scope.item.longitude;

                if (scope.item.lastSeen === null || scope.item.lastSeen < scope.item.lastScanned) {
                    latitude = scope.item.scanLatitude;
                    longitude = scope.item.scanLongitude;
                }

                var center = new google.maps.LatLng(latitude, longitude);
                scope.messages = {
                    approximateAddress: metLocalize('messages', 'ApproximateAddress'),
                    noResultsFound: metLocalize('messages', 'NoResultsFound'),
                    clickApproximateAddress: metLocalize('messages', 'ClickApproximateAddress'),
                    lastSeen: metLocalize('fieldNames', 'LastSeen')
                }
                var map_options = {
                    zoom: 17,
                    center: center,
                    mapTypeControlOptions: {
                        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                        position: google.maps.ControlPosition.TOP_RIGHT
                    },
                };

                var map = new google.maps.Map(element[0], map_options);
                var infowindow = new google.maps.InfoWindow;

                // configure marker
                var marker_options = {
                    map: map,
                    position: center
                };

                // create marker
                var marker = new google.maps.Marker(marker_options);

              

                var accuracy = scope.item.quality;
                if (accuracy <= 0) {
                    accuracy = 65;
                }

                var circle = new google.maps.Circle({
                    strokeWeight: 0,
                    fillColor: '#076AE6',
                    fillOpacity: 0.2,
                    map: map,
                    center: center,
                    radius: accuracy + 30.48
                });

                if (circle.getBounds() !== null) {
                    map.fitBounds(circle.getBounds());
                }
                
                

                window.setTimeout(function () {
                    google.maps.event.trigger(map, 'resize');
                    var adjustedCenter = new google.maps.LatLng(latitude, longitude)
                    map.setCenter(adjustedCenter);
                }, 500);
            }
        }
    }]);
}());
