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

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

    /** @ngInject */
    function sitesEditCtrl($scope) {
        var vm = this;
        var id = $stateParams.id;
        vm.types = "['geocode']";

        vm.showResults = function(){
            return (vm.results ? vm.results=false : vm.results=true)
        }

        vm.placeChanged = function() {


            vm.place = this.getPlace();
            console.log('location', vm.place.geometry.location.lat());
            $scope.markers.osloMarker = {
                lat: vm.place.geometry.location.lat(),
                lng: vm.place.geometry.location.lng(),
                focus: true,
                message: vm.place.geometry,
                draggable: true
            }

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

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

        angular.extend($scope, {
            osloCenter: {},
            markers: {},
            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 user data: ' + error.message;
        });

        $scope.$watch(function() {
            $('#clients').selectpicker('refresh');
        });
        GetById(id);

        function GetById(id) {
            SitesService.GetById(id)
                .then(function (response) {
                    vm.details = response.data[0];
                    vm.address1 = response.data[0].address;
                    //vm.address1 = response.data.client.Address;
                    console.log(  vm.details );
                    var geocoder = new google.maps.Geocoder();
                    var address = vm.address1;
                    console.log(address);
                    geocoder.geocode({ 'address': address }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {

                            vm.address = results[0];
                            console.log(vm.address );

                            $scope.markers.osloMarker = {
                                lat: JSON.parse(vm.address.geometry.location.lat()),
                                lng: JSON.parse(vm.address.geometry.location.lng()),
                                focus: true,
                                message: "test",
                                draggable: true
                            }

                            $scope.osloCenter = {
                                lat: JSON.parse(vm.address.geometry.location.lat()),
                                lng: JSON.parse(vm.address.geometry.location.lng()),
                                zoom: 16
                            }
                        } else {
                            alert("Request failed.")
                        }
                    });
                }, function (error) {
                    $scope.status = 'Unable to load customer data: ' + error.message;
                });
        }
        $scope.onCategoryChange = function () {
            console.log(vm.details.client);

            //alert("Selected Value: " + vm.itemSelected.id + "\nSelected Text: " + vm.itemSelected.name  + " Text: " + vm.itemSelected.power);

        };


        vm.Update = Update;
        vm.owner = $localStorage.currentOwnerid;
        function Update () {
            var id = $stateParams.id;
            var site =  { name: vm.details.name, phone: vm.details.phone, contact: vm.details.contact, client: vm.details.client, owner:  vm.owner, address: vm.details.address, state: vm.details.state, latitude: vm.address.components.location.lat, longitude: vm.address.components.location.long, extra: vm.details.extra };
            console.log(site);
            vm.loading = true;
            SitesService.Update(site, id).then(function (response) {
                if (response) {
                    console.log( response);

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

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

    }

})();
