<?php
class Race_User { 
    private $IDRace_user;
    private $state;
    private $commentInscription;
    private $commentUnscribe;
    private $IDWPUser;
    private $IDRace;

    //Load from ID
    public function loadFromId($id) {
        global $wpdb;

        $sql = "
            SELECT IDRace_user, state, commentInscription, commentUnscribe, IDWPUser, IDRace  FROM cass_race_user WHERE IDRace_user = %d;
        ";

        $result = $wpdb->get_row($wpdb->prepare($sql, $id));
      
        if ($result) {
            $this->setIDRace_User($result->IDRace_user);
            $this->setState($result->state);
            $this->setCommentInscription($result->commentInscription);
            $this->setCommentUnscribe($result->commentUnscribe);
            $this->setIDWPUser($result->IDWPUser);
            $this->setIDRace($result->IDRace);
            return true;
        } else {
            return false;
        }

    }

    //Update 
    public function update() {
        global $wpdb;

        $sql = "
            UPDATE cass_race_user
            SET state=%d, commentInscription=%s, commentUnscribe=%s, IDWPUser=%d, IDRace=%d 
            WHERE IDRace_user = %d;
         ";

         $wpdb->query($wpdb->prepare($sql, $this->getState(), $this->getCommentInscription(), $this->getCommentUnscribe(), $this->getIDWPUser(), $this->getIDRace(), $this->getIDRace_User()));
    }

        //Update state
        public function updateState($newState) {
            global $wpdb;
    
            $sql = "
                UPDATE cass_race_user SET state=%d WHERE IDRace_user=%d;
             ";
             $wpdb->query($wpdb->prepare($sql, $newState, $this->getIDRace_User()));
        }

    //Delete
    public function delete() {
        global $wpdb;

        $sql = "
            DELETE FROM cass_race_user 
                WHERE IDRace_user =%d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getIDRace_User()));
    }

    //Insert
    public function create() {
        global $wpdb;

        $sql = "
        INSERT INTO cass_race_user(state, commentInscription, commentUnscribe, IDWPUser, IDRace)
        VALUES(%d,%s,%s,%d,%d);
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getState(), $this->getCommentInscription(), $this->getCommentUnscribe(), $this->getIDWPUser(), $this->getIDRace()));
    }
    
    function sqlWPTableRaceUserPerRace($IDRace){
        global $wpdb;

        $table = 'cass_race_user';

        $sql = "
            SELECT IDRace_user, state, commentInscription, commentUnscribe, IDWPUser, IDWPUser as firstname, IDWPUser as lastname, IDRace from cass_race_user WHERE IDRace=%d;
             ";

        return $wpdb->get_results(
            $wpdb->prepare($sql, $IDRace),
            ARRAY_A
        );
    }

    public static function participiantAlreadyInCourse($IDRace, $IDWPUser){
        global $wpdb;
        $sql = "
        SELECT IDRace_user FROM cass_race_user WHERE IDRace=%d AND IDWPUser=%d;
         ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $IDRace, $IDWPUser));
        if ($result) {
            return $result->IDRace_user;
        } else {
            return 0;
        }
    }

    public static function selectAllCourseFromUserStructured($IDWPUser){
        global $wpdb;
        $sql = "SELECT cass_race_user.IDRace_user AS IDRace_user, cass_race_user.state AS race_user_state, cass_race.IDRace AS IDRace,cass_race.state AS state, cass_race.placeLatitude AS placeLatitude, cass_race.placeLongitude AS placeLongitude, cass_race.meetingPlaceLatitude AS meetingPlaceLatitude, cass_race.meetingPlaceLongitude AS meetingPlaceLongitude,  cass_race.name AS name,cass_race.meetingPlaceName AS meetingPlaceName, cass_race.start AS start, cass_race.end AS end, cass_group.name AS groupName, cass_type.name AS typeName,cass_level.name AS level
        FROM cass_race
        INNER JOIN cass_group ON cass_race.IDGroup = cass_group.IDGroup 
        INNER JOIN cass_type ON cass_race.IDType = cass_type.IDType 
        INNER JOIN cass_race_user ON cass_race_user.IDRace = cass_race.IDRace
        INNER JOIN cass_level ON cass_race.IDLevel = cass_level.IDLevel
        WHERE cass_race_user.IDWPUser = %d
        ORDER BY cass_race.start DESC;
        ";  
    
        $sql = $wpdb->prepare($sql, $IDWPUser);
    
        return $wpdb->get_results($sql);
    }

    public static function countNumberMemberOfCourseSubsribed($IDRace){
        global $wpdb;

        $sql = "
            SELECT count(IDRace_user) AS count FROM cass_race_user
            WHERE IDRace=%d AND (state=1 OR state=2);
        ";

        $result = $wpdb->get_row($wpdb->prepare($sql, $IDRace));

        return intval($result->count);
    }

    public static function countMemberOfCourseParticipation($IDRace, $IDWPUser){
        global $wpdb;

        $race = new Race();
        $race->loadFromId($IDRace);

        
        
        $sql = "
            SELECT Count(IDRace_user) AS count FROM cass_race_user 
            INNER JOIN cass_race ON  cass_race.IDRace = cass_race_user.IDRace
            WHERE IDWPUser = %d AND (cass_race_user.state = 1 OR cass_race_user.state = 2) AND cass_race.IDSeason = %d;
        ";

    
        $result = $wpdb->get_row($wpdb->prepare($sql, $IDWPUser, $race->getIDSeason()));

        return intval($result->count);
    }

    public static function countMemberOfCourseInscription($IDRace, $IDWPUser){
        global $wpdb;

        $race = new Race();
        $race->loadFromId($IDRace);
        
        $sql = "
            SELECT Count(IDRace_user) AS count FROM cass_race_user 
            INNER JOIN cass_race ON  cass_race.IDRace = cass_race_user.IDRace
            WHERE IDWPUser = %d AND (cass_race_user.state = 2) AND cass_race.IDSeason = %d;
        ";

        $result = $wpdb->get_row($wpdb->prepare($sql, $IDWPUser, $race->getIDSeason()));

        return intval($result->count);
    }

    public static function setDeputy($IDRace_User) {
        global $wpdb;
        $sql = "
            SELECT IDRace_user, IDWPUser, IDRace FROM cass_race_user WHERE IDRace_user=%d;
         ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $IDRace_User)); 

        //Set the deputy of the race
        $sql = "
            UPDATE cass_race SET IDWPUser_Deputy=%d WHERE IDRace=%d;
        ";
        $wpdb->query($wpdb->prepare($sql, $result->IDWPUser, $result->IDRace));
    }

    public static function selectOne($IDWPUser, $IDRace){
        global $wpdb;
        $sql = "SELECT * 
        FROM cass_race_user 
        WHERE IDWPUser = %d 
          AND IDRace = %d;
        ";

        $sql = $wpdb->prepare($sql, $IDWPUser, $IDRace);

        return $wpdb->get_results($sql);
    }

    // Getters
    public function getIDRace_User() {
        return $this->IDRace_user;
    }

    public function getState() {
        return $this->state;
    }

    public function getCommentInscription() {
        return $this->commentInscription;
    }

    public function getCommentUnscribe() {
        return $this->commentUnscribe;
    }

    public function getIDWPUser() {
        return $this->IDWPUser;
    }

    public function getIDRace() {
        return $this->IDRace;
    }

    // Setters
    public function setIDRace_User($IDRace_user) {
        $this->IDRace_user = $IDRace_user;
    }

    public function setState($state) {
        $this->state = $state;
    }

    public function setCommentInscription($commentInscription) {
        $this->commentInscription = $commentInscription;
    }

    public function setCommentUnscribe($commentUnscribe) {
        $this->commentUnscribe = $commentUnscribe;
    }

    public function setIDWPUser($IDWPUser) {
        $this->IDWPUser = $IDWPUser;
    }

    public function setIDRace($IDRace) {
        $this->IDRace = $IDRace;
    }
        
}