<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if((($_SERVER['SERVER_ADDR']==$_SESSION['demo_server_ip']) && ($_SERVER['REMOTE_ADDR']!=$_SESSION['demo_developer_ip']) && ($_SESSION['id_user']==$_SESSION['demo_user_id'])) || ($_SESSION['sml_si']!=session_id())) {
    //DEMO CHECK
    die();
}
require_once("../../db/connection.php");
require_once("../functions.php");
$settings = get_settings();
$tripadvisor_api_key = $settings["tripadvisor_api_key"];
$base_url = 'https://api.content.tripadvisor.com/api/v1/location/search';
$query = trim($_POST['query'] ?? '');
$lat = trim($_POST['lat'] ?? '');
$lng = trim($_POST['lng'] ?? '');
if (empty($query)) {
    http_response_code(400);
    header('Content-Type: application/json');
    ob_end_clean();
    echo json_encode(['error' => _('Missing location')]);
    exit;
}
$params = [
    'key' => $tripadvisor_api_key,
    'searchQuery' => $query,
    'language' => get_tripadvisor_language($_SESSION['lang']),
];
if(!empty($lat) && !empty($lng)) {
    $params['latLong'] = $lat.",".$lng;
}
$api_url = $base_url . '?' . http_build_query($params);
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
http_response_code($http_code);
header('Content-Type: application/json');
ob_end_clean();
echo $response;

function get_tripadvisor_language($language_code) {
    switch ($language_code) {
        case 'ar_SA':
            $mapped_code = 'ar';
            break;
        case 'da_DK':
            $mapped_code = 'da';
            break;
        case 'de_DE':
            $mapped_code = 'de';
            break;
        case 'el_GR':
            $mapped_code = 'el';
            break;
        case 'en_GB':
            $mapped_code = 'en_UK';
            break;
        case 'en_US':
            $mapped_code = 'en';
            break;
        case 'es_ES':
            $mapped_code = 'es';
            break;
        case 'es_MX':
            $mapped_code = 'es_MX';
            break;
        case 'fil_PH':
            $mapped_code = 'en_PH';
            break;
        case 'fr_FR':
            $mapped_code = 'fr';
            break;
        case 'he_IL':
            $mapped_code = 'iw';
            break;
        case 'id_ID':
            $mapped_code = 'in';
            break;
        case 'it_IT':
            $mapped_code = 'it';
            break;
        case 'ja_JP':
            $mapped_code = 'ja';
            break;
        case 'ko_KR':
            $mapped_code = 'ko';
            break;
        case 'nb_NO':
            $mapped_code = 'no';
            break;
        case 'nl_NL':
            $mapped_code = 'nl';
            break;
        case 'pt_BR':
            $mapped_code = 'pt';
            break;
        case 'pt_PT':
            $mapped_code = 'pt_PT';
            break;
        case 'ru_RU':
            $mapped_code = 'ru';
            break;
        case 'sv_SE':
            $mapped_code = 'sv';
            break;
        case 'th_TH':
            $mapped_code = 'th';
            break;
        case 'tr_TR':
            $mapped_code = 'tr';
            break;
        case 'vi_VN':
            $mapped_code = 'vi';
            break;
        case 'zh_CN':
            $mapped_code = 'zh';
            break;
        case 'zh_TW':
            $mapped_code = 'zh_TW';
            break;
        default:
            $mapped_code = 'en';
            break;
    }
    return $mapped_code;
}