<?PHP

require_once 'class.templatepage.php';
require_once 'class.youtube.php';

class RecordingPage extends TemplatePage {
	var $sorts = [
		'listens-desc' => 'recordings.listens DESC, recordings.favourites DESC', // first name
		'favourites-desc' => 'recordings.favourites DESC, recordings.listens DESC', // last name
		'added-desc' => 'recordings.added DESC', // most recent
		// 'relevance' => 'relevance DESC', // relevance
	];

	var $types = [
		'audio-video' => null,
		'video' => 'FLV',
		'audio' => [ 'IN', [ 'MP3', 'OGG' ] ],
	];

	public function __construct() {
		$this->standalone = getInt( 'standalone' );
		parent::__construct();
	}

	public function generate() {
		$this->write_menu( 'recordings' );

		$recording_id = getInt( 'recording_id' );
		$edit = getInt( 'edit' );
		$delete = getInt( 'delete' );
		$id = getInt( 'id' );
		$table = getString( 'table' );

		// Show/edit recording
		if ( $recording_id || ( $edit && !$recording_id ) ) {
			// Load recording
			$row = $this->recordingService->getById( $recording_id );
			// Check recording exists
			if ( $row || ( $edit && !$recording_id ) ) {
				$recording = new Recording( $this, $row ? $row : null );
				// Edit/delete recording
				if ( $edit || $delete ) {
					require_once 'recordings.edit.php';
				} else {
					if ( !$recording->check_redirect() ) {
						$this->redirect( "{$this->base}/{$recording->get_link()}", true );
					}

					$this->write_title( $recording->get_data( 'title', true ) );
					$this->add_breadcrumb( $recording->get_data( 'title', true ) );

					$this->render_recording_view( $recording, true );
				}
			} else {
				$this->redirect( "{$this->base}/recordings" );
			}
		} else {
			return;
			$this->check_redirect( 'recordings' );

			$this->add_breadcrumb( $this->language( 'breadcrumb-all-recording' ), 'recordings' );
			if ( getString( 'value' ) ) {
				$this->add_breadcrumb( $this->get_title() );
			}

			$this->output( '
				new UpdatePanelForm( "#recording_list", null, "#form_main" );
			', 'script', 'javascript' );

			$this->render_recording_list();
		}
	}

	public function render_recording_view( $recording = null, $fullpage = false ) {
		if ( !$recording ) {
			$recording = new Recording( $this );
			$recording->set_data( 'recording_id', getInt( 'recording_id' ) );
			if ( !$recording->reload() ) {
				if ( $fullpage ) {
					$this->redirect( "{$this->base}/recordings" );
				} else {
					return;
				}
			}
		}
		$this->output(
			'<ul class="recordingList recordingListStandalone">' .
				'<li><h2>' . $recording->get_data( 'title', true ) . '</h2></li>' .
				$recording->render_listItem( 'null', $null, true, true, !$fullpage ) .
			'</ul>'
		);
		$this->output( '$( "a" ).attr( "target", "_top" );', 'script', 'javascript' );
		if ( $fullpage && $this->session->user && $this->session->user->may_edit( $recording->get_data( 'user_id' ) ) ) {
			$this->output(
				'<p>' .
					'<a class="button" href="' . $recording->get_link() . '?edit=1">' .
						$this->button_link( $this->language( 'button-edit' ), 'pencil-alt' ) .
					'</a> ' .
					'<a class="button button_alt" href="' . $recording->get_link() . '?delete=1" ' .
						'onclick="return confirm(' . $this->escape_text( json_encode( $this->language( 'confirm-delete-recording' ) ) ) . ')">' .
						$this->button_link( $this->language( 'button-delete' ), 'trash' ) .
					'</a>' .
				'</p>'
			);
		}
	}

	function get_title( $lang = null, $tagline = false ) {
		switch ( getString( 'field' ) ) {
			case 'composer_id':
				$composer = new Composer( $this );
				$composer->set_data( 'composer_id', getInt( 'value' ) );
				$composer->reload();
				$name = $composer->get_linked_name( 'FIRST_LAST' );
				$title = $this->language( 'title-recordings-of', [ 'composer' => $name ] );
				break;
			default:
				$title = $this->escape_text( getString( 'value' ) );
				break;
		}
		return $title;
	}

	public function render_recording_list() {
		$this->output( '<div id="recording_list">' );

		if ( getString( 'value' ) ) {
			$title = $this->get_title();
			$this->write_title( $title );
			$this->output( '<h1>' . $title . '</h1>' );
		} else {
			$this->write_lang_title( 'title-recording' );
			$this->output( '<h1>' . $this->language( 'title-recording' ) . '</h1>' );
			$this->output( '<h3>Popular composers</h3><ul class="bulletList bulletListSmall performersList">' );
			$stmt = $this->db()->recordings_get_composers();
			$i = 0;
			while ( $row = $stmt->fetch_assoc() ) {
				$composer = new Composer( $this, $row );
				$this->output( '<li><a href="' . $composer->get_recording_link() . '">' . $composer->get_name( 'LAST_INITIALS' ) . '</a></li>' );
				if ( ++$i >= 20 ) {
					break;
				}
			}
			$this->output( '</ul><div class="clearLeft"></div><br />' );
			$this->output( '<h3>Popular performers</h3><ul class="bulletList bulletListSmall performersList">' );
			$performers = $this->db()->recordings_get_performers();
			$i = 0;
			foreach ( $performers as $name => $count ) {
				$this->output( '<li><a href="recordings/performers/' . urlencode_slashes( $name ) . '">' . $this->escape_text( $name ) . '</a></li>' );
				if ( ++$i >= 20 ) {
					break;
				}
			}
			$this->output( '</ul><div class="clearLeft"></div><br />' );
		}

		$this->output( '<div id="sideGa">' . $this->get_google_ad( 'SkyscraperWide' ) . '</div><div class="besideGaContent">' );
		# if($this->session->user and $this->session->user->get_data('admin'))
		# $this->output('<p><a class="button" href="recordings/?edit=1">'.$this->button_link($this->language('button-new-recording'), 'plus').'</a></p>');

		$form = new Form( $this, null, null, 'get' );
		$field_sort = new Field( 'select', 'sort', 'listens-desc', $this->language( 'label-music-sort' ), $this->language( 'form-ins-music-sort' ) );
		foreach ( $this->sorts as $name => $sql ) {
			$field_sort->add_option( $name, $this->language( 'label-music-sort-' . $name ) );
		}
		$form->add_field( $field_sort );

		$field_type = new Field( 'select', 'type', 'audio-video', $this->language( 'label-recording-filter-type' ), $this->language( 'form-ins-recording-filter-type' ) );
		foreach ( $this->types as $name => $sql ) {
			$field_type->add_option( $name, $this->language( 'label-recording-filter-type-' . $name ) );
		}
		$form->add_field( $field_type );

		$field = new Field( 'hidden', 'page', '1' );
		$field->persistent = false;
		$form->add_field( $field );
		if ( $form->isPostBack() ) {
			$form->get_data();
		}
		$form->render();

		$this->output( '
			$( ' . json_encode( '#' . $form->get_field_id( 'sort' ) . ',#' . $form->get_field_id( 'type' ) ) . ' )
				.on( "change", $( ' . json_encode( '#' . $form->id ) . ' )[0].submit.bind( $( ' . json_encode( '#' . $form->id ) . ' )[0] ) );
		', 'script', 'javascript' );

		$per_page = 10;
		$page = max( getInt( 'page', 1 ), 1 );

		if ( !empty( $this->sorts[getString( 'sort' )] ) ) {
			$sort = $this->sorts[getString( 'sort' )];
		} else {
			$sort = $this->sorts['listens-desc'];
		}

		$filters = [];

		if ( getString( 'field' ) == 'performers' ) {
			$filters['performers'] = [ 'like', '%' . getString( 'value' ) . '%' ];
		}

		if ( !empty( $this->types[getString( 'type' )] ) ) {
			$filters['recordings.type'] = $this->types[getString( 'type' )];
		} else {
			$filters['recordings.type'] = $this->types['audio-video'];
		}

		$recording = new Recording( $this );
		if ( getString( 'field' ) == 'composer_id' ) {
			$composer = new Composer( $this );
			$composer->set_data( 'composer_id', getInt( 'value' ) );
			$stmt = $composer->get_recordings( $filters, $sort, $per_page, $page );
		} else {
			$stmt = $recording->get( $filters, $sort, $per_page, $page );
		}

		$form = new Form( $this, null, null, 'get' );
		$paging = new Paging( $this, $form, false, $stmt->found_rows, $per_page, $page );

		if ( $stmt->found_rows ) {
			$this->output( 'var playList = new Cantorion.PlayList();', 'script', 'javascript' );
			$paging->render( 'top' );
			$id = "group_" . md5( rand() . rand() );
			$this->output( '<ul class="recordingList recordingListLarge" id="' . $id . '">' );
			$translated = false;
			$output = '';
			while ( $row = $stmt->fetch_assoc() ) {
				$recording = new Recording( $this, $row );
				$output .= $recording->render_listItem( 'playList', $translated, true );
			}
			if ( $translated ) {
				$this->output( '<li>' . $this->translation_links( $id, false, 'top' ) . '</li>' );
			}
			$this->output( $output );
			$this->output( '</ul>' );
			$paging->render( 'bottom' );
		}
		$stmt->close();
		$this->output( '</div></div>' );
	}
}
new RecordingPage();
