<?php if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();

use Bitrix\Main\Loader;
use Bitrix\Main\Localization\Loc;
use intec\core\helpers\ArrayHelper;
use intec\core\helpers\StringHelper;
use intec\template\Template;

/**
 * @var array $arParams
 * @var array $arResult
 * @var CBitrixComponentTemplate $this
 * @global $APPLICATION
 */

if (!Loader::includeModule('iblock'))
    return;

Loc::loadMessages(__FILE__);
$template = Template::getInstance();

$socialItems = [
    'VK',
    'INSTAGRAM',
    'FACEBOOK',
    'TWITTER',
    'YOUTUBE',
    'MAX',
    'RUTUBE',
    'OK',
    'VIBER',
    'WHATSAPP',
    'ZEN',
    'MAILRU',
    'TELEGRAM',
    'PINTEREST',
    'TIKTOK',
    'SNAPCHAT',
    'LINKEDIN',
    'SKYPE'
];

$arParams = ArrayHelper::merge([
    'SETTINGS_USE' => 'N',
    'USER_CONSENT' => 'N',
    'USER_CONSENT_ID' => 'N',
    'USER_CONSENT_REQUIRED' => 'N',
    'USER_MAILING' => 'N',
    'USER_MAILING_ID' => 'N',
    'USER_MAILING_REQUIRED' => 'N',
    'TRANSPARENCY' => 'N',
    'UPPER_COLOR_PRIMARY' => 'N',
    'AUTHORIZATION_SHORT' => 'N',
    'THEME_LIGHT' => 'N',
    'LOGOTYPE_WIDTH' => '130',
    'CONTACTS_MAIN' => null,
    'CONTACTS_ADDRESS_SHOW' => 'N',

    'MENU_MAIN_ROOT' => null,
    'MENU_MAIN_CHILD' => null,
    'MENU_MAIN_LEVEL' => null,
    //'MENU_PERSONAL_SECTION' => 'personal',

    'SOCIAL_SHOW' => 'N',
    'SOCIAL_VIEW' => 'base',
    'SOCIAL_GRAY' => 'Y',
    'SOCIAL_HOVER_SITE' => 'Y'
], $arParams);

foreach ($socialItems as $socialItem)
    if (!ArrayHelper::keyExists('SOCIAL_' . $socialItem, $arParams))
        $arParams['SOCIAL_' . $socialItem] = null;

if ($arParams['SETTINGS_USE'] === 'Y')
    include 'modifiers/settings.php';

$visual = [
    'HEADER' => [
        'TEMPLATE' => '',
        'TRANSPARENCY' => $arParams['TRANSPARENCY'] === 'Y',
        'UPPER_COLOR_PRIMARY' => $arParams['UPPER_COLOR_PRIMARY'] === 'Y'
    ],
    'TAGLINE' => [
        'SHOW' => $arParams['TAGLINE_SHOW'] === 'Y' && !empty($arParams['TAGLINE']),
        'VALUE' => mb_strimwidth($arParams['TAGLINE'], 0, 224, ' ...')
    ],
    'AUTH' => [
        'SHOW' => $arParams['AUTHORIZATION_SHOW'] === 'Y',
        'SHORT' => $arParams['AUTHORIZATION_SHORT'] === 'Y',
        'FORMAT' => ArrayHelper::fromRange(['form', 'url'], $arParams['AUTHORIZATION_FORMAT']),
    ],
    'SEARCH' => [
        'SHOW' => $arParams['SEARCH_SHOW'] === 'Y'
    ],
    'SOCIAL' => [
        'SHOW' => $arParams['SOCIAL_SHOW'] === 'Y',
        'VIEW' => ArrayHelper::fromRange(['base', 'square', 'rounded', 'circle'], $arParams['SOCIAL_VIEW']),
        'GRAY' => $arParams['SOCIAL_GRAY'] === 'Y',
        'HOVER_SITE' => $arParams['SOCIAL_HOVER_SITE'] === 'Y' && $arParams['SOCIAL_GRAY'] === 'Y',
        'ITEMS' => [
            'BASE' => [],
            'OTHER' => []
        ]
    ],
    'USER' => [
        'AUTHORIZE' => $USER->IsAuthorized(),
    ],
    'LINKS' => [
        'PROFILE' => [
            'URL' =>  !empty($arParams["PROFILE_URL"]) ? $arParams["PROFILE_URL"] : '/personal/profile',
        ],
        'LOGIN' => [
            'URL' => !empty($arParams['LOGIN_URL']) ? $arParams['LOGIN_URL'] : '/auth',
        ],
        'DEFERRED' => [
            'USE' => $arParams['DEFERRED_USE'] === 'Y',
            'URL' => $arParams['DEFERRED_USE'] === 'Y' ? $arParams['DEFERRED_URL'] : null
        ],
        'COMPARE' => [
            'USE' => $arParams['COMPARE_USE'] === 'Y',
            'URL' => !empty($arParams['COMPARE_URL']) ? $arParams['COMPARE_URL'] : null
        ],
        'BASKET' => [
            'USE' => $arParams['BASKET_USE'] === 'Y',
            'URL' => !empty($arParams['BASKET_URL']) ? $arParams['BASKET_URL'] : null,
        ],
        'DELAY' => [
            'URL' =>  !empty($arParams['BASKET_URL']) ? $arParams['BASKET_URL'] : null,
        ],
    ],
    'BASKET' => [
        'SHOW' => $arParams['BASKET_SHOW'] === 'Y',
    ],
    'DELAY' => [
        'SHOW' => $arParams['DELAY_SHOW'] === 'Y'
    ],
    'COMPARE' => [
        'SHOW' => $arParams['COMPARE_SHOW'] === 'Y'
    ],
    'THEME' => $arParams['THEME_LIGHT'] === 'Y' ? 'white' : 'black'
];

if ($visual['SOCIAL']['SHOW']) {
    $i = 1;

    foreach ($socialItems as $socialItem)
        if (!empty($arParams['SOCIAL_' . $socialItem])) {
            if ($i > 1)
                $visual['SOCIAL']['ITEMS']['OTHER'][$socialItem]['LINK'] = $arParams['SOCIAL_' . $socialItem];
            else
                $visual['SOCIAL']['ITEMS']['BASE'][$socialItem]['LINK'] = $arParams['SOCIAL_' . $socialItem];

            $i++;
        }

    if (empty($visual['SOCIAL']['ITEMS']['BASE']) && empty($visual['SOCIAL']['ITEMS']['OTHER']))
        $visual['SOCIAL']['SHOW'] = false;
}

$arResult['PROFILE'] = [
    'ITEMS' => [],
    'EXEPT' => [
        'COMPARE',
        'DEFERRED'
    ]
];

foreach ($visual['LINKS'] as $key => $url) {
    if (!ArrayHelper::isIn($key, $arResult['PROFILE']['EXEPT'])) {
        foreach ([$key] as $links) {
            $codeLinks = 'IC_HEADER_STANDARD_'.$links;
            $arResult['PROFILE']['ITEMS'][] = [
                'URL' => $visual['LINKS'][$links]['URL'],
                'CODE' => $links,
                'VALUE' => Loc::getMessage($codeLinks),
            ];
        }
    }
}

unset($links);

$arParams['SEARCH_MODE'] = ArrayHelper::fromRange(['site', 'catalog', 'advanced'], $arParams['SEARCH_MODE']);

if ($arParams['SEARCH_MODE'] === 'site') {
    $arParams['SEARCH_PARAM_NUM_CATEGORIES'] = 1;
    $arParams['SEARCH_PARAM_CATEGORY_0'] = ['no'];
    $arParams['SEARCH_PARAM_CATEGORY_0_iblock_catalogs'] = ['all'];
    $arParams['SEARCH_PARAM_CATEGORY_0_TITLE'] = Loc::getMessage('IC_HEADER_STANDARD_SEARCH_MODE_SITE_TITLE');
    $arParams['SEARCH_PARAM_SHOW_OTHERS'] = 'N';
    $arParams['SEARCH_PARAM_PAGE'] = $arParams['SEARCH_SITE_URL'];
} elseif ($arParams['SEARCH_MODE'] === 'catalog') {
    $arParams['SEARCH_PARAM_NUM_CATEGORIES'] = 1;
    $arParams['SEARCH_PARAM_CATEGORY_0'] = ['iblock_catalogs'];
    $arParams['SEARCH_PARAM_CATEGORY_0_iblock_catalogs'] = ['all'];
    $arParams['SEARCH_PARAM_CATEGORY_0_TITLE'] = Loc::getMessage('IC_HEADER_STANDARD_SEARCH_MODE_CATALOG_TITLE');
    $arParams['SEARCH_PARAM_SHOW_OTHERS'] = 'N';
    $arParams['SEARCH_PARAM_PAGE'] = $arParams['CATALOG_URL'];
}

$arResult['LOGOTYPE']['LINK'] = [
    'USE' => true,
    'VALUE' => $APPLICATION->GetCurPage(false) !== SITE_DIR ? SITE_DIR : null
];

if ($template->mainPage)
    $template->transparentHeader = $template->transparentHeader && $visual['HEADER']['TRANSPARENCY'];
else
    $template->transparentHeader = $visual['HEADER']['TRANSPARENCY'];

$arResult['LOGOTYPE']['DESKTOP']['WIDTH'] = $arParams['LOGOTYPE_WIDTH'] ?: '130';

if (empty($arResult['LOGOTYPE']['LINK']['VALUE']))
    $arResult['LOGOTYPE']['LINK']['USE'] = false;

$arResult['CONTACTS']['MAIN'] = null;

include(__DIR__ . '/modifiers/contacts.php');

$sites = CSite::GetByID(SITE_ID);
$site = $sites->Fetch();
$arResult['COMPANY_NAME'] = $site['SITE_NAME'];

if (Loader::includeModule('form')) {
    $arResult['CONTACTS']['FORM'] = [
        'SHOW' => $arParams['FORMS_CALL_SHOW'] === 'Y' && !empty($arParams['FORMS_CALL_ID']),
        'TITLE' => $arParams['FORMS_CALL_TITLE'],
        'COMPONENT' => 'bitrix:form.result.new',
        'TEMPLATE' => $arParams['FORMS_CALL_TEMPLATE'],
        'PARAMETERS' => [
            'WEB_FORM_ID' => $arParams['FORMS_CALL_ID'],
            'SETTINGS_USE' => $arParams['SETTINGS_USE'],
            'USER_CONSENT' => $arParams['USER_CONSENT'],
            'USER_CONSENT_ID' => $arParams['USER_CONSENT_ID'],
            'USER_CONSENT_REQUIRED' => $arParams['USER_CONSENT_REQUIRED'],
            'USER_MAILING' => $arParams['USER_MAILING'],
            'USER_MAILING_ID' => $arParams['USER_MAILING_ID'],
            'USER_MAILING_REQUIRED' => $arParams['USER_MAILING_REQUIRED'],
            'BUTTON_RESET_SHOW' => 'N',
            'LIST_URL' => ''
        ]
    ];
} else if (Loader::includeModule('intec.startshop')) {
    $arResult['CONTACTS']['FORM'] = [
        'SHOW' => $arParams['FORMS_CALL_SHOW'] === 'Y' && !empty($arParams['FORMS_CALL_ID']),
        'TITLE' => $arParams['FORMS_CALL_TITLE'],
        'COMPONENT' => 'intec:startshop.forms.result.new',
        'TEMPLATE' => $arParams['FORMS_CALL_TEMPLATE'],
        'PARAMETERS' => [
            'FORM_ID' => $arParams['FORMS_CALL_ID'],
            'SETTINGS_USE' => $arParams['SETTINGS_USE'],
            'USER_CONSENT' => $arParams['USER_CONSENT'],
            'USER_CONSENT_ID' => $arParams['USER_CONSENT_ID'],
            'USER_CONSENT_REQUIRED' => $arParams['USER_CONSENT_REQUIRED'],
            'USER_MAILING' => $arParams['USER_MAILING'],
            'USER_MAILING_ID' => $arParams['USER_MAILING_ID'],
            'USER_MAILING_REQUIRED' => $arParams['USER_MAILING_REQUIRED'],
            'BUTTON_RESET_SHOW' => 'N',
            'AJAX_MODE' => 'Y',
            'AJAX_OPTION_JUMP' => 'N',
            'AJAX_OPTION_STYLE' => 'Y',
            'AJAX_OPTION_HISTORY' => 'N',
            'AJAX_OPTION_ADDITIONAL' => '',
            'REQUEST_VARIABLE_ACTION' => 'action',
            'FORM_VARIABLE_CAPTCHA_SID' => 'CAPTCHA_SID',
            'FORM_VARIABLE_CAPTCHA_CODE' => 'CAPTCHA_CODE'
        ]
    ];
} else {
    $arResult['CONTACTS']['FORM'] = [
        'SHOW' => false,
        'TITLE' => ''
    ];
}

if ($arResult['CONTACTS']['FORM']['SHOW'] && empty($arParams['FORMS_CALL_ID']))
    $arResult['CONTACTS']['FORM']['SHOW'] = false;

$arResult['MENU'] = [];
$arResult['MENU']['MAIN'] = [
    'SHOW' => true,
    'TEMPLATE' => empty($arParams['MENU_MAIN_TEMPLATE']) ? 'horizontal.1' : $arParams['MENU_MAIN_TEMPLATE'],
    'ROOT' => $arParams['MENU_MAIN_ROOT'],
    'CHILD' => $arParams['MENU_MAIN_CHILD'],
    'LEVEL' => $arParams['MENU_MAIN_LEVEL'],
    'WIDE' => false
];

if($arParams['MENU_WIDE_MODE'] === 'inner')
    $arResult['MENU']['MAIN']['WIDE'] = $APPLICATION->GetCurPage() !== '/';
else if ($arParams['MENU_WIDE_MODE'] === 'main')
    $arResult['MENU']['MAIN']['WIDE'] = $APPLICATION->GetCurPage() === '/';
else if ($arParams['MENU_WIDE_MODE'] !== 'none')
    $arResult['MENU']['MAIN']['WIDE'] = true;

$sPrefix = 'AUTH_FORM_';
$arParameters = [];
foreach ($arParams as $sKey => $sValue)
    if (StringHelper::startsWith($sKey, $sPrefix)) {
        $sKey = StringHelper::cut($sKey, StringHelper::length($sPrefix));
        $arResult['AUTHORIZATION'][$sKey] = $sValue;
        $arResult['AUTHORIZATION']['AUTH_FORGOT_PASSWORD_URL'] =  $arParams['PASSWORD_URL'];
        $arResult['AUTHORIZATION']['USE_REGISTRATION'] =  $arParams['AUTHORIZATION_FORM_USE_REGISTRATION'];
        $arResult['AUTHORIZATION']['USER_CONSENT'] =  $arParams['USER_CONSENT'];
        $arResult['AUTHORIZATION']['USER_CONSENT_ID'] =  $arParams['USER_CONSENT_ID'];
        $arResult['AUTHORIZATION']['USER_CONSENT_REQUIRED'] =  $arParams['USER_CONSENT_REQUIRED'];
        $arResult['AUTHORIZATION']['USER_MAILING'] =  $arParams['USER_MAILING'];
        $arResult['AUTHORIZATION']['USER_MAILING_ID'] =  $arParams['USER_MAILING_ID'];
        $arResult['AUTHORIZATION']['USER_MAILING_REQUIRED'] =  $arParams['USER_MAILING_REQUIRED'];
    }


$arResult['REGIONALITY'] = [
    'USE' => $arParams['REGIONALITY_USE'] === 'Y' && Loader::includeModule('intec.regionality'),
    'template' => $arParams['REGIONALITY_TEMPLATE'],
    'parameters' => [
        'settingsUse' => $arParams['SETTINGS_USE'],
        'color' => $visual['THEME'] === 'black' ? 'ntr-d' : 'ntr-b'
    ]
];

$arResult['VISUAL'] = $visual;

unset($visual, $sites, $site);