'use strict';

/* Services */

var appServices = angular.module('bananaraceApp.services', []);

appServices.service('ChampionshipService', function($http) {
    this.getChampionships = function() {
        console.log('service.getChampionships called');
        return $http.post('server/getchampionships.php', {});
    };

    this.deleteChampionship = function(championship) {
        return $http.post('server/deletechampionship.php', {
            idchampionship: championship.idchampionship
        });
    };

    this.addChampionship = function(newchampionship) {
        return $http.post('server/addchampionship.php', {
            name: newchampionship.name
        });
    };
});

appServices.service('PouleService', function($http) {
    this.getPoules = function(idchampionship) {
        return $http.post('server/getpoules.php', {
            idchampionship: idchampionship
        });
    };

    this.addPoule = function(newpoule, idchampionship) {
        return $http.post('server/addpoule.php', {
            tier: newpoule.tier,
            poule: newpoule.poule,
            idchampionship: idchampionship
        });
    };

    this.deletePoule = function(tier, poule, idchampionship) {
        return $http.post('server/deletepoule.php', {
            tier: tier,
            poule: poule,
            idchampionship: idchampionship
        });
    };

    this.addTeam = function(team, idchampionship, tier, poule) {
        return $http.post('server/addteamtopoule.php', {
            idteam: team.idteam,
            tier: tier,
            poule: poule,
            idchampionship: idchampionship
        });
    };

    this.removeTeam = function(team, idchampionship, tier, poule) {
        return $http.post('server/removeteamfrompoule.php', {
            idteam: team.idteam,
            tier: tier,
            poule: poule,
            idchampionship: idchampionship
        });
    };

    this.getRacesInPoule = function(idchampionship, tier, poule) {
        return $http.post('server/getracesinpoule.php', {
            tier: tier,
            poule: poule,
            idchampionship: idchampionship
        });
    };

    this.addRace = function(idchampionship, tier, poule, idteam) {
        return $http.post('server/addracetopoule.php', {
            tier: tier,
            poule: poule,
            idchampionship: idchampionship,
            idteam: idteam
        });
    };

    this.deleteRace = function(idchampionship, tier, poule, idrace) {
        return $http.post('server/deleterace.php', {
            tier: tier,
            poule: poule,
            idchampionship: idchampionship,
            idrace: idrace
        });
    };
});


appServices.service('TeamService', function($http) {
    this.getTeams = function(idchampionship) {
        return $http.post('server/getteams.php', {
            idchampionship: idchampionship
        });
    };

    this.getTeamsInPoule = function(idchampionship, tier, poule) {
        return $http.post('server/getteams.php', {
            idchampionship: idchampionship,
            tier: tier,
            poule: poule
        });
    };

    this.deleteTeam = function(team, idchampionship) {
        return $http.post('server/deleteteam.php', {
            idteam: team.idteam,
            idchampionship: idchampionship
        });
    };

    /**
     * Filters: teams that are from the given championship and haven't been used in another poule...
     */
    this.getTeamsAvailableForPoule = function(idchampionship, tier, poule) {
        return $http.post('server/getteams.php', {
            idchampionship: idchampionship,
            tier: tier,
            poule: poule,
            available: true
        });
    };

    this.addTeam = function(newteam, idchampionship) {
        console.log('adding team');
        return $http.post('server/addteam.php', {
            name: newteam.name,
            idchampionship: idchampionship
        });
    };
});


appServices.service('RaceService', function($http) {
    this.getDrives = function(idrace) {
        return $http.post('server/getdrives.php', {
            idrace: idrace
        });
    };

    this.getTeam = function(idrace) {
        return $http.post('server/getraceteam.php', {
            idrace: idrace
        });
    };

    this.addDrive = function(race, driver, laps) {
			console.log('raceservice.adddrive got called: '+race+' '+driver.iddriver+' '+laps);
        return $http.post('server/adddrive.php', {
            idrace: race,
            iddriver: driver.iddriver,
            laps: laps
        });
    };

    this.deleteDrive = function(drivenr, idrace) {
        return $http.post('server/deletedrive.php', {
            drivenr: drivenr,
            idrace: idrace
        });
    };

    this.addMeasurementNow = function(idrace) {
        return $http.post('server/addmeasurement.php', {
            type: 'now',
            idrace: idrace
        });
    };

    this.addMeasurementRelative = function(idrace, timestamp) {
        return $http.post('server/addmeasurement.php', {
            type: 'relative',
            idrace: idrace,
            timestamp: timestamp
        });
    };

    this.getMeasurements = function(idrace) {
        return $http.post('server/getmeasurements.php', {
            idrace: idrace
        });
    };

    this.updateMeasurement = function(measurement) {
        return $http.post('server/updatemeasurement.php', {
            idmeasurement: measurement.idmeasurement,
            valid: measurement.valid
        });
    };

    this.deleteMeasurement = function(measurement) {
        console.log('deleting measurement');
        console.log(measurement);
        return $http.post('server/deletemeasurement.php', {
            idmeasurement: measurement.idmeasurement
        });
    };
});


appServices.service('DriverService', function($http) {
    this.getDrivers = function(idchampionship, idteam) {
        return $http.post('server/getdrivers.php', {
            idchampionship: idchampionship,
            idteam: idteam
        });
    };

    this.addDriver = function(newdriver, idchampionship, idteam) {
        console.log('adding driver');
        return $http.post('server/adddriver.php', {
            idchampionship: idchampionship,
            name: newdriver.name,
            idteam: idteam
        });
    };

    this.deleteDriver = function(driver) {
        console.log('removing driver');
        return $http.post('server/deletedriver.php', {
            iddriver: driver.iddriver
        });
    };
});

appServices.service('DriveService', function($http) {
    this.getComments = function(idrace, drivenr) {
        console.log('getting comments for: ' + idrace + ' ' + drivenr);
        return $http.post('server/getcomments.php', {
            idrace: idrace,
            drivenr: drivenr
        });
    };

    this.addComment = function(idrace, drivenr, comment, penaltyseconds) {
        return $http.post('server/addcomment.php', {
            idrace: idrace,
            drivenr: drivenr,
            comment: comment,
            penaltyseconds: penaltyseconds
        });
    };

    this.deleteComment = function(idcomment) {
        return $http.post('server/deletecomment.php', {
            idcomment: idcomment
        });
    };

    this.deleteDriver = function(driver) {
        console.log('removing driver');
        return $http.post('server/deletedriver.php', {
            iddriver: driver.iddriver
        });
    };
});


appServices.service('OverviewService', function($http) {
    this.getData = function(idchampionship) {
        return $http.post('server/getoverviewofchampionship.php', {
            idchampionship: idchampionship
        });
    };
});

appServices.service('ViewerService', function($http) {

    this.getTeamsForPoule = function(idchampionship, tier, poule) {
        return $http.post('server/getteamsforpoule.php', {
            tier: tier,
            poule: poule,
            idchampionship: idchampionship
        });
    };
});

appServices.service('MainService', function($http, $cookies) {

    var activerace = null;

    this.refreshConfig = function() {
        $http.post('server/config.php', {
            action: 'getConfig'
        }).
        success(function(data, status, headers, config) {
            // console.log('refreshConfig successful...');
            //refresh activerace
            if (data.activeidrace != null) {
                // console.log('setting the activerace...');
                activerace = {
                    idrace: data.activeidrace,
                    tier: data.tier,
                    poule: data.poule,
                    idchampionship: data.idchampionship
                };
            } else {
                // console.log('idrace was null, setting activerace to null');
                activerace = null;
            }
            // console.log(activerace);
        }).
        error(function(data, status, headers, config) {
            console.log('getConfig failed...');
        });
    };

    this.setActiveRace = function(idrace, tier, poule, idchampionship) {
        if (idrace == null) {
            activerace = null;
        } else {
            activerace = {
                idrace: idrace,
                tier: tier,
                poule: poule,
                idchampionship: idchampionship
            };
        }
        $http.post('server/config.php', {
            action: 'setActiveIdRace',
            idrace: idrace
        }).
        success(function(data, status, headers, config) {
            //do something?
        });
    };

    this.getActiveRace = function() {
        return activerace;
    };

    this.toggleActiveRace = function(idrace, tier, poule, idchampionship) {
        console.log('toggling active race' + idrace);
        //if the race is not the same as the currently active on: switch it
        if (activerace == null || activerace.idrace != idrace) {
            this.setActiveRace(idrace, tier, poule, idchampionship);
        } else {
            //well, it's the same: we're disabling the activeness of the race
            activerace = null;
            this.setActiveRace(null, null, null, null);
        }
    };

    //load the current config
    this.refreshConfig();
});
