<?php
/**
 * @copyright	Copyright (c) 2016 AmyTheme (http://www.amytheme.com). All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

defined( 'ABSPATH' ) or die;

// ===============================================================================================
// -----------------------------------------------------------------------------------------------
// METABOX OPTIONS
// -----------------------------------------------------------------------------------------------
// ===============================================================================================
$options	= array();

$sidebars = array(
	'left'    => get_template_directory_uri() . '/images/sidebars/sidebar_left.png',
	'right'   => get_template_directory_uri() . '/images/sidebars/sidebar_right.png',
	'full'    => get_template_directory_uri() . '/images/sidebars/sidebar_full.png',
	'fluid'   => get_template_directory_uri() . '/images/sidebars/sidebar_fluid.png',
);

global $wp_registered_sidebars;
$sidebar_widgets = array();

if ( ! empty( $wp_registered_sidebars ) ) {
	foreach ( $wp_registered_sidebars as $key => $value ) {
		$sidebar_widgets[ $key ] = $value['name'];
	}
}

// -----------------------------------------------------------------------------------------------
// MOVIE DETAILS
// -----------------------------------------------------------------------------------------------
$options[] = array(
	'id'		=> '_movie_block_details',
	'title'		=> esc_html__( 'Movie Details', 'amy-movie' ),
	'post_type'	=> 'amy_movie',
	'context'   => 'normal',
	'priority'  => 'default',
	'sections'	=> array(
		array(
			'name'		=> 'movie_details',
			'fields'	=> array(
				array(
					'id'    => 'movie_release',
					'type'  => 'datepicker',
					'title' => esc_html__( 'Release', 'amy-movie' ),
				),

				array(
					'id'	=> 'movie_duration',
					'type'	=> 'text',
					'title'	=> esc_html__( 'Duration', 'amy-movie' ),
					'after'	=> '<em>min</em>',
				),

				array(
					'id'	=> 'movie_imdb',
					'type'	=> 'text',
					'title'	=> esc_html__( 'Imdb rating', 'amy-movie' ),
				),

				array(
					'id'	=> 'movie_mpaa',
					'type'	=> 'text',
					'title'	=> esc_html__( 'MPAA', 'amy-movie' ),
				),

				array(
					'id'	=> 'movie_language',
					'type'	=> 'text',
					'title'	=> esc_html__( 'Language', 'amy-movie' ),
				),

				array(
					'id'    => 'movie_gallery',
					'type'  => 'gallery',
					'title' => esc_html__( 'Gallery', 'amy-movie' ),
				),

				array(
					'id'              	=> 'movie_tralier',
					'type'            	=> 'group',
					'button_title'    	=> esc_html__( 'Add new tralier', 'amy-movie' ),
					'accordion_title' 	=> esc_html__( 'Tralier', 'amy-movie' ),
					'title'				=> esc_html__( 'Tralier', 'amy-movie' ),
					'fields'			=> array(
						array(
							'id'		=> 'movie_tralier_link',
							'type'		=> 'text',
							'title'		=> esc_html__( 'Tralier link', 'amy-movie' ),
							'desc'		=> esc_html__( 'Support youtube or vimeo', 'amy-movie' ),
							'info'		=> esc_html__( 'Youtube link: https://www.youtube.com/watch?v=, Vimeo link: https://vimeo.com/', 'amy-movie' ),
						),
					),
				),
			),
		),
	),
);

// -----------------------------------------------------------------------------------------------
// MOVIE CINEMA
// -----------------------------------------------------------------------------------------------
$cinema_opts 	= array();
$cinema_list	= amy_movie_get_cinema();

if ( ! empty( $cinema_list ) ) {
	foreach ( $cinema_list as $cinema ) {
		$cinema_opts[ $cinema->ID ] = $cinema->post_title;
	}
}

if ( amy_get_option( 'enable_m_cinema', true ) == true ) {
	$options[] = array(
		'id' => '_movie_block_cinema',
		'title' => esc_html__( 'Cinema Events', 'amy-movie' ),
		'post_type' => 'amy_movie',
		'context' => 'normal',
		'priority' => 'default',
		'sections' => array(
			array(
				'name' => 'movie_cinema',
				'fields' => array(
					array(
						'id' => 'amy_movie_cinema',
						'type' => 'group',
						'name' => esc_html__( 'Add Cinema', 'amy-movie' ),
						'button_title' => esc_html__( 'Add new Cinema', 'amy-movie' ),
						'accordion_title' => esc_html__( 'Cinema', 'amy-movie' ),
						'fields' => array(
							array(
								'id' => 'select_cinema',
								'type' => 'select',
								'title' => esc_html__( 'Cinema', 'amy-movie' ),
								'options' => $cinema_opts,
							),

							array(
								'id' => 'cinema_date_1',
								'type' => 'datepicker',
								'title' => esc_html__( 'Date 1', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_hour_1',
								'type' => 'text',
								'class' => 'amy-tagsinput',
								'title' => esc_html__( 'Time in date 1', 'amy-movie' ),
								'after' => esc_html__( 'List hour in date. Eg: 12h30, 20h24', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_date_2',
								'type' => 'datepicker',
								'title' => esc_html__( 'Date 2', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_hour_2',
								'type' => 'text',
								'class' => 'amy-tagsinput',
								'title' => esc_html__( 'Time in date 2', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_date_3',
								'type' => 'datepicker',
								'title' => esc_html__( 'Date 3', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_hour_3',
								'type' => 'text',
								'class' => 'amy-tagsinput',
								'title' => esc_html__( 'Time in date 3', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_date_4',
								'type' => 'datepicker',
								'title' => esc_html__( 'Date 4', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_hour_4',
								'type' => 'text',
								'class' => 'amy-tagsinput',
								'title' => esc_html__( 'Time in date 4', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_date_5',
								'type' => 'datepicker',
								'title' => esc_html__( 'Date 5', 'amy-movie' ),
							),

							array(
								'id' => 'cinema_hour_5',
								'type' => 'text',
								'class' => 'amy-tagsinput',
								'title' => esc_html__( 'Time in date 5', 'amy-movie' ),
							),
						),
					),
				),
			),
		),
	);
}

// -----------------------------------------------------------------------------------------------
// MOVIE RATE
// -----------------------------------------------------------------------------------------------
$m_turn 	= 0;
$m_point 	= 0;
$m_ave 		= 0;

if ( isset( $_GET['post'] ) ) {
	$postid 	= $_GET['post'];
	$m_rate		= amy_movie_get_rating( $postid );

	if ( isset( $m_rate['movie_rating_turn'] ) ) {
		$m_turn = $m_rate['movie_rating_turn'];
	}

	if ( isset( $m_rate['movie_rating_points'] ) ) {
		$m_point = $m_rate['movie_rating_points'];
	}
	if ( $m_point != 0 && $m_turn != 0 ) {
		$m_ave	= round( $m_point / $m_turn, 1 );
	}
}

$rate_note = 'Now: ' . $m_turn . ' turn rate, ' . $m_point . ' total points, average: ' . $m_ave . '';

$options[] = array(
	'id'		=> '_movie_block_rating',
	'title'		=> esc_html__( 'Movie Rating', 'amy-movie' ),
	'post_type'	=> 'amy_movie',
	'context'	=> 'normal',
	'priority'  => 'default',
	'sections'	=> array(
		array(
			'name'		=> 'movie_rating',
			'fields'	=> array(
			    array(
					'type'    => 'notice',
					'class'   => 'success',
					'content' => $rate_note,
				),

				array(
					'id'		=> 'custom_rating',
					'type'		=> 'switcher',
					'default'	=> false,
					'title'		=> esc_html__( 'Custom rating', 'amy-movie' ),
				),

				array(
					'id'			=> 'movie_rating_turn',
					'type'			=> 'text',
					'title'			=> esc_html__( 'Turn rating', 'amy-movie' ),
					'dependency'	=> array( 'custom_rating', '==', true ),
					'default'		=> $m_turn,
				),

				array(
					'id'			=> 'movie_rating_points',
					'type'			=> 'text',
					'title'			=> esc_html__( 'Total Points', 'amy-movie' ),
					'dependency'	=> array( 'custom_rating', '==', true ),
					'default'		=> $m_point,
				),
			),
		),
	),
);

// -----------------------------------------------------------------------------------------------
// MOVIE SIDEBAR
// -----------------------------------------------------------------------------------------------
$options[]  = array(
	'id'        => '_movie_banner',
	'title'     => esc_html__( 'Movie Banner', 'amy-movie' ),
	'post_type' => 'amy_movie',
	'context'   => 'side',
	'priority'  => 'low',
	'sections'  => array(
		array(
			'name'			=> '_movie_banner',
			'fields'      	=> array(
				array(
					'id'		=> 'movie_banner',
					'type'      => 'image',
				),
			),
		),

	),
);

$options[]  = array(
	'id'        => '_movie_poster',
	'title'     => esc_html__( 'Movie Poster', 'amy-movie' ),
	'post_type' => 'amy_movie',
	'context'   => 'side',
	'priority'  => 'low',
	'sections'  => array(
		array(
			'name'			=> '_movie_poster',
			'fields'      => array(
				array(
					'id'        => 'movie_poster',
					'type'      => 'image',
				),
			),
		),

	),
);

$options[]  = array(
	'id'        => '_movie_sidebar',
	'title'     => esc_html__( 'Movie Sidebar', 'amy-movie' ),
	'post_type' => 'amy_movie',
	'context'   => 'side',
	'priority'  => 'low',
	'sections'  => array(
		array(
			'title'       => esc_html__( 'Movie Sidebar', 'amy-movie' ),
			'name'			=> '_movie_sidebar',
			'fields'      => array(
				array(
					'id'          => 'movie_sidebar',
					'type'        => 'image_select',
					'radio'       => true,
					'options'     => array(
						'left'    => get_template_directory_uri() . '/images/sidebars/sidebar_left.png',
						'right'   => get_template_directory_uri() . '/images/sidebars/sidebar_right.png',
						'full'    => get_template_directory_uri() . '/images/sidebars/sidebar_full.png',
					),
					'default'     => 'right',
					'attributes'	=> array(
						'data-depend-id'	=> '_movie_sidebar',
					),
				),

				array(
					'id'              => 'movie_sidebar_widget',
					'type'            => 'select',
					'options'         => array_reverse( $sidebar_widgets ),
					'default_option'  => esc_html__( 'Choose a custom sidebar', 'amy-movie' ),
					'dependency'      => array( '_movie_sidebar', 'any', 'right,left' ),
				),
			),
		),

	),
);
// -----------------------------------------------------------------------------------------------
// CINEMA OPTIONS
// -----------------------------------------------------------------------------------------------
$options[] = array(
	'id'		=> '_cinema_block_options',
	'title'		=> esc_html__( 'Cinema Details', 'amy-movie' ),
	'post_type'	=> 'amy_cinema',
	'context'   => 'normal',
	'priority'  => 'default',
	'sections'	=> array(
		array(
			'name'			=> 'cinema_details',
			'fields'      => array(
				array(
					'id'    => 'cinema_banner',
					'type'  => 'image',
					'title' => esc_html__( 'Banner', 'amy-movie' ),
				),

				array(
					'id'    => 'cinema_gallery',
					'type'  => 'gallery',
					'title' => esc_html__( 'Gallery', 'amy-movie' ),
				),

				array(
					'id'    => 'cinema_address',
					'type'  => 'text',
					'title' => esc_html__( 'Address', 'amy-movie' ),
				),

				array(
					'id'    => 'cinema_phone',
					'type'  => 'text',
					'title' => esc_html__( 'Phone', 'amy-movie' ),
				),

				array(
					'id'    => 'cinema_website',
					'type'  => 'text',
					'title' => esc_html__( 'Website', 'amy-movie' ),
				),

				array(
					'id'    => 'cinema_email',
					'type'  => 'text',
					'title' => esc_html__( 'Email', 'amy-movie' ),
				),

				array(
					'id'    	=> 'cinema_map',
					'type'  	=> 'textarea',
					'title' 	=> esc_html__( 'Google Map Iframe', 'amy-movie' ),
					'sanitize' 	=> true,
					'after'		=> esc_html__( 'Visit Google maps to create your map (Step by step: 1) Find location 2) Click the cog symbol in the lower right corner and select "Share or embed map" 3) On modal window select "Embed map" 4) Copy iframe code and paste it).', 'amy-movie' ),
				),
			),
		),
	),
);


// -----------------------------------------------------------------------------------------------
// PAGE OPTIONS
// -----------------------------------------------------------------------------------------------
$options[]  = array(
	'id'        => '_sidebar',
	'title'     => esc_html__( 'Page Sidebar Model', 'amy-movie' ),
	'post_type' => 'page',
	'context'   => 'side',
	'priority'  => 'low',
	'sections'  => array(
		array(
			'name'			=> '_page_sidebar',
			'title'       => esc_html__( 'Page Sidebar', 'amy-movie' ),
			'fields'      => array(
				array(
					'id'          => 'page_sidebar',
					'type'        => 'image_select',
					'radio'       => true,
					'options'     => $sidebars,
					'default'     => array( 'full' ),
					'attributes'  => array(
						'data-depend-id'  => 'page_sidebars',
					),
				),

				array(
					'id'              => 'page_sidebar_widget',
					'type'            => 'select',
					'options'         => array_reverse( $sidebar_widgets ),
					'default_option'	=> esc_html__( 'Choose a custom sidebar', 'amy-movie' ),
					'dependency'		=> array( 'page_sidebars', 'any', 'right,left' ),
				),
			),
		),

	),
);

$options[] = array(
	'id'			=> '_custom_page_options',
	'title'			=> esc_html__( 'Page Options', 'amy-movie' ),
	'post_type'		=> 'page',
	'context'		=> 'normal',
	'priority'		=> 'default',
	'sections'		=> array(
		array(
			'name'		=> 'page_options',
			'fields'	=> array(
				array(
					'id'		=> 'enable_page_title',
					'type'		=> 'switcher',
					'title'		=> esc_html__( 'Enable Page Title', 'amy-movie' ),
					'default'	=> true,
				),

				array(
					'id'		=> 'title_position',
					'type'		=> 'select',
					'title'		=> esc_html__( 'Title Position', 'amy-movie' ),
					'options'	=> array(
						'center'	=> esc_html__( 'Center', 'amy-movie' ),
						'left'		=> esc_html__( 'Left', 'amy-movie' ),
						'right'		=> esc_html__( 'Right', 'amy-movie' ),
					),
					'default'	=> 'left',
					'dependency'	=> array( 'enable_page_title', '==', true ),
				),

				array(
					'id'		=> 'title_background_image',
					'type'		=> 'image',
					'title'		=> esc_html__( 'Title Background Image', 'amy-movie' ),
					'dependency'	=> array( 'enable_page_title', '==', true ),
				),

				array(
					'id'		=> 'title_background',
					'type'		=> 'color_picker',
					'title'		=> esc_html__( 'Title Background', 'amy-movie' ),
					'dependency'	=> array( 'enable_page_title', '==', true ),
				),

				array(
					'id'		=> 'tile_color',
					'type'		=> 'color_picker',
					'title'		=> esc_html__( 'Title Color', 'amy-movie' ),
					'dependency'	=> array( 'enable_page_title', '==', true ),
				),
			),
		),
	),
);

AmyFramework_Metabox::instance( $options );
