<?php
/*
Plugin Name: Embed Media Alpha Script 
Plugin URI: 
Description: Embed Media Alpha Script 
Author: Vivek B
Version: 1.0
Author URI: 
*/

if ( ! defined( 'ABSPATH' ) ) exit;

function embedScript() {
	return embedScript::instance();
}
add_action( 'plugins_loaded', 'embedScript' );

class embedScript{

	private static $_instance = null;

	public $baseURL = "//insurance.mediaalpha.com/js/serve.js";

	public $formId  = 3;

	public $insuranceTypes = array(
							'Auto Insurance'        => 'https://quote.usinsuranceonline.com/launch?quoteType=AUT&refID=15990&refCampaign=&kw=&brand=36',
							'Health Insurance'      => 'https://quote.usinsuranceonline.com/launch?quoteType=HEA&refID=15990&refCampaign=&kw=&brand=10',
							'Home Insurance'        => 'https://quote.usinsuranceonline.com/launch?quoteType=HOM&refID=15990&refCampaign=&kw=&brand=10',
							'Life Insurance'        => 'https://quote.usinsuranceonline.com/launch?quoteType=LIF&refID=15990&refCampaign=&kw=&brand=10',
							'Long Term Care'        => 'https://quote.usinsuranceonline.com/launch?quoteType=LTC&refID=15990&refCampaign=&kw=&brand=10',
							'Renters Insurance'     => 'https://quote.2insure4less.com/launch?quoteType=REN&refID=15990&refCampaign=&kw=&brand=10',
							'Commercial Insurance'  => 'https://quote.usinsuranceonline.com/launch?quoteType=BIZ&refID=15990&refCampaign=&kw=&brand=10'
						  );

	public static function instance() {
		if ( is_null( self::$_instance ) )
			self::$_instance = new self();
		return self::$_instance;
	} // End instance()

	public function __construct(){
		add_filter( 'gform_confirmation_'.$this->formId, array( $this, 'custom_confirmation' ), 10, 4 );
		add_action( 'wp_footer', function(){
			?>
			<script>
				var types = <?php echo json_encode($this->insuranceTypes); ?>;
				jQuery( document ).ready(function(){
					jQuery("#gform_submit_button_"+<?php echo $this->formId; ?>).click(function(){
						console.log(types);
						if( !jQuery("#input_"+<?php echo $this->formId; ?>+"_6").val()|| 
							!jQuery("#input_"+<?php echo $this->formId; ?>+"_7").val() ){
							return true;
						}

						var selectedType = jQuery("#input_"+<?php echo $this->formId; ?>+"_7").val();
						window.open( types[selectedType], '_blank' );
					});
				});
			</script>
			<?php
		});
	}

	public function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
	   	//add_action( 'wp_head', array( $this, 'addScripts' ) );

	   	$confirmation = $this->addScripts( $entry );
	    return $confirmation;
	}

	public function addScripts( $entry ){

		$zipcode  = rgar( $entry, '6' );
		$leadType = rgar( $entry, '7' );

		$script = '<script type="text/javascript">
					/* WCM Investments / Organic - Leave Behind */
					var MediaAlphaExchange = {
					    "type": "ad_unit",
					    "placement_id": "NOWkAna_EzDr3J8VO6uen8AxDiluqw",
					    "version": "17",
					    "sub_1": "Low Car Ins",
					    "data": { 
					        "zip": "'.$zipcode.'"
					    }
					};
					</script>
					<script src="'.$this->baseURL.'"></script>';
		return $script;
	}

}
?>