<?php

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

defined('_JEXEC') or die;

class UserpointsRouter implements Joomla\CMS\Component\Router\RouterInterface {

    private static $sefview = array(
        'invite' => 'invite-layout',
        'latestactivity' => 'latest-activities',
        'medals' => 'medals',
        'rules' => 'rules',
        'account' => 'user-points-profile',
        'users' => 'users-list'
    );

    public function build(&$query) {
        $result = [];
        $mainquery = array_diff_key($query, ['option' => true, 'Itemid' => true]);

        // Handle pagination parameter
        if (isset($query['limitstart'])) {
            $result[] = 'page-' . $query['limitstart'];
            unset($query['limitstart']);
        }

        foreach ($mainquery as $key => $value) {
            if ('account' == $value) {
                $result[] = (2 == count($mainquery)) ? 'profile' : 'account';
            } else {
                $result[] = $value;
            }
            unset($query[$key]);
        }
        return $result;
    }

    public function parse(&$segments) {
        $vars = [];

        // Check for pagination segment first
        foreach ($segments as $i => $segment) {
            if (strpos($segment, 'page-') === 0) {
                $vars['limitstart'] = (int) substr($segment, 5);
                unset($segments[$i]);
                $segments = array_values($segments);
                break;
            }
        }

        if (count($segments) && method_exists($this, $segments[0])) {
            $method = $segments[0];
            $result = $this->$method($segments);
            $vars = array_merge($vars, $result);
        }

        $segments = [];
        return $vars;
    }

    private function account($segments) {
        // $_GET['view'] = $_REQUEST['view'] = 'account';
        if (3 == count($segments)) {
            // $_GET['task'] = $_REQUEST['task'] = 'downloadactivity';
            $userid = $this->stripItemidEtc($segments[2]);
            // $_GET['userid'] = $_REQUEST['userid'] = $userid;
            return ['view' => 'account', 'task' => 'downloadactivity', 'userid' => $userid];
        } elseif (2 == count($segments)) {
            $userid = $this->stripItemidEtc($segments[1]);
            // $_GET['userid'] = $_REQUEST['userid'] = $userid;
            return ['view' => 'account', 'userid' => $userid];
        }
        return [];
    }

    private function profile($segments) {
        // $_GET['view'] = $_REQUEST['view'] = 'account';
        if (2 == count($segments)) {
            $userid = $this->stripItemidEtc($segments[1]);
            // $_GET['userid'] = $_REQUEST['userid'] = $userid;
            return ['view' => 'account', 'userid' => $userid];
        }
        return [];
    }

    private function plugin($segments) {
        if (4 == count($segments)) {
            return [
                'view' => 'plugin',
                'trigger' => $segments[1],
                'redirect' => $segments[2],
                'pointitem' => $segments[3]
            ];
        }
        return [];
    }

    private function stripItemidEtc($value) {
        $parts = explode('&', $value);
        return $parts[0];
    }

    public function preprocess($query) {
        return $query;
    }
}