<?php

use Bitrix\Main\Context;
use Bitrix\Main\Loader;
use Bitrix\Main\SiteTable;
use intec\Core;
use intec\core\collections\Arrays;
use intec\core\helpers\Json;
use intec\core\net\Url;
use intec\regionality\models\Region;
use intec\regionality\models\SiteSettings;
use intec\regionality\tools\Domain as DomainTools;

if (empty($_POST['site'])) {
    http_response_code(404);
    die();
}

$requestedSiteId = trim((string)$_POST['site']);

if (
    $requestedSiteId === ''
    || mb_strlen($requestedSiteId) > 64
    || !preg_match('/^[a-zA-Z0-9_.-]+$/', $requestedSiteId))
{
    http_response_code(404);
    die();
}

const STOP_STATISTICS = true;
const PUBLIC_AJAX_MODE = true;

require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php');

$context = Context::getCurrent();
$ctxLid = $context?->getSite();
$siteFromContext = ($ctxLid !== null && $ctxLid !== '') ? $ctxLid : null;
$siteFromConstant = (defined('SITE_ID') && SITE_ID !== '') ? SITE_ID : null;

if ($siteFromContext !== null && $siteFromConstant !== null && $siteFromContext !== $siteFromConstant) {
    http_response_code(404);
    die();
}

$currentSiteId = $siteFromConstant ?? $siteFromContext;

if ($currentSiteId === null || $currentSiteId === '' || $requestedSiteId !== $currentSiteId) {
    http_response_code(404);
    die();
}

$siteRow = SiteTable::getRow([
    'filter' => [
        '=LID' => $requestedSiteId,
        '=ACTIVE' => 'Y',
    ],
    'select' => ['LID'],
]);

if ($siteRow === null) {
    http_response_code(404);
    die();
}

if (!defined('SITE_ID')) {
    define('SITE_ID', $requestedSiteId);
} elseif ((string)constant('SITE_ID') !== $requestedSiteId) {
    http_response_code(404);
    die();
}

/**
 * @global CMain $APPLICATION
 */

if (
    !Loader::includeModule('intec.core') ||
    !Loader::includeModule('intec.regionality')
) {
    http_response_code(500);
    die();
}

$request = Core::$app->request;

if (!$request->getIsPost() && !$request->getIsAjax()) {
    http_response_code(403);
    die();
}

$region = $request->post('region');
$site = $request->post('site');
$url = $request->post('url');

if (empty($url))
    $url = $request->getHostInfo();

$sites = Arrays::fromDBResult(CSite::GetList($by = 'sort', $order = 'asc'))->indexBy('ID');
$site = $sites->get($site);
$response = [
    'status' => 'error'
];

if (!empty($site) && !empty($region)) {
    $siteSettings = SiteSettings::getCurrent();
    $region = Region::findOne($region);

    if (!empty($region)) {
        $regionCurrent = Region::getCurrent();

        if ($region->id !== $regionCurrent->id) {
            if ($region->active && $region->isForSites($site['ID'])) {
                $url = new Url($url);

                /** Если опция использования доменов активна */
                if ($siteSettings->domainsUse) {
                    $domain = $region->resolveDomain(IntecRegionality::CrossSiteUse() ? false : $site['ID'], true);
                    $domainRoot = DomainTools::getRoot($domain);
                    $domainCurrent = $request->getHostName();
                    $domainCurrent = DomainTools::getRoot($domainCurrent);

                    /** Если корневые домены совпадают или домен пустой, генерируем перенаправление на местный домен */
                    if (empty($domain) || $domainRoot === $domainCurrent) {
                        if (!empty($domain))
                            $url->setHost($domain);

                        Region::setCurrent($region);
                        Region::remember(
                            $region,
                            $siteSettings->domainsUse,
                            $siteSettings->regionRememberTime
                        );
                    } else {
                        $path = $url;
                        $path->setHost(null);
                        $path = $path->build();

                        $url = new Url($request->getHostInfo());
                        $url->setHost($domain);
                        $url->setPathString('/bitrix/admin/regionality_regions_select.php');
                        $url->getQuery()->setRange([
                            'path' => $path,
                            'region' => $region->id,
                            'site' => $site['ID'],
                            'current' => 'Y',
                            'lang' => LANGUAGE_ID
                        ]);
                    }
                } else {
                    Region::setCurrent($region);
                    Region::remember(
                        $region,
                        $siteSettings->domainsUse,
                        $siteSettings->regionRememberTime
                    );
                }

                $response = [
                    'status' => 'success',
                    'domain' => $domain,
                    'region' => $region->toArray(),
                    'action' => 'redirect',
                    'url' => $url->build()
                ];
            } else {
                $response['message'] = 'Region inactive or is not for this site.';
            }
        } else {
            Region::setCurrent($region);
            Region::remember(
                $region,
                $siteSettings->domainsUse,
                $siteSettings->regionRememberTime
            );

            $response['status'] = 'success';
        }
    } else {
        $response['message'] = 'Unknown region.';
    }
} else {
    $response['message'] = 'Unknown site or region.';
}

echo Json::encode($response, 320, true);

CMain::FinalActions();
