var DetailCtrl = function(NotesSvc, $scope, $state, $ionicHistory, $ionicPopup) {
    this.note = {};
    var self = this;

    this.getNote = function() {
        if ($state.params.id) {
            this.note = NotesSvc.getNote($state.params.id);
            this.evaluation.rating = this.note.evaluation;
        }
    };

    this.retour = function() {
        $ionicHistory.goBack();
    };

    this.save = function(form) {
        if(form.$valid) {
            $state.go('home');
        }
    };

    this.evaluation = {
        iconOn : 'ion-ios-star',
        iconOff : 'ion-ios-star-outline',
        iconOnColor: 'rgb(200, 200, 100)',
        iconOffColor:  'rgb(200, 100, 100)',
        rating:  5,
        minRating:1,
        callback: function(rating) {
          self.ratingsCallback(rating);
        }
      };

    this.ratingsCallback = function(rating) {
        this.note.evaluation = rating;
        console.log('Nouvelle note : ', rating);
    };

    this.partagerNote = function() {
        var options = {
            message: this.note.description,
            subject: this.note.titre,
            files: ['http://ionicframework.com/img/ionic-logo-blog.png'],
            url: 'http://ionicframework.com/'
        };

        if (window.plugins && window.plugins.socialsharing) {
            window.plugins.socialsharing.shareWithOptions(options, function(result) {console.log(result);}, function(error) {console.log(error);});
        }
    };

    this.marquerPosition = function() {
        var onSuccess = function(position) {
            self.note.position = {
                latitude :position.coords.latitude,
                longitude: position.coords.longitude,
                altitude: position.coords.altitude,
                precision: position.coords.accuracy,
                vitesse: position.coords.speed,
                date: position.timestamp };
            $scope.$apply();
            console.log(self.note.position);
        };

        var onError = function (error) {
            alert('code: '    + error.code    + '\n' +
                'message: ' + error.message + '\n');
        };

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(onSuccess, onError);
        }
    };

    $scope.$on("$ionicView.afterEnter", function(event, data){
        self.getNote();
    });
};