class WDP_WCEPO_Compatibility {
	const KEY = 'theme_complete_epo';
	const WDP_VERSION = '2.2.4';
	const TARGET_VERSION = '4.9.6';

	protected $tm_keys = array(
		'tm_epo_product_original_price',
		'tm_epo_options_prices',
		'tm_epo_product_price_with_options',
		'tmhasepo',
		'tmcartepo',
		'tmcartfee',
		'tmdata',
		'tm_epo_options_static_prices',
		'tmsubscriptionfee',
		'tmpost_data',
		'tmcartepo_data',
		'addons',
		//'tm_cart_item_key',
	);

	public function is_required() {
		return class_exists( 'Themecomplete_Extra_Product_Options_Setup' );
	}

	public function admin_install() {
	}

	public function remove_admin_hooks() {
	}

	/**
	 * @param WDP_Price_Display $price_display
	 */
	public function install( $price_display ) {
		$install = function() {
			add_filter( 'wdp_cart_item_data_before_apply', array( $this, 'wdp_cart_item_data_before_apply' ), 10, 2 );
			add_filter( 'wdp_original_cart_item_data', array( $this, 'wdp_original_cart_item_data' ), 10, 1 );
			add_filter( "wdp_get_product_regular_price", array( $this, 'wdp_get_product_any_price' ), 10, 3 );
			add_filter( "wdp_get_product_sale_price", array( $this, 'wdp_get_product_any_price' ), 10, 3 );
			add_filter( "wdp_get_product_initial_price", array( $this, 'wdp_get_product_any_price' ), 10, 3 );
			add_filter( 'wdp_save_cart_item_keys', array( $this, 'wdp_save_cart_item_keys' ), 10, 1 );
		};

		if ( did_action('init') ) {
			$install();
		} else {
			add_action( 'init', $install );
		}
	}

	public function remove_hooks() {
		if ( did_action( 'init' ) ) {
			remove_filter( 'wdp_cart_item_data_before_apply', array( $this, 'wdp_cart_item_data_before_apply' ), 10 );
			remove_filter( 'wdp_original_cart_item_data', array( $this, 'wdp_original_cart_item_data' ), 10 );
			remove_filter( "wdp_get_product_regular_price", array( $this, 'wdp_get_product_any_price' ), 10 );
			remove_filter( "wdp_get_product_sale_price", array( $this, 'wdp_get_product_any_price' ), 10 );
			remove_filter( "wdp_get_product_initial_price", array( $this, 'wdp_get_product_any_price' ), 10 );
			remove_filter( 'wdp_save_cart_item_keys', array( $this, 'wdp_save_cart_item_keys' ), 10 );
		}
	}

	public function wdp_cart_item_data_before_apply( $cart_item_data, $original_cart_item_data ) {
		foreach ( $this->tm_keys as $key ) {
			if ( isset( $original_cart_item_data[ $key ] ) ) {
				$cart_item_data[ $key ] = $original_cart_item_data[ $key ];
			}
		}

		return $cart_item_data;
	}

	public function wdp_original_cart_item_data( $original_cart_item_data ) {
		foreach ( $this->tm_keys as $key ) {
			if ( isset( $original_cart_item_data[ $key ] ) ) {
				unset( $original_cart_item_data[ $key ] );
			}
		}

		return $original_cart_item_data;
	}

	public function wdp_get_product_any_price( $price, $product, $item_meta ) {
		if ( $price !== "" && isset( $item_meta['tm_epo_options_prices'] ) ) {
			$price = floatval( $price ) + floatval( $item_meta['tm_epo_options_prices'] );
		}

		return $price;
	}

	public function wdp_save_cart_item_keys( $keys ) {
		return array_merge( $keys, $this->tm_keys );
	}
}

$comp = new WDP_WCEPO_Compatibility();
if($comp->is_required() ) {
    $comp->install(null);
}