<?php
/**
 * Hotels Booking Core Functions.
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Get full list of currency codes.
 * @return array
 */
if(!function_exists('get_dt_theme_hb_currencies')) {
	function get_dt_theme_hb_currencies() {
		return array_unique(
			apply_filters( 'dt_theme_hb_currencies',
				array(
					'AED' => __( 'United Arab Emirates Dirham', 'trendytravel' ),
					'AUD' => __( 'Australian Dollars', 'trendytravel' ),
					'BDT' => __( 'Bangladeshi Taka', 'trendytravel' ),
					'BRL' => __( 'Brazilian Real', 'trendytravel' ),
					'BGN' => __( 'Bulgarian Lev', 'trendytravel' ),
					'CAD' => __( 'Canadian Dollars', 'trendytravel' ),
					'CLP' => __( 'Chilean Peso', 'trendytravel' ),
					'CNY' => __( 'Chinese Yuan', 'trendytravel' ),
					'COP' => __( 'Colombian Peso', 'trendytravel' ),
					'CZK' => __( 'Czech Koruna', 'trendytravel' ),
					'DKK' => __( 'Danish Krone', 'trendytravel' ),
					'EUR' => __( 'Euros', 'trendytravel' ),
					'HKD' => __( 'Hong Kong Dollar', 'trendytravel' ),
					'HRK' => __( 'Croatia kuna', 'trendytravel' ),
					'HUF' => __( 'Hungarian Forint', 'trendytravel' ),
					'ISK' => __( 'Icelandic krona', 'trendytravel' ),
					'IDR' => __( 'Indonesia Rupiah', 'trendytravel' ),
					'INR' => __( 'Indian Rupee', 'trendytravel' ),
					'ILS' => __( 'Israeli Shekel', 'trendytravel' ),
					'JPY' => __( 'Japanese Yen', 'trendytravel' ),
					'KRW' => __( 'South Korean Won', 'trendytravel' ),
					'MYR' => __( 'Malaysian Ringgits', 'trendytravel' ),
					'MXN' => __( 'Mexican Peso', 'trendytravel' ),
					'NGN' => __( 'Nigerian Naira', 'trendytravel' ),
					'NOK' => __( 'Norwegian Krone', 'trendytravel' ),
					'NZD' => __( 'New Zealand Dollar', 'trendytravel' ),
					'PHP' => __( 'Philippine Pesos', 'trendytravel' ),
					'PLN' => __( 'Polish Zloty', 'trendytravel' ),
					'GBP' => __( 'Pounds Sterling', 'trendytravel' ),
					'RON' => __( 'Romanian Leu', 'trendytravel' ),
					'RUB' => __( 'Russian Ruble', 'trendytravel' ),
					'SGD' => __( 'Singapore Dollar', 'trendytravel' ),
					'ZAR' => __( 'South African rand', 'trendytravel' ),
					'SEK' => __( 'Swedish Krona', 'trendytravel' ),
					'CHF' => __( 'Swiss Franc', 'trendytravel' ),
					'TWD' => __( 'Taiwan New Dollars', 'trendytravel' ),
					'THB' => __( 'Thai Baht', 'trendytravel' ),
					'TRY' => __( 'Turkish Lira', 'trendytravel' ),
					'USD' => __( 'US Dollars', 'trendytravel' ),
					'VND' => __( 'Vietnamese Dong', 'trendytravel' ),
				)
			)
		);
	}
}

/**
 * Get Currency symbol.
 * @param string $currency (default: '')
 * @return string
 */
if(!function_exists('get_dt_theme_hb_currency_symbol')) {
	function get_dt_theme_hb_currency_symbol( $currency = '' ) {
		if ( ! $currency ) {
			$currency = get_option('dt_theme_hb_currency');
		}
	
		switch ( $currency ) {
			case 'AED' :
				$currency_symbol = 'د.إ';
				break;
			case 'BDT':
				$currency_symbol = '&#2547;&nbsp;';
				break;
			case 'BRL' :
				$currency_symbol = '&#82;&#36;';
				break;
			case 'BGN' :
				$currency_symbol = '&#1083;&#1074;.';
				break;
			case 'AUD' :
			case 'CAD' :
			case 'CLP' :
			case 'MXN' :
			case 'NZD' :
			case 'HKD' :
			case 'SGD' :
			case 'USD' :
				$currency_symbol = '&#36;';
				break;
			case 'EUR' :
				$currency_symbol = '&euro;';
				break;
			case 'CNY' :
			case 'RMB' :
			case 'JPY' :
				$currency_symbol = '&yen;';
				break;
			case 'RUB' :
				$currency_symbol = '&#1088;&#1091;&#1073;.';
				break;
			case 'KRW' : $currency_symbol = '&#8361;'; break;
			case 'TRY' : $currency_symbol = '&#84;&#76;'; break;
			case 'NOK' : $currency_symbol = '&#107;&#114;'; break;
			case 'ZAR' : $currency_symbol = '&#82;'; break;
			case 'CZK' : $currency_symbol = '&#75;&#269;'; break;
			case 'MYR' : $currency_symbol = '&#82;&#77;'; break;
			case 'DKK' : $currency_symbol = 'kr.'; break;
			case 'HUF' : $currency_symbol = '&#70;&#116;'; break;
			case 'IDR' : $currency_symbol = 'Rp'; break;
			case 'INR' : $currency_symbol = 'Rs.'; break;
			case 'ISK' : $currency_symbol = 'Kr.'; break;
			case 'ILS' : $currency_symbol = '&#8362;'; break;
			case 'PHP' : $currency_symbol = '&#8369;'; break;
			case 'PLN' : $currency_symbol = '&#122;&#322;'; break;
			case 'SEK' : $currency_symbol = '&#107;&#114;'; break;
			case 'CHF' : $currency_symbol = '&#67;&#72;&#70;'; break;
			case 'TWD' : $currency_symbol = '&#78;&#84;&#36;'; break;
			case 'THB' : $currency_symbol = '&#3647;'; break;
			case 'GBP' : $currency_symbol = '&pound;'; break;
			case 'RON' : $currency_symbol = 'lei'; break;
			case 'VND' : $currency_symbol = '&#8363;'; break;
			case 'NGN' : $currency_symbol = '&#8358;'; break;
			case 'HRK' : $currency_symbol = 'Kn'; break;
			default    : $currency_symbol = ''; break;
		}
	
		return $currency_symbol;
	}
}

/**
 * Get Available Rooms.
 * @param string hotel_id
 * @return string
 */
add_action("wp_ajax_dt_theme_hbroom_available_lists", "dt_theme_hbroom_available_lists");
if(!function_exists('dt_theme_hbroom_available_lists')) {
	function dt_theme_hbroom_available_lists() {
		$hotelid = $_REQUEST['hotel_id'];
		
		if($hotelid != NULL) {
			$out = '';
			$hotel_settings = get_post_meta ( $hotelid, '_hotel_settings', TRUE );
			echo "<pre>";
			print_r($hotel_settings);
			echo "</pre>";
			if(array_key_exists("room-types",$hotel_settings)):
				$out .= '<option value="">'.__('Choose Room Type', 'trendytravel').'</option>';
				
				$room_list = array_filter(array_unique($hotel_settings["room-types"]));
				foreach($room_list as $room):
					$out .= '<option value="'.$room.'">'.get_the_title($room).'</option>';
				endforeach;
			endif;
			
			echo do_shortcode($out);
			die(1);
		}
	}
}

/**
 * Get Unavailable Dates.
 * @param string hotel_id, room_id
 * @return string
 */
add_action("wp_ajax_dt_theme_hbroom_unavailable_dates", "dt_theme_hbroom_unavailable_dates");
if(!function_exists('dt_theme_hbroom_unavailable_dates')) {
	function dt_theme_hbroom_unavailable_dates() {
		$hotelid = $_REQUEST['hotel_id'];
		$roomid = $_REQUEST['room_id'];
		
		if($roomid != NULL) {
			$availableoptions = get_option('hb_available_settings');
			$udates = $availableoptions[$hotelid][$roomid];
			
			echo do_shortcode($udates);
			die(1);
		}
	}
}

/**
 * Set Unavailable Dates.
 * @param string room_id & sel_dates
 * @return string
 */
add_action("wp_ajax_dt_theme_hbroom_set_unavailable", "dt_theme_hbroom_set_unavailable");
if(!function_exists('dt_theme_hbroom_set_unavailable')) {
	function dt_theme_hbroom_set_unavailable() {
		$hotelid = $_REQUEST['hotel_id'];
		$roomid = $_REQUEST['room_id'];
		$sdates = $_REQUEST['sdates'];
		
		if($roomid != NULL && $sdates != NULL) {
			$availableoptions = get_option('hb_available_settings');
			$udates = $availableoptions[$hotelid][$roomid];
			$udates = $udates.','.$sdates;
			$udates = explode(',', $udates);
			$udates = array_filter($udates);
			$udates = implode(',', $udates);
			$availableoptions[$hotelid][$roomid] = $udates;
			update_option('hb_available_settings', $availableoptions);
			
			echo do_shortcode($udates);
			die(1);
		}
	}
}

/**
 * Clear Unavailable Dates.
 * @param string room_id
 * @return string
 */
add_action("wp_ajax_dt_theme_hbroom_clear_unavailable", "dt_theme_hbroom_clear_unavailable");
if(!function_exists('dt_theme_hbroom_clear_unavailable')) {
	function dt_theme_hbroom_clear_unavailable() {
		$hotelid = $_REQUEST['hotel_id'];
		$roomid = $_REQUEST['room_id'];
		
		if($roomid != NULL) {
			$alldates = get_option('hb_available_settings');
			unset($alldates[$hotelid][$roomid]);
			update_option('hb_available_settings', $alldates);
			die(1);
		}
	}
}