<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Site\Problems;

defined('_JEXEC') or die;

class Medal extends ProblemBase {

    protected static $fieldnames = [];
    protected static $fieldtypes = [];
    protected static $count = -1;
    protected static $tablename = '#__alpha_userpoints_medals';
    protected static $updatetable = '#__alpha_userpoints_medals';
    protected static $keyname = 'id';

    public function __construct(int $id = 0) {
        parent::__construct();
        if ($id) {
            $this->getOneRow($id);
        }
    }

    public static function withRid(int $rid) {
        $medalobject = new self();
        $db = SQL::getInstance();
        $db->setQuery(sprintf("SELECT * FROM `%s` WHERE rid = %d", self::$tablename, $rid));
        $data = $db->loadObject();
        if (is_object($data)) {
            $medalobject->problem_object = $data;
        }
        return $medalobject;
    }

    public static function selectionWithMedal(int $medal, int $limitstart, int $limit) {
        $db = SQL::getInstance();
        $db->setQuery(sprintf("SELECT "
                . " m.medaldate AS dateawarded, m.reason , u.name, u.username, lv.id AS cid, lv.typerank, lv.icon, lv.rank "
                . " FROM `%s` AS m"
                . " LEFT JOIN #__alpha_userpoints_levelrank AS lv ON m.medal=lv.id"
                . " LEFT JOIN #__alpha_userpoints AS aup ON m.rid=aup.id"
                . " LEFT JOIN #__users AS u ON aup.userid=u.id"
                . " WHERE m.medal = %d"
                . " ORDER BY m.medaldate DESC LIMIT %d, %d", self::$tablename, $medal, $limitstart, $limit));
        return self::makeProblems($db->loadObjectList());
    }
    
    public static function userMedals (string $referrerid, int $userid) {
        $db = SQL::getInstance();
        $q = sprintf("SELECT m.id, m.medaldate, m.reason, lv.rank, lv.description, lv.icon, lv.image "
            . " FROM `%s` AS m, #__alpha_userpoints_levelrank AS lv, #__alpha_userpoints AS aup "
            . " WHERE m.medal = lv.id AND aup.id = m.rid AND (aup.referreid = %s OR aup.userid = %d) "
            . " ORDER BY m.medaldate DESC", self::$tablename, $db->quote($referrerid), $userid);
        $db->setQuery($q);
        return self::makeProblems($db->loadObjectList());
    }

    public static function deleteWithRid(int $rid, int $medal = 0) {
        $db = SQL::getInstance();
        $sql = sprintf("DELETE FROM `%s` WHERE `rid` = %d", self::$updatetable, $rid);
        if ($medal) {
            $sql .= sprintf(" AND medal = %d", $medal);
        }
        $db->setQuery($sql);
        $db->execute();
    }

    public static function byRid(int $rid): array {
        $db = SQL::getInstance();
        $query = sprintf("SELECT COUNT(*), medal FROM `%s` WHERE `rid` = %d GROUP BY `medal`", self::$tablename, $rid);
        $db->setQuery($query);
        $medals = (array) $db->loadObjectList();
        foreach ($medals as $onemedal) {
            $result[$onemedal->medal] = $onemedal;
        }
        return isset($result) ? $result : [];
    }
}
