<?php

namespace TotalThemeCore\Widgets;

use TotalThemeCore\WidgetBuilder;

defined( 'ABSPATH' ) || exit;

/**
 * About widget.
 */
class Widget_About extends WidgetBuilder {

	/**
	 * Widget args.
	 */
	private $args;

	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {
		$branding = $this->branding();
		$name = $branding
			? sprintf(
				/* translators: branding label */
				esc_html__( '%s - About', 'total-theme-core' ),
				$branding
			)
			: esc_html__( 'About', 'total-theme-core' );

		$this->args = [
			'id_base' => 'wpex_about',
			'name'    => $name,
			'options' => [
				'customize_selective_refresh' => true,
				//'show_instance_in_rest' => true,
			],
			'fields'  => [
				[
					'id'    => 'title',
					'label' => esc_html__( 'Title', 'total-theme-core' ),
					'type'  => 'text',
				],
				[
					'id'    => 'image',
					'label' => esc_html__( 'Image', 'total-theme-core' ),
					'type'  => 'media_upload',
				],
				[
					'id'      => 'img_size',
					'label'   => esc_html__( 'Image Size', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => 'intermediate_image_sizes',
					'exclude_custom' => true,
				],
				[
					'id'      => 'img_style',
					'label'   => esc_html__( 'Image Style', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => [
						'plain'   => esc_html__( 'Plain', 'total-theme-core' ),
						'rounded' => esc_html__( 'Rounded', 'total-theme-core' ),
						'round'   => esc_html__( 'Round', 'total-theme-core' ),
					],
					'default' => 'plain',
				],
				[
					'id'    => 'img_max_width',
					'label' => esc_html__( 'Image Max Width', 'total-theme-core' ),
					'type'  => 'text',
				],
				[
					'id'      => 'img_aspect_ratio',
					'label'   => esc_html__( 'Image Aspect Ratio', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => 'aspect_ratio',
				],
				[
					'id'      => 'alignment',
					'label'   => esc_html__( 'Alignment', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => [
						''       => esc_html__( 'Default', 'total-theme-core' ),
						'left'   => esc_html__( 'Left', 'total-theme-core' ),
						'center' => esc_html__( 'Center', 'total-theme-core' ),
						'right'  => esc_html__( 'Right', 'total-theme-core' ),
					],
				],
				[
					'id'    => 'description',
					'label' => esc_html__( 'Description', 'total-theme-core' ),
					'type'  => 'textarea',
				],
				[
					'id'    => 'wpautop',
					'label' => esc_html__( 'Automatically add paragraphs', 'total-theme-core' ),
					'type'  => 'checkbox',
				],
			],
		];

		$this->create_widget( $this->args );
	}

	/**
	 * Front-end display of widget.
	 */
	public function widget( $args, $instance ) {
		extract( $this->parse_instance( $instance ) );

		echo wp_kses_post( $args['before_widget'] );

		$this->widget_title( $args, $instance );

		$html = '';

		// Wrap classes
		$classes = [
			'wpex-about-widget',
			'wpex-clr'
		];

		if ( $alignment ) {
			$classes[] = 'wpex-text-' . sanitize_html_class( $alignment );
		}

		// Begin widget wrap
		$html .= '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">';

		// Sanitize image
		if ( is_numeric( $image ) ) {
			$img_size = $img_size ?: 'full';
			$image = wp_get_attachment_image_url( $image, $img_size );
		}

		// Display the image
		if ( $image ) {
			$img_classes = [ 'wpex-align-middle' ];
			if ( 'round' === $img_style || 'rounded' === $img_style ) {
				$img_classes[] = 'wpex-' . sanitize_html_class( $img_style );
			}
			if ( ! empty( $img_aspect_ratio ) && preg_match( '#^\d+/\d+$#', $img_aspect_ratio ) ) {
				$img_classes[] = 'wpex-object-cover';
				$img_classes[] = 'wpex-aspect-' . str_replace( '/', '-', $img_aspect_ratio );
			}
			$img_style = '';
			if ( ! empty( $img_max_width ) ) {
				$img_max_width_safe = sanitize_text_field( $img_max_width );
				if ( is_numeric( $img_max_width_safe ) ) {
					$img_max_width_safe = "{$img_max_width_safe}px";
				}
				if ( $img_max_width_safe ) {
					$img_style .= "width:{$img_max_width_safe}";
				}
			}
			if ( $img_style ) {
				$img_style = ' style="' . esc_attr( $img_style ) . '"';
			}
			$html .= '<div class="wpex-about-widget-image wpex-mb-20">';
				$html .= '<img class="' . esc_attr( implode( ' ', $img_classes ) ) . '" src="' . esc_url( $image ) . '" alt="' . esc_attr( $title ) . '"' . $img_style . '>';
			$html .= '</div>';

		}

		// Display the description
		if ( $description ) {
			$html .= '<div class="wpex-about-widget-description wpex-last-mb-0 wpex-clr">';
				if ( true == wp_validate_boolean( $wpautop ) ) {
					$html .= wpautop( wp_kses_post( $description ) );
				} else {
					$html .= wp_kses_post( $description );
				}
			$html .= '</div>';
		}

		// Close widget wrap
		$html .= '</div>';

		// @codingStandardsIgnoreLine
		echo $html;
		echo wp_kses_post( $args['after_widget'] );
	}

}
register_widget( 'TotalThemeCore\\Widgets\\Widget_About' );
