console.log('controllers.js - PeopleController initializing.');

// Provides basic functions:
// Provide people list.
// Navigate to details page.
// Select location and refresh data.

myApp.controller('PeopleController',function($scope, $location, $window, PeopleFactory) {

	// Initialize the controller by getting the initial people and measure data.
	// This is effectively a constructor.

	console.log('PeopleController Initializing - controller/people.js', $scope);

    $scope.people = PeopleFactory.getPeople();
    console.log("$scope.people = ",$scope.people);

    PeopleFactory.getMeasures(function(data){
        $scope.measures = data;
        console.log('getMeasures() - Have our Measures', $scope.measures)
    });

    $scope.selectPerson = function (id) {
    	console.log('selectPerson() id = ', id);
    	// console.log('scope.people', $scope.people);
    	var i = $scope.people.length;
    	console.log('selectPerson() id = ', id, i);
    	while (i--) {
		   console.log('id ', $scope.people[i].bioguide_id, id)
	       if ($scope.people[i].bioguide_id == id) {
        	   	console.log("found id = ", id);
        	   	$scope.person = $scope.people[i];

        	   	var path = '/detail/'+id;
        	   	$location.path(path);
        	   	return;
           }

	   }
    }

});

myApp.controller('PeopleDetailController',function($scope, $location, $window, $routeParams, PeopleFactory) {

    // Initialize the controller by getting the initial people and measure data.
    // This is effectively a constructor.

    console.log('PeopleDetailController Initializing - routeParams = ', $routeParams);

    $scope.person = PeopleFactory.getPerson($routeParams.person_id);

    console.log("$scope.person = ",$scope.person);

    PeopleFactory.getMeasures(function(data){
        $scope.measures = data;
        console.log('getMeasures() - Have our Measures', $scope.measures)
    });

});

myApp.controller('LocationController',function($scope, $location, $window, PeopleFactory) {

    // Initialize the controller by getting the initial people and measure data.
    // This is effectively a constructor.

    $scope.useCurrentLocation = function() {
        console.log("useCurrentLocaton()", $scope.localPosition);
        // Go get the data based on the long lat

        PeopleFactory.getPeopleByLatLong($scope.localPosition.coords.latitude,
                         $scope.localPosition.coords.longitude, function(data){
            $scope.people = data;
            console.log('getPeopleByLatLong() - Have our people. $scope', $scope.people);
            var path = '/list';
            $location.path(path);
        });

    }

    $scope.useZipCode = function() {
        console.log("useZipCode() Zip = ", $scope.zip);

        PeopleFactory.getPeopleByZipCode($scope.zip,function(data){
            $scope.people = data;
            console.log('getPeopleByZipCode() - Have our people.', $scope.people);
            var path = '/list';
            $location.path(path);
        });
    };

    $scope.savePosition = function(position) {
        console.log("savePosition(): lat long", position.coords.latitude, position.coords.longitude);
        $scope.localPosition = position;
        $scope.$apply();
    };

    $scope.showError = function(error) {
        switch (error.code) {
            case error.PERMISSION_DENIED:
                $scope.error = "User denied the request for Geolocation."
                break;
            case error.POSITION_UNAVAILABLE:
                $scope.error = "Location information is unavailable."
                break;
            case error.TIMEOUT:
                $scope.error = "The request to get user location timed out."
                break;
            case error.UNKNOWN_ERROR:
                $scope.error = "An unknown error occurred."
                break;
        }
        $scope.$apply();
    }

    $scope.getPosition = function() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition($scope.savePosition, $scope.showError);
        }
        else {
            $scope.error = "Geolocation is not supported by this browser.";
        }
    }
 
    $scope.getPosition();

});