<?php
 if ( ! defined( 'ABSPATH' ) ) exit;
 
 if ( !class_exists('WC_Cybersource_Device_Fingerprinting' ) ) {

	 class WC_Cybersource_Device_Fingerprinting {
		 
		 protected $org_id;
		 protected $sessionId;
		 protected $merchant_id;
		 protected $integration;
		 protected $sandbox;
		 protected $enabled;
		 
		 public function __construct(){
			 
			$cb_api = get_option( 'woocommerce_cybsawm_settings' );

			if(empty($cb_api)) return;
			
			$this->merchant_id = ! empty( $cb_api[ 'merchant_id' ]) ? $cb_api[ 'merchant_id' ] : '' ;
			
			if( $cb_api[ 'mode' ] == 'p' ){
				 $this->org_id = 'k8vif92e';
				 $this->sandbox = 0;
			} else {
				 $this->org_id = '1snn5n9w';
				 $this->sandbox = 1;
			}

			$this->integration = $cb_api[ 'cb_api' ];
			
			$sessionName = "WOOCYB_" . time();
			
			$this->sessionId =  $sessionName;

			$this->enabled = $cb_api[ 'enabled' ];
			
			add_filter( 'cybersource_session_id', array( $this, 'cybersource_session_id' ) );
			add_action( 'wp_footer', array( $this, 'sa_generate_browser_screen_input' ) );
			add_action( 'cybersource_device_fingerprint_profiler', array( $this, 'generate_fingerprint_input' ) );
			add_action( 'cybersource_so_device_fingerprint_profiler', array( $this, 'so_generate_fingerprint_input' ) );
			
			add_action( 'wp_enqueue_scripts', array( $this, 'load_script_profiler' ) );	
			
		 }
		 
		 public function load_script_profiler(){
		 	if( $this->integration != 'so'){
		 		wp_enqueue_script( 'cybersource-profiler', plugins_url( 'assets/js/wc-cybersource-device-fingerprint.min.js' , __FILE__ ) , 'jquery' ); 
		 	}
			
		 }
		 
		public function so_profiler(){

			if( $this->integration == 'so'  && is_checkout() === true && is_wc_endpoint_url( 'order-received' ) === false && is_wc_endpoint_url( 'order-pay' ) === false){
				wp_enqueue_script( 'cybersource-profiler', plugins_url( 'assets/js/wc-cybersource-device-fingerprint.min.js' , __FILE__ ) , 'jquery' ); 
				$this->so_generate_fingerprint_input();	
			}
			
		}

		public function so_generate_fingerprint_input(){
			?>
			<script>
				jQuery(document).ready(function( $ ) {
					$fingerprint = $('<input type="hidden" id="device_fingerprint_id" name="device_fingerprint_id" value="' + cybsDeviceFingenprintProfiler("<?php echo $this->merchant_id; ?>","<?php echo $this->sandbox?>", "<?php echo apply_filters( 'cybersource_session_id', false ) ?>") + '">');

					$( ".woocommerce-checkout" ). append( $fingerprint );
				});
			 </script>
			<?php
			
		}

		public function sa_generate_browser_screen_input(){

			if($this->enabled == 'no' ) return; 
			
			?>
			<script>
				jQuery(document).ready(function( $ ) {

			 		var screenHeightVal = screen.height;


					$screenHeight = $('<input type="hidden" id="customer_browser_screen_height" name="customer_browser_screen_height" value="' + screenHeightVal + '">');

					$( ".woocommerce-checkout" ). append( $screenHeight );

					var screenWidthVal = screen.width;

					$screenWidth = $('<input type="hidden" id="customer_browser_screen_width" name="customer_browser_screen_width" value="' + screenWidthVal + '">');

					$( ".woocommerce-checkout" ). append( $screenWidth );

				});
			 </script>
			<?php
			
		}
		
		public function generate_fingerprint_input(){
			?>
			<script>
			 document.write('<input type="hidden" id="device_fingerprint_id" name="device_fingerprint_id" value="' + cybsDeviceFingenprintProfiler("<?php echo $this->merchant_id; ?>","<?php echo $this->sandbox?>", "<?php echo apply_filters( 'cybersource_session_id', false ) ?>") + '">');
			 //var screenHeight = screen.height;
			 //	document.write('<input type="hidden" id="customer_browser_screen_height" name="customer_browser_screen_height" value="' + screenHeight + '">');
			//var screenWidth = screen.width;
			//	document.write('<input type="hidden" id="customer_browser_screen_width" name="customer_browser_screen_width" value="' + screenWidth + '">');
			</script>
			 
			<?php
			
		}
		
		
		public function cybersource_session_id(){
			return $this->sessionId;	
		}

	 }
	 
	 function cybersource_device_fingerprinting_init(){
		 return new WC_Cybersource_Device_Fingerprinting();
	 } 
	 
	 add_action( 'init', 'cybersource_device_fingerprinting_init', 0);
	 
 } 
