<commit_before>angular.module('next.controllers', [])
.controller('OverviewCtrl', function($scope, $rootScope, $location, $ionicPlatform, $ionicLoading, $ionicScrollDelegate, $cordovaGeolocation, $ionicViewSwitcher, $timeout, ApiService, StationService) {
    $scope.stations = [];
    $ionicLoading.show({
        template: 'Laster inn stasjoner...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner><button class="button button-outline button-cancel-search" ng-click="$root.cancel()">Avbryt søk</button></div>'
    });

    $rootScope.cancel = $ionicLoading.hide();
    $scope.hasStations = true;

    var lat;
    var lng;

    var NUM_STATIONS = 15;

    $ionicPlatform.ready(function() {
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;

                // Oslo: 59.932624, 10.734738
                // Ytterby: 57.863906199999995 11.9110967

                console.log("Coordinates from cordova geolocation", lat, lng);

                if ((lng + "").substr(0, 4) === "11.9") {
                    lat = 59.932624;
                    lng = 10.734738;

                    console.log("But since you are in Ytterby we'll use", lat, lng, "instead! :)");
                }

                var preferredStation = ApiService.getPreferredStation(lat, lng);
                if (preferredStation) {
                    $ionicViewSwitcher.nextTransition('none');
                    StationService.setStation(preferredStation);
                    $location.path('/station-detail');
                }
                getStationsFromApi(lat, lng);
                $ionicLoading.hide();

            }, function(err) {
                console.log("Error getting current position! :(", err);
            });
    });

    $ionicPlatform.on('resume', function() {
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                getStationsFromApi(lat, lng);
            })
    })

    $scope.goToSearch = function() {
        $location.path('/search');
    }

    $scope.goToStation = function(station) {
        if (station && lat && lng) {
            ApiService.preferStation(station, lat, lng);
            setTimeout(function() {
                getStationsFromApi(lat, lng);
            }, 1000)
        }

        StationService.setStation(station);
        $location.path('/station-detail');
    };


    function getStationsFromApi(lat, lng) {
        ApiService.getStationList(lat, lng, NUM_STATIONS, function(err, stations) {
            $timeout(function() {
                $scope.favorites = stations.favorites;
                $scope.regular = stations.regular;
                $scope.$apply();

                $scope.$broadcast('scroll.refreshComplete');
                $ionicScrollDelegate.scrollTop(true);

                $ionicLoading.hide();

                if (stations.regular.length === 0) $scope.hidden = false;
                else $scope.hidden = true;
            })

            $timeout(function() {
                $scope.hasStations = stations.hasStations;
            }, 1000)
        });
    }

    $scope.refresh = function() {
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                getStationsFromApi(lat, lng);
            });
    };

    $scope.reset = function(station) {
        $ionicLoading.show({
            template: 'Lagrer favoritt... <div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner>'
        });
        ApiService.unpreferStation(station, lat, lng);
        getStationsFromApi(lat, lng);
    };

})

.controller('DetailCtrl', function($scope, $rootScope, $ionicSlideBoxDelegate, $ionicPlatform, $ionicLoading, $ionicScrollDelegate, $cordovaGeolocation, $timeout, ApiService, StationService, $filter) {

    $ionicLoading.show({
        template: 'Laster inn avganger...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner><br /><button class="button button-outline button-cancel-search" ng-click="$root.cancel()">Avbryt søk</button></div>'
    });

    $rootScope.cancel = $ionicLoading.hide;

    $scope.hasDepartures = false;
    $scope.isLoaded = false;

    if (StationService.getStation() !== null) {
        $scope.selectedStation = StationService.getStation();
        getLinesFromApi({
            id: $scope.selectedStation.ID
        });
    }

    function getLinesFromApi(options) {
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                ApiService.getDeparturesForStation(options, $scope.selectedStation, lat, lng, (function(err, lines) {
                    $timeout(function() {
                        console.log(lines);
                        $scope.favorites = lines.favorites;
                        $scope.regular = lines.regular;
                        $scope.isLoaded = true;
                        $scope.hasDepartures = lines.hasDepartures;
                        $scope.$apply();
                        $scope.$broadcast('scroll.refreshComplete');
                        $ionicScrollDelegate.scrollTop(true);
                        $ionicLoading.hide();
                    }, 500);
                }));;
            });
    }

    $scope.refresh = function(options) {
        var options = options || {};
        if (options.showLoadingOverlay) {
            $ionicLoading.show({
                template: 'Oppdaterer avganger...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner><br /><button class="button button-outline button-cancel-search" ng-click="$root.cancel()">Avbryt søk</button></div>'
            });
        }
        getLinesFromApi({
            id: $scope.selectedStation.ID,
            force: true
        });
    };

    $scope.preferDeparture = function(departure) {
        $ionicLoading.show({
            template: 'Lagrer favoritt... <div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner>'
        });
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                ApiService.preferDeparture(departure, $scope.selectedStation.Name, lat, lng);
                ApiService.getDeparturesForStation({id: $scope.selectedStation.ID}, $scope.selectedStation, lat, lng, (function(err, lines) {
                    $timeout(function() {
                        $scope.favorites = lines.favorites;
                        $scope.regular = lines.regular;
                        $scope.isLoaded = true;
                        $scope.hasDepartures = lines.hasDepartures;
                        $scope.$broadcast('scroll.refreshComplete');
                        $ionicScrollDelegate.scrollTop(true);
                        $ionicLoading.hide();
                    }, 500);
                }));;
            })
    };

    $ionicPlatform.on('resume', function() {
        $scope.refresh({
            showLoadingOverlay: true
        });
    });

    $scope.reset = function(departure) {
        $ionicLoading.show({
            template: 'Fjerner favoritt... <div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner>'
        });
        ApiService.unpreferDeparture(departure, lat, lng);
        getLinesFromApi({
            id: $scope.selectedStation.ID,
        });
    };
})

.controller('SearchCtrl', function($scope, $ionicLoading, $rootScope, ApiService, $cordovaGeolocation, $timeout, StationService, $location) {
    $scope.stations = [];<commit_msg>using new service<commit_after>angular.module('next.controllers', [])
.controller('OverviewCtrl', function($scope, $rootScope, $location, $ionicPlatform, $ionicLoading, $ionicScrollDelegate, $cordovaGeolocation, $ionicViewSwitcher, $timeout, ApiService, StationService, HttpService) {
    $scope.stations = [];

    $rootScope.cancel = $ionicLoading.hide();
    $scope.hasStations = true;

    $ionicLoading.show({
        template: 'Laster inn stasjoner...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner></div>'
    });

    var lat;
    var lng;

    var NUM_STATIONS = 15;

    $ionicPlatform.ready(function() {
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;

                // Oslo: 59.932624, 10.734738
                // Ytterby: 57.863906199999995 11.9110967

                console.log("Coordinates from cordova geolocation", lat, lng);

                if ((lng + "").substr(0, 4) === "11.9") {
                    lat = 59.932624;
                    lng = 10.734738;

                    console.log("But since you are in Ytterby we'll use", lat, lng, "instead! :)");
                }

                var preferredStation = HttpService.getPreferredStation(lat, lng);

                if (preferredStation) {
                    $ionicViewSwitcher.nextTransition('none');
                    StationService.setStation(preferredStation);
                    $location.path('/station-detail');
                }
                getStationsFromApi(lat, lng);
                $ionicLoading.hide();

            }, function(err) {
                console.log("Error getting current position! :(", err);
            });
    });

    $ionicPlatform.on('resume', function() {
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                getStationsFromApi(lat, lng);
            })
    })

    $scope.goToSearch = function() {
        $location.path('/search');
    }

    $scope.goToStation = function(station) {
        if (station && lat && lng) {
            HttpService.preferStation(station, lat, lng);
            setTimeout(function() {
                getStationsFromApi(lat, lng);
            }, 1000)
        }

        StationService.setStation(station);
        $location.path('/station-detail');
    };


    function getStationsFromApi(lat, lng) {
        HttpService.getStations(lat, lng, 15).then(function(stations) {
            $scope.favorites = stations.favorites;
            $scope.regular = stations.regular;

            $scope.$broadcast('scroll.refreshComplete');
            $ionicScrollDelegate.scrollTop(true);

            $ionicLoading.hide();

            if (stations.regular.length === 0) $scope.hidden = false;
            else $scope.hidden = true;

            $scope.hasStations = stations.hasStations;
        });
    }

    $scope.refresh = function() {
        $ionicLoading.show({
            template: 'Laster inn stasjoner...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner></div>'
        });
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                getStationsFromApi(lat, lng);
            });
    };

    $scope.reset = function(station) {
        $ionicLoading.show({
            template: 'Fjerner favoritt... <div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner>'
        });
        HttpService.unpreferStation(station, lat, lng);
        getStationsFromApi(lat, lng);
    };

})

.controller('DetailCtrl', function($scope, $rootScope, $ionicSlideBoxDelegate, $ionicPlatform, $ionicLoading, $ionicScrollDelegate, $cordovaGeolocation, $timeout, HttpService, StationService, $filter) {
/*    $rootScope.cancel = $ionicLoading.hide();
*/
    $scope.hasDepartures = false;
    $scope.isLoaded = false;

    if (StationService.getStation() !== null) {
        $scope.selectedStation = StationService.getStation();
        getLinesFromApi({
            id: $scope.selectedStation.ID
        });
    }

    function getLinesFromApi(options) {
        $ionicLoading.show({
            template: 'Laster inn avganger...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner><br /></div>'
        });
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                HttpService.getDeparturesForStation(options, $scope.selectedStation, lat, lng).then(function(lines) {
                    console.log(lines);
                    $scope.favorites = lines.favorites;
                    $scope.regular = lines.regular;
                    $scope.isLoaded = true;
                    $scope.hasDepartures = lines.hasDepartures;
                    $scope.$broadcast('scroll.refreshComplete');
                    $ionicScrollDelegate.scrollTop(true);
                    $ionicLoading.hide();
                });;
            });
    }

    $scope.refresh = function(options) {
        $ionicLoading.show({
            template: 'Oppdaterer avganger...<div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner><br /><button class="button button-outline button-cancel-search" ng-click="$root.cancel()">Avbryt søk</button></div>'
        });
        getLinesFromApi({
            id: $scope.selectedStation.ID,
            force: true
        });
    };

    $scope.preferDeparture = function(departure) {
        $ionicLoading.show({
            template: 'Lagrer favoritt... <div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner>'
        });
        $cordovaGeolocation.getCurrentPosition()
            .then(function(position) {
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                HttpService.preferDeparture(departure, $scope.selectedStation.Name, lat, lng);
                getLinesFromApi({
                    id: $scope.selectedStation.ID,
                });
            })
    };

    $ionicPlatform.on('resume', function() {
        $scope.refresh({
            showLoadingOverlay: true
        });
    });

    $scope.reset = function(departure) {
        $ionicLoading.show({
            template: 'Fjerner favoritt... <div class="loading-icon"><ion-spinner icon="spiral" class="spinner-positive"></ion-spinner>'
        });
        ApiService.unpreferDeparture(departure, lat, lng);
        getLinesFromApi({
            id: $scope.selectedStation.ID,
        });
    };
})

.controller('SearchCtrl', function($scope, $ionicLoading, $rootScope, ApiService, $cordovaGeolocation, $timeout, StationService, $location) {
    $scope.stations = [];
{'fraction_non_alphanumeric': 0.13537432804510294, 'fraction_numerical': 0.009964599449324768, 'mean_word_length': 2.0546655987184623, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6236049', 'n_tokens_mistral': 4496, 'n_tokens_neox': 4157, 'n_words': 652}