<?php

namespace LoftyIDX\includes;

use LoftyIDX\includes\LoftyIDX;

defined('ABSPATH') || exit;

class LoftyIDXRouter
{
    const listingPageKeys = ['search', 'featured_listing', 'sold_listing', 'market_report_page'];

    const defaultRoutes = [
        'listing' => [
            'url' => '/listing',
            'pattern' => '^listing$',
            'match' => 'index.php?lofty_page=listing',
            'template' => 'listing.php',
            'pageKey' => 'search'    // fixed category value
        ],
        'feature-listing' => [
            'url' => '/feature-listing',
            'pattern' => '^feature-listing$',
            'match' => 'index.php?lofty_page=feature-listing',
            'template' => 'feature-listing.php',
            'pageKey' => 'featured_listing'
        ],
        'listing-detail' => [
            'url' => '/listing-detail/:lofty_listing_id/:lofty_listing_address',
            'pattern' => '^listing-detail/([^/]+)/([^/]+)/?$',
            'match' => 'index.php?lofty_page=listing-detail&lofty_listing_id=$matches[1]&lofty_listing_address=$matches[2]',
            'template' => 'listing-detail.php',
            'pageKey' => 'listing_detail'
        ],
        'sold-listing' => [
            'url' => '/sold-listing',
            'pattern' => '^sold-listing$',
            'match' => 'index.php?lofty_page=sold-listing',
            'template' => 'sold-listing.php',
            'pageKey' => 'sold_listing'
        ],
        'sold-listing-detail' => [
            'url' => '/sold-listing/detail/:lofty_listing_id/:lofty_listing_address',
            'pattern' => '^sold-listing/detail/([^/]+)/([^/]+)/?$',
            'match' => 'index.php?lofty_page=sold-listing-detail&lofty_listing_id=$matches[1]&lofty_listing_address=$matches[2]',
            'template' => 'sold-listing-detail.php',
            'pageKey' => 'sold_listing_detail'
        ],
        'profile' => [
            'url' => '/profile',
            'pattern' => '^profile',
            'match' => 'index.php?lofty_page=profile',
            'template' => 'profile.php',
            'pageKey' => 'profile'
        ],
        'market-report' => [
            'url' => '/market-report-collection',
            'pattern' => '^market-report',
            'match' => 'index.php?lofty_page=market-report',
            'template' => 'market-report.php',
            'pageKey' => 'market_report_page'
        ],
        'pwreset' => [
            'url' => '/pwreset',
            'pattern' => '^pwreset$',
            'match' => 'index.php?lofty_page=pwreset',
            'template' => 'password-reset.php',
            'pageKey' => 'pwreset'
        ],
    ];

    public  static function getRoutes($from_api = true)
    {
        if (!$from_api) {
            return self::defaultRoutes;
        }
        $proxy = LoftyIDX::get_instance()->services['proxy'];
        $resp = $proxy->get_data('/admin/common/page/router');
        if ($resp === false) {
            return self::defaultRoutes;
        }

        $routes = $resp['data'];
        $routes_map = [];
        foreach ($routes as $route) {
            $routes_map[$route['pageKey']] = trim($route['uri'], '/');
        }

        $new_routes = array_merge_recursive([], self::defaultRoutes);
        foreach ($new_routes as $k => &$item) {
            if (isset($item['pageKey']) && array_key_exists($item['pageKey'], $routes_map)) {
                $uri = $routes_map[$item['pageKey']];
                $item['url'] = $uri;
                if (in_array($item['pageKey'], self::listingPageKeys)) {
                    $item['pattern'] = '^' . $uri . '$';
                }
            }
        }

        return $new_routes;
    }

    public static function parseAllParams()
    {
        $all_keys = [];
        $routes = self::getRoutes(false);
        foreach ($routes as $route) {
            if (isset($route['match'])) {
                $match = $route['match'];
                $params = explode('&', wp_parse_url($match, PHP_URL_QUERY));
                foreach ($params as $param) {
                    list($key, $value) = explode('=', $param);
                    $all_keys[] = $key;
                }
            }
        }
        return array_unique($all_keys);
    }
}
