<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

require_once __DIR__ . '/constants.php';
require_once __DIR__ . '/utils.php';
require_once __DIR__ . '/options.php';

function r2b2_placement_html( $script_src, $div_id = null ) {
	$html = '';
	if ( ! empty( $div_id ) ) {
		$html .= '<div id="' . esc_attr( $div_id ) . '" class="r2b2-ad"></div>';
	}
	$html .= '<script src="' . esc_attr( $script_src ) . '" async"></script>';

	return $html;
}

function r2b2_selfpromo_html( $name, $width = 300, $height = 300 ) {
	$html = '';
	$html = '<div class="r2b2-ad">';
	$html .= '<iframe width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" frameborder="0" ';
	$html .= 'src="' . esc_attr( R2B2_DELIVERY_DOMAIN . 'static/selfpromo/banner.html?name=' . urlencode( $name ) ) . '"></iframe>';
	$html .= '</div>';

	return $html;
}

function r2b2_delivery_url( $dgpm, $selfpromo = false, $divId = '' ) {
	$url = R2B2_DELIVERY_DOMAIN . 'get/';
	$url .= join( '/', [
			$dgpm['d'],
			$dgpm['g'],
			$dgpm['p'],
		]
	);
	if ( $dgpm['m'] ) {
		$url .= '/mobile';
	}
	$params = [];
	if ( $selfpromo ) {
		$params[] = 'selfpromo=true';
	}
	if ( $divId ) {
		$params[] = 'opt[dom][selector]=%23' . $divId;
	}
	$url .= '?' . join( '&', $params );

	return $url;
}

function r2b2_enqueue_placement( $dgpm, $track_selfpromo = false ) {
	wp_enqueue_script(
		'r2b2-placement-' . join( '-', $dgpm ),
		r2b2_delivery_url( $dgpm, $track_selfpromo ),
		[], R2B2_PLUGIN_VERSION, [ 'in_footer' => true ]
	);
}

function r2b2_get_placement_data() {
	$options    = get_option( R2B2_OPTIONS );
	$listData   = $options[ R2B2_OPTION_PLACEMENT_LIST_DATA ] ?? '';
	$placements = explode( "\n", $listData ) ?? [];
	foreach ( $placements as $index => $placement ) {
		$placements[ $index ] = json_decode( $placement, true );
	}

	return $placements;
}

function r2b2_get_mediatype( $placement_name ) {
	if ( str_contains( $placement_name, 'fixed' ) || str_contains( $placement_name, 'sticky' ) ) {
		return R2B2_FORMAT_STICKY;
	} elseif ( str_contains( $placement_name, 'vignette' ) ) {
		return R2B2_FORMAT_VIGNETTE;
	} elseif ( str_contains( $placement_name, 'interscroll' ) ) {
		return R2B2_FORMAT_VIGNETTE;
	} elseif ( str_contains( $placement_name, 'in-media' ) || str_contains( $placement_name, 'inmedia' ) ) {
		return R2B2_FORMAT_VIGNETTE;
	} elseif ( str_contains( $placement_name, 'native' ) ) {
		return R2B2_FORMAT_VIGNETTE;
	} elseif ( str_contains( $placement_name, 'outstream' ) ) {
		return R2B2_FORMAT_VIGNETTE;
	} elseif ( str_contains( $placement_name, 'branding' ) || str_contains( $placement_name, 'skin' ) ) {
		return R2B2_FORMAT_VIGNETTE;
	} else {
		return R2B2_FORMAT_BANNER;
	}
}

function r2b2_is_placement_mobile( $placement_name ) {
	return str_contains( $placement_name, 'mobile' );
}
