<?php
namespace BwWinnersGlobalSite;

if ( ! class_exists( __NAMESPACE__ . '\Competition_Posts' ) ) {

	class Competition_Posts {

		private $post_type = 'competition';

		public function __construct () {
			add_action( 'init', [ $this, 'register' ] );
			add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
			add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
			add_filter( 'posts_clauses', [ $this, 'competition_post_clauses' ], 10, 2 );
		}

		public function register () {

			// competition

			$labels = [
				'name' => 'Competitions',
				'singular_name' => 'Competition',
				'add_new' => 'Add New Competition',
				'add_new_item' => 'Add New Competition',
				'edit_item' => 'Edit Competition',
				'new_item' => 'New Competition',
				'all_items' => 'All Competitions',
				'view_item' => 'View Competition',
				'search_items' => 'Search Competitions',
				'not_found' =>  'No Competition Found',
				'not_found_in_trash' => 'No competition found in Trash', 
				'parent_item_colon' => '',
				'menu_name' => 'Competitions',
			];

			$args = [
				'public' => true,
				'labels' => $labels,
				'has_archive' => true,
				'show_ui' => true,
				'capability_type' => 'post',
				'hierarchical' => false,
				'query_var' => true,
				'show_in_rest' => true,
				'menu_icon' => 'dashicons-awards',
				'supports' => [
					'title',
					'editor',
					'excerpt',
					'revisions',
					'thumbnail',
					'author',
					'page-attributes'
				]
			];

			register_post_type( $this->post_type, $args );
		}

		public function add_meta_boxes () {
			add_meta_box(
				'winners_competition_meta_box',
				esc_html__( 'competition', 'bw-winners-global-site' ),
				[ $this, 'meta_box' ],
				$this->post_type,
				'normal',
				'high'
			);
		}

		public function meta_box ( $post ) {

			wp_nonce_field( basename( __FILE__ ), 'winners_competition_nonce' );

			global $bw_winners_global_site;

			$competition = $bw_winners_global_site->entities->get_competition( $post->ID );

			if ( empty( $competition ) ) {
				return;
			}

			$type = $competition ? $competition['type'] : '';

			?>
				<p>
					<label for="winners_competition_type"><?php _e( "Competition Type", 'bw-winners-global-site' ); ?></label>
					<br />
					<input class="widefat" type="text" name="winners_competition_type" id="winners_competition_type" value="<?php echo esc_attr( $type ); ?>" size="30" />
				</p>
			<?php
		}

		public function save_post ( $post_id, $post ) {

			if ( get_post_type( $post ) !== $this->post_type ) {
				return;
			}
			
			$post_type_object = get_post_type_object( $post->post_type );

			if ( ! current_user_can( $post_type_object->cap->edit_post, $post_id ) ) {
				return;
			}

			global $bw_winners_global_site;

			// Update non metabox data here

			$competition = $bw_winners_global_site->entities->get_competition( $post->ID );

			if ( ! $competition ) {
				return;
			}

			$competition['name'] = $post->post_title;

			if ( isset( $_POST['winners_competition_nonce'] ) && wp_verify_nonce( $_POST['winners_competition_nonce'], basename( __FILE__ ) ) ) {

				// Update metabox data here

				$competition['type'] = $_POST['winners_competition_type'];
				$competition['category'] = $_POST['winners_competition_category'];
			}

			$bw_winners_global_site->entities->update_competition( $competition );
		}
		
		public function competition_post_clauses ( $clauses, $wp_query ) {
			global $wpdb;

			$competition_posts_name = $wpdb->prefix . 'bw_winners_v2_competition_posts';

			$competition_id = $wp_query->get('competition_id');

			if ($competition_id) {
				$clauses['join'] .= " INNER JOIN {$competition_posts_name} AS competition_posts ON {$wpdb->posts}.ID = competition_posts.post_id ";
				$clauses['where'] .= $wpdb->prepare( " AND competition_posts.competition_id = %d ", $competition_id );
			}

			return $clauses;
		}
	}
}
