﻿app.controller('HomeController', ['$scope', "$mdToast", function ($scope, $mdToast) {

    $scope.$parent.title = "Home";

    $scope.predicate = 'score';
    $scope.reverse = true;

    $scope.search = function (song) {
        if (!$scope.searchText || (song.name.toLowerCase().indexOf($scope.searchText.toLowerCase()) !== -1) 
            || (song.artist.toLowerCase().indexOf($scope.searchText.toLowerCase()) !== -1) 
            || (song.year.toString().indexOf($scope.searchText) !== -1)) {
            return true;
        }
        return false;
    };

    $scope.songs = [{ name: "Satellit", artist: "Ted Gärdestad", score: 420, year: 1978 },
                    { name: "Waterloo", artist: "ABBA", score: 9001, year: 1974 },
                    { name: "Diggiloo Diggiley", artist: "Herreys", score: 69, year: 1984 }];

    $scope.sortBy = function (field) {

        if ($scope.predicate === field) {
            $scope.reverse = !$scope.reverse;
        }
        else {
            $scope.predicate = field;
            $scope.reverse = false;
        }
    }

    $scope.getSortClass = function (field) {
        if ($scope.predicate === field) {
            switch ($scope.reverse){
                case true:
                    return "mdl-data-table__header--sorted-descending"
                case false:
                    return "mdl-data-table__header--sorted-ascending"
            }

            return;
        }
    };

    $scope.showToast = function (action) {
        switch (action) {
            case "primary":
                {
                    var toast = $mdToast.simple()
                                            .textContent('Primary button clicked')
                                            .position('top right');
                    $mdToast.show(toast);
                    break;
                }
            case "accent":
                {
                    var toast = $mdToast.simple()
                                            .textContent('Accent button clicked')
                                            .position('top left');
                    $mdToast.show(toast);
                    break;
                }
            default:
                return;

        }
    };

}]);