/**
 * @author monitour
 * created on 27.07.2017
 */
(function () {
    'use strict';

    angular.module('Monitour.pages.sites')
        .controller('sitesNewCtrl', sitesNewCtrl);

    /** @ngInject */
    function sitesNewCtrl($scope, $rootScope, ClientsService, SweetAlert, SitesService) {
        var vm = this;
        vm.types = "['geocode']";
        vm.isDisabled = true;
        vm.form = {

            components: [
            ],
            display: "form",
            page: 0

        };


        console.log(vm.form);
        vm.placeChanged = function() {


            vm.place = this.getPlace();

            vm.zipcode = '';
            vm.country = '';
            vm.state = '';
            vm.city = '';

            // FINDING ZIP
            if (vm.place.address_components[vm.place.address_components.length-1].types[0] == 'postal_code') {
                vm.zipcode = Number(vm.place.address_components[vm.place.address_components.length-1].long_name);
            };

            // FINDING COUNTRY
            if (vm.place.address_components[vm.place.address_components.length-1].types[0] == 'country' ||
                vm.place.address_components[vm.place.address_components.length-2].types[0] == 'country') {
                if(vm.place.address_components[vm.place.address_components.length-1].types[0] == 'country'){
                    vm.country = vm.place.address_components[vm.place.address_components.length-1].long_name;
                }else{
                    vm.country = vm.place.address_components[vm.place.address_components.length-2].long_name;
                }
            };

            // FINDING STATE
            if (vm.place.address_components[vm.place.address_components.length-1].types[0] == 'administrative_area_level_1' ||
                vm.place.address_components[vm.place.address_components.length-2].types[0] == 'administrative_area_level_1' ||
                vm.place.address_components[vm.place.address_components.length-3].types[0] == 'administrative_area_level_1') {

                if(vm.place.address_components[vm.place.address_components.length-1].types[0] == 'administrative_area_level_1'){
                    vm.state = vm.place.address_components[vm.place.address_components.length-1].long_name;
                }else if(vm.place.address_components[vm.place.address_components.length-2].types[0] == 'administrative_area_level_1'){
                    vm.state = vm.place.address_components[vm.place.address_components.length-2].long_name;
                }else{
                    vm.state = vm.place.address_components[vm.place.address_components.length-3].long_name;
                }
            };

            // FINDING CITY
            if (vm.place.address_components[vm.place.address_components.length-1].types[0] == 'locality' ||
                vm.place.address_components[vm.place.address_components.length-2].types[0] == 'locality' ||
                vm.place.address_components[vm.place.address_components.length-3].types[0] == 'locality' ||
                vm.place.address_components[vm.place.address_components.length-4].types[0] == 'locality' ) {

                if(vm.place.address_components[vm.place.address_components.length-1].types[0] == 'locality'){
                    vm.city = vm.place.address_components[vm.place.address_components.length-1].long_name;
                }else if(vm.place.address_components[vm.place.address_components.length-2].types[0] == 'locality'){
                    vm.city = vm.place.address_components[vm.place.address_components.length-2].long_name;
                }else if(vm.place.address_components[vm.place.address_components.length-3].types[0] == 'locality'){
                    vm.city = vm.place.address_components[vm.place.address_components.length-3].long_name;
                }else{
                    vm.city = vm.place.address_components[vm.place.address_components.length-4].long_name;
                }
            };

            console.log(vm.city);
            console.log('location', vm.place.address_components);

            if( vm.radius  == null){
                $scope.markers.siteMarker = {
                    lat: vm.place.geometry.location.lat(),
                    lng: vm.place.geometry.location.lng(),
                    focus: true,
                    message: vm.place.formatted_address,
                    draggable: true
                }

                $scope.siteCenter = {
                    lat: vm.place.geometry.location.lat(),
                    lng: vm.place.geometry.location.lng(),
                    zoom: 16
                }
            } else {

                $scope.markers.siteMarker = {
                    lat: vm.place.geometry.location.lat(),
                    lng: vm.place.geometry.location.lng(),
                    focus: true,
                    message: vm.place.formatted_address,
                    draggable: true
                }

                $scope.siteCenter = {
                    lat: vm.place.geometry.location.lat(),
                    lng: vm.place.geometry.location.lng(),
                    zoom: 16
                }

                $scope.paths.sitePath = {

                    latlngs: {
                        lat: vm.place.geometry.location.lat(),
                        lng: vm.place.geometry.location.lng()
                    },
                    radius: vm.radius,
                    type: 'circle'
                }
            };


            //vm.map.setCenter(vm.place.geometry.location);
        }



        var tiles = { tiles: {

            url: "http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
            options: {
                attribution: "",
                minZoom: 18,
                maxZoom: 22
            }}};
        angular.extend($scope, tiles,  {
            siteCenter: {},
            markers: {},
            paths: {},

            layers: {
                baselayers: {
                    googleRoadmap: {
                        name: 'Google Streets',
                        layerType: 'ROADMAP',
                        type: 'google'
                    },
                    googleTerrain: {
                        name: 'Google Terrain',
                        layerType: 'TERRAIN',
                        type: 'google'
                    },
                    googleHybrid: {
                        name: 'Google Hybrid',
                        layerType: 'HYBRID',
                        type: 'google'
                    }

                }
            },

            events: { // or just {} //all events
                markers:{
                    enable: [ 'dragend' ]
                    //logic: 'emit'
                }
            }
        });


        $scope.$on("leafletDirectiveMarker.dragend", function(event, args){
            console.log(args.model.lat);
            console.log(args.model.lng);
        });

        ClientsService.GetAll().then(function (response) {
            vm.clients = response.data;
            console.log(vm.clients);
        }, function (error) {
            $scope.status = 'Unable to load customer data: ' + error.message;
        });

        $scope.onCategoryChange = function () {
            console.log(vm.selectedClientId);
        };

        vm.onRadiusChange = function () {
            console.log(vm.radius);
            $scope.paths.sitePath = {

                latlngs: {
                    lat: vm.place.geometry.location.lat(),
                    lng: vm.place.geometry.location.lng()
                },
                radius: vm.radius,
                type: 'circle'
            }
        }

        $scope.$watch(function() {
            $('#categoryGroup').selectpicker('refresh');
        });
        vm.create = create;
      //  vm.owner = $localStorage.currentOwnerid;
        function create () {
            var site =  { name: vm.name, phone: vm.phone, contact: vm.contact, client: vm.selectedClientId,  state: vm.address.components.state, address: vm.address.name, latitude: vm.address.components.location.lat, longitude: vm.address.components.location.long, extra: vm.extra };
            console.log(site);
            vm.loading = true;
            SitesService.Create(site).then(function (response) {
                if (response) {
                    console.log( response);

                    SweetAlert.swal('Amazing', 'You\'ve done it', 'success');
                    $state.go('sites.list', {}, {reload: true});
                } else {

                    console.log( response);
                    vm.loading = false;
                    SweetAlert.swal('Error has occured', 'Please check the details you have entered');
                }
            });
        };

    }

})();