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

/**
 * Inventory_Presser_Location_Address
 *
 * This class creates the Address widget.
 */
class Inventory_Presser_Location_Address extends WP_Widget {


	const ID_BASE = '_invp_address';

	/**
	 * __construct
	 *
	 * Calls the parent class' contructor and adds a hook that will delete the
	 * option that stores this widget's data when the plugin's delete all data
	 * method is run.
	 *
	 * @return void
	 */
	function __construct() {
		parent::__construct(
			self::ID_BASE,
			__( 'Address', 'inventory-presser' ),
			array(
				'description'           => __( 'Display one or more mailing addresses.', 'inventory-presser' ),
				'show_instance_in_rest' => true,
			)
		);

		add_action( 'invp_delete_all_data', array( $this, 'delete_option' ) );
	}

	/**
	 * delete_option
	 *
	 * Deletes the option that stores this widget's data.
	 *
	 * @return void
	 */
	public function delete_option() {
		delete_option( 'widget_' . self::ID_BASE );
	}

	/**
	 * Outputs the content of the widget
	 *
	 * @param array $args
	 * @param array $instance
	 */
	public function widget( $args, $instance ) {
		if ( empty( $instance['cb_display'] ) ) {
			return;
		}

		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] );
		// before and after widget arguments are defined by themes.
		echo wp_kses_post( $args['before_widget'] );
		if ( ! empty( $title ) ) {
			echo wp_kses_post( $args['before_title'] ) . esc_html( $title ) . wp_kses_post( $args['after_title'] );
		}

		if ( isset( $instance['cb_single_line'] ) && $instance['cb_single_line'] == 'true' ) {
			foreach ( $instance['cb_display'] as $i => $term_id ) {
				$location = get_term( $term_id, 'location' );
				if ( ! is_wp_error( $location ) && null != $location ) {
					printf(
						'<span>%s</span>',
						esc_html( str_replace( PHP_EOL, ', ', trim( $location->description ) ) )
					);
				}
			}
		} else {
			foreach ( $instance['cb_display'] as $i => $term_id ) {
				$location = get_term( $term_id, 'location' );
				if ( ! is_wp_error( $location ) && null !== $location ) {
					echo '<div>' . wp_kses_post( nl2br( $location->description ) ) . '</div>';
				}
			}
		}

		echo wp_kses_post( $args['after_widget'] );
	}

	/**
	 * Outputs the widget settings form that is shown in the dashboard.
	 *
	 * @param  array $instance
	 * @return void
	 */
	public function form( $instance ) {

		$title = isset( $instance['title'] ) ? $instance['title'] : '';

		// get all locations.
		$location_terms = get_terms(
			array(
				'taxonomy'   => 'location',
				'hide_empty' => false,
			)
		);

		// set.
		if ( isset( $instance['cb_display'] ) ) {
			$cb_display = $instance['cb_display'];
		} else {
			// majority of dealers will have one address, let's precheck it for them.
			if ( count( $location_terms ) == 1 ) {
				$cb_display = array( $location_terms[0]->term_id );
			} else {
				$cb_display = array();
			}
		}

		$address_table = '<table><tbody>'
			. '<tr><td colspan="2">'
			. esc_html__( 'Select Addresses to Display', 'inventory-presser' )
			. '</td></tr>';

		// loop through each location, set up form.
		foreach ( $location_terms as $index => $term_object ) {
			$address_checkbox = sprintf(
				'<input id="%s" name="%s" value="%s" type="checkbox"%s>',
				esc_attr( $this->get_field_id( 'cb_title' ) ),
				esc_attr( $this->get_field_name( 'cb_display[]' ) ),
				esc_attr( $term_object->term_id ),
				checked( ( in_array( $term_object->term_id, $cb_display ) ), true, false )
			);
			$address_table   .= sprintf( '<tr><td>%s</td><td>%s</td></tr>', $address_checkbox, wp_kses_post( nl2br( $term_object->description ) ) );
		}

		$address_table .= '</tbody></table>';

		// Widget admin form.
		?>
		<p>
		<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'inventory-presser' ); ?></label>
		<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>
		<p>
		<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'cb_single_line' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'cb_single_line' ) ); ?>" value="true"<?php checked( ( isset( $instance['cb_single_line'] ) && 'true' === $instance['cb_single_line'] ) ); ?>>
		<label for="<?php echo esc_attr( $this->get_field_id( 'cb_single_line' ) ); ?>"><?php esc_html_e( 'Single Line Display', 'inventory-presser' ); ?></label>
		</p>
		<p>
		<?php
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $address_table;
		?>
		</p>
		<?php
	}

	/**
	 * Saves the widget settings when a dashboard user clicks the Save button.
	 *
	 * @param  array $new_instance
	 * @param  array $old_instance
	 * @return array The updated array full of settings
	 */
	public function update( $new_instance, $old_instance ) {
		$instance                   = array();
		$instance['title']          = ( ! empty( $new_instance['title'] ) ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
		$instance['cb_display']     = ( ! empty( $new_instance['cb_display'] ) ) ? $new_instance['cb_display'] : array();
		$instance['cb_single_line'] = ( ! empty( $new_instance['cb_single_line'] ) ) ? $new_instance['cb_single_line'] : '';
		return $instance;
	}
}
