<?php
class AVUP_Xml_Item extends SimpleXMLElement {

	protected $item;

	public function init( $params, $feed_infos ) {

		if ( empty( $params ) || empty( $feed_infos ) ) {
			return false;
		}

		$params     = json_decode( json_encode( $params ), true );
		$feed_infos = json_decode( json_encode( $feed_infos ), true );
		$partner_id = $params['partner']['id'];

		$this->item['id']              = $this->get_partner_feed_infos( 'feed_item_id', $partner_id, $feed_infos );
		$this->item['title']           = $this->get_partner_feed_infos( 'feed_item_title', $partner_id, $feed_infos );
		$this->item['desc']            = $this->get_partner_feed_infos( 'feed_item_desc', $partner_id, $feed_infos );
		$this->item['tags']            = $this->get_partner_feed_infos( 'feed_item_tags', $partner_id, $feed_infos );
		$this->item['duration']        = $this->get_partner_feed_infos( 'feed_item_length', $partner_id, $feed_infos );
		$this->item['duration_format'] = $this->get_partner_feed_infos( 'feed_item_length_format', $partner_id, $feed_infos );
		$this->item['thumb_url']       = $this->get_partner_feed_infos( 'feed_item_thumb_url', $partner_id, $feed_infos );
		$this->item['thumbs_urls']     = $this->get_partner_feed_infos( 'feed_item_thumbs_urls', $partner_id, $feed_infos );
		$this->item['trailer_url']     = $this->get_partner_feed_infos( 'feed_item_trailer_url', $partner_id, $feed_infos );
		$this->item['video_url']       = $this->get_partner_feed_infos( 'feed_item_video_url', $partner_id, $feed_infos );
		$this->item['tracking_url']    = $this->get_partner_feed_infos( 'feed_item_join_url', $partner_id, $feed_infos );
		$this->item['code']            = $this->get_partner_feed_infos( 'feed_item_code', $partner_id, $feed_infos );
		$this->item['actors']          = $this->get_partner_feed_infos( 'feed_item_actors', $partner_id, $feed_infos );
	}

	public function get_id() {
		return AVUP_Item::get_id( $this->item );
	}

	public function is_valid() {
		return AVUP_Item::is_valid( $this->item );
	}

	public function get_data_for_js() {
		return AVUP_Item::get_data_for_js( $this->item );
	}

	public function get_data_for_json( $cpt = 0 ) {
		return AVUP_Item::get_data_for_json( $this->item, $cpt = 0 );
	}

	private function get_partner_feed_infos( $partner_feed_item, $partner_id, $feed_infos ) {

		$output         = false;
		$feed_item_type = isset( $feed_infos[ $partner_feed_item ] ) ? key( $feed_infos[ $partner_feed_item ] ) : null;
		$short_item     = '';

		if ( isset( $feed_infos[ $partner_feed_item ][ $feed_item_type ] ) ) {

			$short_item = $feed_infos[ $partner_feed_item ][ $feed_item_type ];

			$results = array();
			preg_match_all( '/<%(.+)%>/U', $short_item, $results );

			foreach ( $results[1] as $result ) {
				if ( strpos( $result, 'get_partner_option' ) !== false ) {
					$saved_partner_options = WPSCORE()->get_product_option( 'AVUP', $partner_id . '_options' );
					$option                = str_replace( array( 'get_partner_option("', '")' ), array( '', '' ), $result );
					$new_result            = '$saved_partner_options["' . $option . '"]';
					$short_item            = str_replace( '<%' . $result . '%>', eval( 'return ' . $new_result . ';' ), $short_item );
				} else {
					$short_item = str_replace( '<%' . $result . '%>', eval( 'return ' . $result . ';' ), $short_item );
				}
			}
			unset( $results );

			switch ( $feed_item_type ) {

				case 'data':
					$output = $short_item;
					break;

				case 'node':
					$output = $this->$short_item;

					if ( $partner_id == 'pornxs' && $short_item == 'StreamRotatorContent' ) {
						$output = str_replace( 'http:', 'https:', $output );
					}

					break;

				case 'node_foreach':
					$exploded_final_node = explode( 'foreach:', $short_item );

					if ( $exploded_final_node[1] ) {

						$final_node    = $exploded_final_node[1];
						$exploded_path = explode( '/', $exploded_final_node[0] );
						$path          = $exploded_path[0];
						$output        = array();
						$elts          = (array) $this->$path;

						foreach ( (array) $elts[ $final_node ] as $elt ) {

							if ( $partner_id == 'upornia' ) {
								$elt = 'http://direct.upornia.com/get_file/0/' . $elt;
							}

							$output[] = (string) trim( $elt );

						}
					} else {

						$elts = $this->xpath( '//' . $short_item );

						foreach ( (array) $elts as $elt ) {

							$output[] = (string) trim( $elt );

						}
					}

					$output = implode( ',', (array) $output );
					break;

				case 'attributes':
					$output = $this->attributes()->$short_item;
					break;
			}
		}
		if ( ! $output ) {
			return false;
		}
		return AVUP_Item::clean_string( $output );
	}
}
