<?php

class AVUP_Item {

	public static function clean_string( $string ) {
		$string = preg_replace( '/u([\da-fA-F]{4})/', '&#x\1;', $string );
		return (string) stripslashes( trim( str_replace( array( '<p>', '</p>' ), '', AVUP\ForceUTF8\Encoding::UTF8FixWin1252Chars( html_entity_decode( $string, ENT_HTML5 ) ) ) ) );
	}

	public static function get_id( $item ) {
		return $item['id'];
	}

	public static function is_valid( $item ) {
		return ( $item['code'] || $item['video_url'] )
			  && $item['id'] != ''
			  && $item['title'] != '';
	}

	public static function get_data_for_js( $item ) {
		return $item['id'];
	}

	public static function get_data_for_json( $item ) {

		if ( xbox_get_field_value( 'avup-options', 'import-title' ) == 'off' ) {
			$item['title'] = '';
		}
		if ( xbox_get_field_value( 'avup-options', 'import-description' ) == 'off' ) {
			$item['desc'] = '';
		}
		if ( xbox_get_field_value( 'avup-options', 'import-tags' ) == 'off' ) {
			$item['tags'] = '';
		}
		if ( xbox_get_field_value( 'avup-options', 'import-actors' ) == 'off' ) {
			$item['actors'] = '';
		}

		return array(
			'response'     => array(
				'code'    => 'success',
				'message' => 'This video is ready to be imported!',
			),
			'id'           => (string) $item['id'],
			'title'        => (string) $item['title'],
			'desc'         => (string) $item['desc'],
			'tags'         => (string) $item['tags'],
			'duration'     => (string) self::get_length_in_seconds( $item ),
			'thumb_url'    => (string) $item['thumb_url'],
			'thumbs_urls'  => (string) $item['thumbs_urls'],
			'trailer_url'  => (string) $item['trailer_url'],
			'video_url'    => (string) $item['video_url'],
			'tracking_url' => (string) $item['tracking_url'],
			'embed'        => (string) $item['code'],
			'actors'       => (string) $item['actors'],
			'checked'      => true,
			'grabbed'      => false,
		);
	}

	public static function get_length_in_seconds( $item ) {
		$length = $item['duration'];
		$format = $item['duration_format'];

		if ( ! ( $length && $format ) ) {
			return false;
		}

		switch ( $format ) {
			case 'mm':
				$length = (int) $length * 60;
				break;
			case 'mm,ss':
				$length = explode( ',', $length );
				$mm     = (int) $length[0] * 60;
				$ss     = (int) $length[1];
				$length = $mm + $ss;
				break;
			case 'coupe_sss':
				break;
			case 'ss':
				break;
			case 'hh:mm:ss':
				$length = explode( ':', $length );
				$hh     = (int) $length[0] * 60 * 60;
				$mm     = (int) $length[1] * 60;
				$ss     = (int) $length[2];
				$length = $hh + $mm + $ss;
				break;
			case 'mm:ss':
				$length = explode( ':', $length );
				$mm     = (int) $length[0] * 60;
				$ss     = (int) $length[1];
				$length = $mm + $ss;
				break;
			case 'mm,ss/mm.ss':
				$length = str_replace( '.', ',', $length );
				$length = explode( ',', $length );
				$mm     = (int) $length[0] * 60;
				$ss     = (int) $length[1];
				$length = $mm + $ss;
				break;
			case 'mmmsss':
				$length = explode( 'm', $length );
				$mm     = (int) $length[0] * 60;
				$ss     = (int) str_replace( 's', '', $length[1] );
				$length = $mm + $ss;
				break;
			case 'min sec':
				$length = explode( 'min ', $length );
				$mm     = (int) $length[0] * 60;
				$ss     = (int) str_replace( 'sec', '', $length[1] );
				$length = $mm + $ss;
				break;
			case 'mmm:sss':
				$length = str_replace( 'm', '', $length );
				$length = str_replace( 's', '', $length );
				$length = explode( ':', $length );
				$mm     = (int) $length[0] * 60;
				$ss     = (int) $length[1];
				$length = $mm + $ss;
				break;
			case 'THMS':
				preg_match_all( '/[0-9]+/', $length, $matches );
				$matches = $matches[0];
				switch ( count( $matches ) ) {
					case 1:
						$length = $matches[0];
						break;
					case 2:
						$length = $matches[0] * 60 + $matches[1];
						break;
					case 3:
						$length = $matches[0] * 24 + $matches[1] * 60 + $matches[2];
						break;
				}
				break;
			case 'xvideos':
				preg_match_all( '/[0-9]+/', $length, $matches );
				$matches = $matches[0];
				switch ( count( $matches ) ) {
					case 1:
						if ( strpos( $length, 'min' ) !== false ) {
							$length = $matches[0] * 60;
						} else {
							$length = $matches[0] * 1;
						}
						break;
					case 2:
						if ( strpos( $length, 'h' ) !== false && strpos( $length, 'min' ) !== false ) {
							$length = $matches[0] * 60 * 60 + $matches[1] * 60;
						} else {
							$length = $matches[0] * 60 + $matches[1];
						}
						break;
					case 3:
						$length = $matches[0] * 60 * 60 + $matches[1] * 60 + $matches[1];
						break;
				}
				break;
		}
		return $length;
	}
}
