<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Site;

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Router\Route;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\WebAssetManager;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

class View extends HtmlView {

    protected $viewname = '';
    protected $controller = null;
    static protected $viewpath;

    public function __construct($controller) {
        $this->controller = $controller;
        $classname = get_class($this);
        $classparts = explode('\\', $classname);
        $this->viewname = \substr(\array_pop($classparts), 0, -4);
        if (false !== strpos($classname, 'View') || version_compare(JVERSION, '4.0', 'ge')) {
            $config = array('template_path' => JPATH_SITE . static::$viewpath . $this->viewname . '/tmpl/');
            parent::__construct($config);
        }
    }

    public function __get($name) {
        return $this->controller->$name ?? '';
    }

    public function display($tpl = null) {
        if (Factory::getConfig()->get('debug')) {
            $displayclassparts = explode('\\', get_class($this));
            $viewname = substr(end($displayclassparts), 0, -4);
            $templatename = empty($tpl) ? strtolower($this->viewname) : $tpl;
            echo "<div>DEBUG HELP: View=$viewname Template=default_$templatename</div>";
        }
        parent::display(empty($tpl) ? strtolower($this->viewname) : $tpl);
    }

    public function showtext(string $text, bool $onlyif = true): string {
        return self::_($text, $onlyif);
    }

    public static function _(string $text, bool $onlyif = true): string {
        return $onlyif ? $text : '';
    }

    public static function Text_($name): string {
        return Text::_($name);
    }

    public static function _x(array $textarray): string {
        return implode("\n", $textarray);
    }

    public function showarray(array $textarray): string {
        return self::_x($textarray);
    }

    public function getFormattedPointsAdm($points) {
        return UserPoints::getFormattedPoints($points);
    }

    protected function isIEAdm() {
        $userAgent = Factory::getApplication()->input->server->getString('HTTP_USER_AGENT');
        if (preg_match('/MSIE/i', $userAgent)) {
            WebAssetManager::getInstance('com_userpoints')->useScript('html5.scripts');
        }
    }

    protected function getCpanelToolbar() {
        ToolbarHelper::custom('cpanel', 'dashboard.png', 'dashboard.png', Text::_('AUP_CPANEL'), false);
        ToolbarHelper::divider();
    }

    protected function getPrefHelpToolbar() {
        $tag = Factory::getLanguage()->getTag();

        ToolbarHelper::divider();
        ToolbarHelper::custom('rules', 'move.png', 'move.png', Text::_('AUP_RULES'), false);
        // Options button.
        if (UserPoints::getUser()->authorise('core.admin', 'com_userpoints')) {
            ToolbarHelper::preferences('com_userpoints');
        }
        ToolbarHelper::divider();
        ToolbarHelper::help('screen.userpoints', true);
    }

    protected function displayDateTime(string $datetime): string {
        return '1000-01-01 00:00:00' == $datetime ? Text::_('AUP_DATE_NEVER') : $datetime;
    }

    protected function presentDate($type, $date, $format): string {
        if ('date' == $type) {
            return date($format, strtotime($date));
        }
    }

    protected function getProfileLink($profilechoice, $userinfo, $xhtml = true) {

        switch ($profilechoice) {
            case 'ku' :
                $profilLink = Route::_('index.php?option=com_kunena&func=fbprofile&userid=' . $userinfo->userid, $xhtml);
                $menu = 'com_kunena';
                break;
            case 'cb' :
                $profilLink = Route::_('index.php?option=com_comprofiler&task=userProfile&user=' . $userinfo->userid, $xhtml);
                $menu = 'index.php?option=com_comprofiler&view=userprofile';
                break;
            case 'cbe' :
                $profilLink = Route::_('index.php?option=com_cbe&task=userProfile&user=' . $userinfo->userid, $xhtml);
                $menu = 'index.php?option=com_cbe&task=userProfile';
                break;
            case 'js' :
                $profilLink = Route::_('index.php?option=com_community&view=profile&userid=' . $userinfo->userid, $xhtml);
                $menu = 'index.php?option=com_community&view=profile';
                break;
            case 'es' :
                $profilLink = Route::_('index.php?option=com_easysocial&view=profile&id=' . $userinfo->userid, $xhtml);
                $menu = 'index.php?option=com_easysocial&view=profile';
                break;
            case 'eb' :
                $profilLink = Route::_('index.php?option=com_easyblog&view=blogger&layout=listings&id=' . $userinfo->userid, $xhtml);
                $menu = 'index.php?option=com_easyblog&view=blogger';
                break;
            case 'ep' :
                $profilLink = Route::_('index.php?option=com_jsn&view=profile&id=' . $userinfo->userid, $xhtml);
                $menu = 'index.php?option=com_jsn&view=profile';
                break;
            case 'j!' :
                $profilLink = Route::_('index.php?option=com_users&view=profile', $xhtml);
                $menu = 'index.php?option=com_users&view=profile';
                break;

            default :
                // UP Link Profile
                $profilLink = Route::_('index.php?option=com_userpoints&view=account&userid=' . $userinfo->referreid, $xhtml);
                $menu = 'index.php?option=com_userpoints&view=account';
                break;
        }
        if ($menu != '') {
            $db = SQL::getInstance();
            $lang = Factory::getLanguage();
            $q = "SELECT id FROM #__menu WHERE `link`= {$db->quote($menu)}
        AND `type`='component' AND `published`='1'  AND access='1' AND ( language = {$db->quote($lang->getTag())} OR language='*')";
            $db->setQuery($q);
            $profilLink .= '&Itemid=' . $db->loadResult();
        }

        return $profilLink;
    }

    public function getFormattedPoints($points) {
        return UserPoints::getFormattedPoints($points);
    }
}
