<?php
defined( 'ABSPATH' ) || exit;

/**
 * Admin: slide meta fields.
 */
class Awp_Slider_Admin {

	/**
	 * @var Awp_Slider_Admin|null
	 */
	private static $instance = null;

	/**
	 * @return Awp_Slider_Admin
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Hooks.
	 */
	public function init() {
		add_action( 'add_meta_boxes', array( $this, 'add_slider_meta_boxes' ) );
		add_action( 'save_post_awp-slider', array( $this, 'save_slider_meta_box_data' ) );
		add_filter( 'manage_awp-slider_posts_columns', array( $this, 'columns' ) );
		add_action( 'manage_awp-slider_posts_custom_column', array( $this, 'column_content' ), 10, 2 );
	}

	/**
	 * Meta boxes.
	 */
	public function add_slider_meta_boxes() {
		add_meta_box(
			'awp-slide-details',
			__( 'Slide Details', 'advance-wp-slider' ),
			array( $this, 'render_details_meta_box' ),
			'awp-slider',
			'normal',
			'high'
		);
	}

	/**
	 * Render details fields.
	 *
	 * @param WP_Post $post Post.
	 */
	public function render_details_meta_box( $post ) {
		wp_nonce_field( 'awp_slider_meta_nonce_action', 'awp_slider_meta_nonce' );

		$subtitle    = get_post_meta( $post->ID, 'slider_subtitle', true );
		$button_text = get_post_meta( $post->ID, 'slider_button_text', true );
		$button_link = get_post_meta( $post->ID, 'slider_button_link', true );
		?>
		<p style="margin-bottom:12px;">
			<?php esc_html_e( 'Use the featured image as the slide background. Title and editor content appear as heading and description.', 'advance-wp-slider' ); ?>
		</p>
		<table class="form-table" role="presentation">
			<tr>
				<th><label for="slider_subtitle"><?php esc_html_e( 'Subtitle', 'advance-wp-slider' ); ?></label></th>
				<td>
					<input type="text" class="widefat" id="slider_subtitle" name="slider_subtitle" value="<?php echo esc_attr( $subtitle ); ?>" />
					<p class="description"><?php esc_html_e( 'Optional small label above the title.', 'advance-wp-slider' ); ?></p>
				</td>
			</tr>
			<tr>
				<th><label for="slider_button_text"><?php esc_html_e( 'Button text', 'advance-wp-slider' ); ?></label></th>
				<td>
					<input type="text" class="widefat" id="slider_button_text" name="slider_button_text" value="<?php echo esc_attr( $button_text ); ?>" />
				</td>
			</tr>
			<tr>
				<th><label for="slider_button_link"><?php esc_html_e( 'Button link', 'advance-wp-slider' ); ?></label></th>
				<td>
					<input type="url" class="widefat" id="slider_button_link" name="slider_button_link" value="<?php echo esc_attr( $button_link ); ?>" placeholder="https://" />
				</td>
			</tr>
		</table>
		<?php
	}

	/**
	 * Save meta.
	 *
	 * @param int $post_id Post ID.
	 */
	public function save_slider_meta_box_data( $post_id ) {
		if ( ! isset( $_POST['awp_slider_meta_nonce'] ) ||
			! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['awp_slider_meta_nonce'] ) ), 'awp_slider_meta_nonce_action' ) ) {
			return;
		}

		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		if ( wp_is_post_revision( $post_id ) ) {
			return;
		}

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		if ( isset( $_POST['slider_subtitle'] ) ) {
			update_post_meta(
				$post_id,
				'slider_subtitle',
				sanitize_text_field( wp_unslash( $_POST['slider_subtitle'] ) )
			);
		}

		if ( isset( $_POST['slider_button_text'] ) ) {
			update_post_meta(
				$post_id,
				'slider_button_text',
				sanitize_text_field( wp_unslash( $_POST['slider_button_text'] ) )
			);
		}

		if ( isset( $_POST['slider_button_link'] ) ) {
			update_post_meta(
				$post_id,
				'slider_button_link',
				esc_url_raw( wp_unslash( $_POST['slider_button_link'] ) )
			);
		}
	}

	/**
	 * Admin list columns.
	 *
	 * @param array $columns Columns.
	 * @return array
	 */
	public function columns( $columns ) {
		$new = array();
		foreach ( $columns as $key => $label ) {
			$new[ $key ] = $label;
			if ( 'title' === $key ) {
				$new['awp_image']  = __( 'Image', 'advance-wp-slider' );
				$new['awp_button'] = __( 'Button', 'advance-wp-slider' );
			}
		}
		return $new;
	}

	/**
	 * Column content.
	 *
	 * @param string $column Column key.
	 * @param int    $post_id Post ID.
	 */
	public function column_content( $column, $post_id ) {
		if ( 'awp_image' === $column ) {
			$thumb = get_the_post_thumbnail( $post_id, array( 60, 40 ) );
			echo $thumb ? wp_kses_post( $thumb ) : '&mdash;';
		}
		if ( 'awp_button' === $column ) {
			$text = get_post_meta( $post_id, 'slider_button_text', true );
			echo $text ? esc_html( $text ) : '&mdash;';
		}
	}
}
