<?php
class Database {
	public $_handler;
 	//private $_logger;
    //'localhost', 'root', '2n83thnb!b0y1833s', "winaskin"
 	public function __construct($host = "localhost", $username = "root", $password = "2n83thnb!b0y1833s", $database = "winaskin")
    {
		//$this->_logger = new Logger("database", $this);
 		Try {
			$this->_handler = new PDO('mysql:host=' . $host . ';dbname=' . $database, $username, $password);
 			$this->_handler->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 			//$this->_logger->writeLog(Logger::DEBUG, 'Connecting to the database...');
 		}
 		Catch (Exception $e) {
			throw new Exception('Database connection error :' . $e->getMessage());
 			//$this->_logger->writeLog(Logger::CRITICAL, 'Database connection error :' . $e->getMessage());
 		}
 	}
	
 	private function query($sql, ...$args)
    {
 		//$this->_logger->writeLog(Logger::DEBUG, 'Processing request : ' . $sql);
		$request = $this->_handler->prepare($sql);
 		if($request ==! false) {
 			if($request->execute($args))
			{
 				$resultArray = $request->fetchAll(PDO::FETCH_ASSOC);
 				$request->closeCursor();
 				return $resultArray;
			}
			else {
                echo 'Request failed :' . $sql;
				//$this->_logger->writeLog(Logger::ERROR, 'Request failed :' . $sql);
 				return null;
			}
 		} else {
                echo 'Wrong request :' . $sql;
			//$this->_logger->writeLog(Logger::ERROR, 'Wrong request :' . $sql);
 			return null;
 		}
 	}
 	
 	private function execute($sql, ...$args)
 	{
 		//$this->_logger->writeLog(Logger::DEBUG, 'Processing request : ' . $sql);
 		$request = $this->_handler->prepare($sql);
 		if($request ==! false) {
 			if($request->execute($args))
			{
 				return true;
			}
			else {
                echo 'Request failed :' . $sql;
				//$this->_logger->log(Logger::ERROR, 'Request failed :' . $sql);
 				return false;
			}
 		} else {
            echo 'Wrong request :' . $sql;
			//$this->_logger->log(Logger::ERROR, 'Wrong request :' . $sql);
 			return false;
 		}
 	}
    
    public function getLastInsertId()
    {
        return $this->_handler->lastInsertId();
    }
    
    public function getCheaters()
    {
        return $this->query('SELECT * FROM users2 WHERE daycoins > 0 AND lastco LIKE "June 22 2016%" AND tradeurl = "" AND adcoins = 0');
    }
    
    public function deleteCheaters()
    {
        return $this->execute('UPDATE users2 SET status = "BAN" WHERE daycoins > 0 AND lastco LIKE "June 22 2016%" AND tradeurl = "" AND adcoins = 0');
    }
    
    public function deleteCheatersParticipationsForRaffle($raffleId)
    {
        return $this->execute('DELETE FROM raffle' . intval($raffleId) . ' WHERE userid IN (SELECT steamid FROM users2 WHERE daycoins > 0 AND lastco LIKE "June 22 2016%" AND tradeurl = "" AND adcoins = 0)');
    }
    
    public function getParticipationsForRaffle($raffleId)
    {
        return $this->query('SELECT * FROM raffle' . intval($raffleId) . ' WHERE userid != 0');
    }
    
    public function reorderParticipation($raffleId, $participation, $ticketStart, $ticketEnd)
    {
        return $this->execute('UPDATE raffle' . intval($raffleId) . ' SET ticketstart = ?, ticketend = ? WHERE id = ?', $ticketStart, $ticketEnd, $participation);
    }
    
}
?>