<?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;

/**
 * After setup theme
 */
if ( ! function_exists( 'amy_movie_action_after_theme_setup' ) ) {
	function amy_movie_action_after_theme_setup() {
		global $content_width;

		if ( ! isset( $content_width ) ) {
			$content_width	= 1170;
		}

		// Make theme available for translation.
		load_theme_textdomain( 'amy-movie', get_template_directory() . '/languages' );

		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'post-formats', array( 'image', 'video' ) );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'title-tag' );
		add_theme_support( 'custom-background' );
		add_theme_support( 'custom-header' );
		add_theme_support( 'editor_style' );

		add_theme_support( 'widgets' );
		add_theme_support( 'menus' );

		add_editor_style();

		remove_theme_support( 'custom-header' );

		add_theme_support('html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		));

		add_theme_support( 'microformats2' );
		add_theme_support( 'microformats' );
		add_theme_support( 'microdata' );

		// custom image sizes
		$custom_image_sizes	= amy_get_option( 'custom_image_sizes' );

		if ( ! empty( $custom_image_sizes ) ) {
			foreach ( $custom_image_sizes as $size ) {
				if ( $size['img_crop'] ) {
					$crop	= array( 'left', 'top' );
				}

				add_image_size( sanitize_title( $size['img_size_name'] ), $size['img_size_width'], $size['img_size_height'], $crop );
			}
		}

		// default image sizes
		$defaults_image_size = amy_movie_default_image_size();

		if ( ! empty( $defaults_image_size ) ) {
			foreach ( $defaults_image_size as $size ) {
				if ( $size['img_crop'] ) {
					$crop	= array( 'left', 'top' );
				}

				add_image_size( sanitize_title( $size['img_size_name'] ), $size['img_size_width'], $size['img_size_height'], $crop );
			}
		}
	}

	add_action( 'after_setup_theme', 'amy_movie_action_after_theme_setup' );
}

/**
 * Add admin script.
 */
if ( ! function_exists( 'amy_movie_action_admin_enqueue_scripts' ) ) {
	function amy_movie_action_admin_enqueue_scripts() {
		wp_enqueue_style( 'jquery-ui-datepicker', 	get_template_directory_uri() . '/css/admin-style.css' );
		wp_enqueue_style( 'fancybox', 				get_template_directory_uri() . '/css/vendor/jquery.fancybox.css', array() );

		wp_enqueue_script( 'field-date-js', 'Field_Date.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker' ), time(), true );
		wp_enqueue_script( 'tagsinput', 			get_template_directory_uri() . '/js/jquery.tagsinput.js', array( 'jquery' ), '1.3.3', true );
		wp_enqueue_script( 'amy-admin-script', 	get_template_directory_uri() . '/js/admin-script.js', array( 'jquery' ), '1.0.0', true );
		wp_enqueue_script( 'fancybox', 			get_template_directory_uri() . '/js/vendor/jquery.fancybox.js', array( 'jquery' ), '2.1.5', true );

		wp_localize_script('amy-admin-script', 'amy_script', array(
			'ajax_url'		=> admin_url( 'admin-ajax.php' ),
		));
	}

	add_action( 'admin_print_scripts-post.php', 'amy_movie_action_admin_enqueue_scripts', 99 );
	add_action( 'admin_print_scripts-post-new.php', 'amy_movie_action_admin_enqueue_scripts', 99 );
	add_action( 'admin_enqueue_scripts', 'amy_movie_action_admin_enqueue_scripts', 99 );
}

/*
 * Ajax Rating
 */

if ( ! function_exists( 'amy_movie_ajax_rate' ) ) {
	function amy_movie_ajax_rate() {
		$point 		= $_REQUEST['point'];
		$post_id	= $_REQUEST['post_id'];

		$now_rate	= get_post_meta( $post_id, '_movie_block_rating' );
		$new_rate	= array();
		$ip_arr		= array();

		$ip_new 	= $_SERVER['REMOTE_ADDR'];
		$ip_arr[]	= $ip_new;
		$ip_old		= get_post_meta( $post_id, '_movie_block_rating_ip' );

		if ( $ip_old ) {
			if ( in_array( $ip_new, $ip_old[0] ) == true ) {
				echo -1;
				exit;
			} else {
				foreach ( $ip_old[0] as $ip ) {
					$ip_arr[] = $ip;
				}

				update_post_meta( $post_id, '_movie_block_rating_ip', $ip_arr );
			}
		} else {
			add_post_meta( $post_id, '_movie_block_rating_ip', $ip_arr );
		}

		if ( $now_rate ) {
			$now_turn	= $now_rate[0]['movie_rating_turn'];
			$now_point	= $now_rate[0]['movie_rating_points'];

			$new_rate['movie_rating_turn']		= $now_turn + 1;
			$new_rate['movie_rating_points']	= $now_point + $point;
			$new_rate['custom_rating']			= '0';

			update_post_meta( $post_id, '_movie_block_rating', $new_rate );
		} else {
			$new_rate['movie_rating_turn']		= 1;
			$new_rate['movie_rating_points']	= $point;
			$new_rate['custom_rating']			= '0';

			add_post_meta( $post_id, '_movie_block_rating', $new_rate );
		}

		$new_ave = $new_rate['movie_rating_points'] / $new_rate['movie_rating_turn'];

		if ( get_post_meta( $post_id, '_amy_movie_rate' ) ) {
			update_post_meta( $post_id, '_amy_movie_rate', round( $new_ave, 1 ) );
		} else {
			add_post_meta( $post_id, '_amy_movie_rate', round( $new_ave, 1 ) );
		}

		echo 1;
		exit;
	}

	add_action( 'wp_ajax_amy_movie_ajax_rate', 'amy_movie_ajax_rate' );
	add_action( 'wp_ajax_nopriv_amy_movie_ajax_rate', 'amy_movie_ajax_rate' );
}

/*
 *	Check Post View
 */
if ( ! function_exists( 'amy_movie_set_post_views' ) ) {
	function amy_movie_set_post_views( $postID ) {
		$count_key 	= '_amy_post_views_count';
		$count 		= get_post_meta( $postID, $count_key, true );

		if ( $count == '' ) {
			$count = 0;
			delete_post_meta( $postID, $count_key );
			add_post_meta( $postID, $count_key, '0' );
		} else {
			$count++;
			update_post_meta( $postID, $count_key, $count );
		}
	}

	// To keep the count accurate, lets get rid of prefetching
	remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
}

/*
 * Track Post View
 */
if ( ! function_exists( 'amy_movie_track_post_views' ) ) {
	function amy_movie_track_post_views( $post_id ) {
		if ( ! is_single() ) {
			return;
		}

		if ( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}

		amy_movie_set_post_views( $post_id );
	}

	add_action( 'wp_head', 'amy_movie_track_post_views' );

}

if ( ! function_exists( 'amy_movie_insert_fb_tag_in_head' ) ) {
	function amy_movie_insert_fb_tag_in_head() {
		global $post;

		if ( ! is_singular() ) {
			return;
		}

		echo '<meta property="og:title" content="' . get_the_title() . '"/>';
		echo '<meta property="og:type" content="article"/>';
		echo '<meta property="og:description" content="' . wp_strip_all_tags( amy_movie_get_excerpt_by_id( $post->ID, 30 ) ) . '"/>';
		echo '<meta property="og:url" content="' . get_permalink() . '"/>';

		echo '<meta name="twitter:card" content="summary" />';
		echo '<meta name="twitter:description" content="' . wp_strip_all_tags( amy_movie_get_excerpt_by_id( $post->ID, 30 ) ) . '" />';
		echo '<meta name="twitter:title" content="' . get_the_title() . '" />';

		if ( has_post_thumbnail( $post->ID ) ) {
			$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
			echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
			echo '<meta name="twitter:image" content="' . esc_attr( $thumbnail_src[0] ) . '" />';
		}

		echo '';
	}

	add_action( 'wp_head', 'amy_movie_insert_fb_tag_in_head', 5 );
}

if ( ! function_exists( 'amy_movie_ajax_filter' ) ) {
	function amy_movie_ajax_filter() {
		$genreid 	= $_REQUEST['genreid'];
		$cinemaid	= $_REQUEST['cinemaid'];
		$release	= $_REQUEST['release'];
		$data_send	= $_REQUEST['data_send'];
		$sortby		= $_REQUEST['sortby'];

		$data_send	= json_decode( base64_decode( $data_send ) );

		$img_size	= $data_send->msize;
		$mdate		= $data_send->mdate;
		$morderby	= $data_send->morderby;
		$morder		= $data_send->morder;
		$perpage	= $data_send->perpage;
		$type		= $data_send->type;
		$pagination = $data_send->pagination;
		$c_url		= $data_send->current_url;

		$paged 		= (get_query_var( 'paged' )) ? intval( get_query_var( 'paged' ) ) : intval( get_query_var( 'page' ) );
		$params 	= array();

		$params['movie_date']		= $mdate;
		$params['movie_orderby']	= $morderby;
		$params['movie_order']		= $morder;
		$params['movie_per_page']	= $perpage;
		$params['paged']			= $paged;
		$params['sortby']			= $sortby;

		if ( $cinemaid != 0 && $cinemaid != 'undefined' ) {
			$params['movie_cinema'] = $cinemaid;
		}

		if ( $release != null && $release != 'undefined' ) {
			$params['movie_release'] = $release;
		}

		if ( $genreid != 0 && $genreid != 'undefined' ) {
			$params['movie_genre']	 	= $genreid;
		}

		$arpg 		= amy_movie_query( $params );
		$the_query 	= new WP_Query( $arpg );
		$max   		= intval( $the_query->max_num_pages );
		$data		= $the_query->posts;

		if ( $type == 'list' ) {
			$options = array(
				'img_size'		=> explode( ',', $img_size ),
				'max_pages'		=> $max,
				'pagination'	=> $pagination,
				'time'			=> $release,
				'current_url'	=> $c_url,
			);

			$json = amy_movie_listlayout( $data, $options );
		} else if ( $type == 'grid' ) {
			$options	 = array(
				'img_size'		=> explode( ',', $img_size ),
				'layout'		=> $data_send->layout,
				'column'		=> $data_send->column,
				'max_pages'		=> $max,
				'pagination'	=> $pagination,
				'current_url'	=> $c_url,
			);

			$json = amy_movie_gridlayout( $data, $options );
		}

		if ( $json == '' ) {
			$json = esc_html__( 'No movie', 'amy-movie' );
		}

		echo json_encode( $json );
		exit;
	}

	add_action( 'wp_ajax_amy_movie_ajax_filter', 'amy_movie_ajax_filter' );
	add_action( 'wp_ajax_nopriv_amy_movie_ajax_filter', 'amy_movie_ajax_filter' );
}

if ( ! function_exists( 'amy_movie_ajax_showtime' ) ) {
	function amy_movie_ajax_showtime() {
		$cinema_id		= $_REQUEST['cinema_id'];
		$movie_id		= $_REQUEST['movie_id'];
		$action_type	= $_REQUEST['action_type'];
		$st_type		= $_REQUEST['st_type'];

		if ( $action_type == 'shortcode' ) {
			$type = $st_type;
		} else {
			$type		= amy_get_option( 'movie_showtime_type' );
		}

		$content = amy_movie_get_showtime( $movie_id, $cinema_id, $type, '' );

		if ( $content == '' ) {
			$content = esc_html__( 'No Time', 'amy-movie' );
		}

		echo json_encode( $content );
		exit;
	}

	add_action( 'wp_ajax_amy_movie_ajax_showtime', 'amy_movie_ajax_showtime' );
	add_action( 'wp_ajax_nopriv_amy_movie_ajax_showtime', 'amy_movie_ajax_showtime' );
}

if ( ! function_exists( 'amy_movie_ajax_change_date_format' ) ) {
	function amy_movie_ajax_change_date_format() {
		$date	= $_REQUEST['date'];

		if ( $date != '' ) {
			$newdate	= date_i18n( get_option( 'date_format' ), strtotime( $date ) );
		} else {
			$newdate	= '';
		}

		echo json_encode( $newdate );
		exit;
	}

	add_action( 'wp_ajax_amy_movie_ajax_change_date_format', 'amy_movie_ajax_change_date_format' );
	add_action( 'wp_ajax_nopriv_amy_movie_ajax_change_date_format', 'amy_movie_ajax_change_date_format' );
}
