<?php namespace MeowCrew\CancellationOffers\Offer\CPT\Form;

use MeowCrew\CancellationOffers\Core\ServiceContainerTrait;
use MeowCrew\CancellationOffers\Offer\Entity\Offer;

abstract class FormTab {

	use ServiceContainerTrait;

	/**
	 * Form
	 *
	 * @var Form
	 */
	protected $form;

	public function __construct( Form $form ) {
		$this->form = $form;
	}

	abstract public function getId();

	abstract public function getTitle();

	abstract public function getDescription();

	public function getIcon(): string {
		return '<span class="dashicons dashicons-clipboard"></span>';
	}

	abstract public function render( Offer $offer );

	public static function renderEditor( $args = array(), $editorType = 'full' ) {

		$args = wp_parse_args( $args, array(
				'id'    => false,
				'value' => null,
		) );

		if ( 'tiny' === $editorType ) {

			$editorArgs = array(
					'quicktags'        => false,
					'editor_height'    => 50,
					'textarea_rows'    => 2,
					'media_buttons'    => false,
					'editor_class'     => 'cancellation-offers-components__mce--tiny',
					'teeny'            => true,
					'drag_drop_upload' => false,
					'tinymce'          => array(
							'resize'   => 'vertical',
							'menubar'  => false,
							'wpautop'  => true,
							'toolbar2' => '',
							'toolbar1' => implode( ',', array(
									'bold',
									'italic',
									'strikethrough',
									'link',
									'alignleft',
									'aligncenter',
									'alignright',
									'spellchecker',
							) ),
					),
			);
		} else {

			$editorArgs = array(
					'editor_height'    => 100,
					'editor_class'     => 'cancellation-offers-components__mce',
					'tinymce'          => array(
							'resize' => 'vertical',
					),
					'drag_drop_upload' => false,
			);
		}


		if ( ! empty( $args['name'] ) ) {
			$editorArgs['textarea_name'] = $args['name'];
		}

		wp_editor( $args['value'], $args['id'], $editorArgs );

	}

	public static function renderInputRow( $args = array() ) {

		$args = wp_parse_args( $args, array(
				'id'                => false,
				'name'              => false,
				'label'             => '',
				'type'              => 'text',
				'custom_attributes' => array(),
				'is_required'       => false,
				'description'       => false,
				'value'             => null,
				'placeholder'       => '',
				'help_tip'          => false,
		) );

		// Consider throwing an  error
		if ( ! $args['id'] ) {
			return;
		}

		$name = $args['name'] ? $args['name'] : $args['id'];

		?>
		<div
				class="cancellation-offers-components-row cancellation-offers-components-row--<?php echo esc_attr( $args['id'] ); ?>">
			<div class="cancellation-offers-components-row__label">
				<label for="<?php echo esc_attr( $args['id'] ); ?>">
					<?php echo esc_html( $args['label'] ); ?>
				</label>

				<?php if ( $args['help_tip'] ): ?>
					<?php echo wp_kses_post( wc_help_tip( $args['help_tip'] ) ); ?>
				<?php endif; ?>
			</div>
			<div class="cancellation-offers-components-row__value">

				<?php if ( in_array( $args['type'], array( 'text', 'number', 'email' ) ) ): ?>

					<input <?php echo esc_attr( $args['is_required'] ? 'required' : '' ); ?>
							name="<?php echo esc_attr( $name ); ?>"
							type="<?php echo esc_attr( $args['type'] ); ?>"
							placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"
							id="<?php echo esc_attr( $args['id'] ); ?>"
							type="<?php echo esc_attr( $args['type'] ); ?>"
							value="<?php echo esc_attr( $args['value'] ); ?>">

				<?php elseif ( 'textarea' === $args['type'] ): ?>

					<textarea <?php echo esc_attr( $args['is_required'] ? 'required' : '' ); ?>
						id="<?php echo esc_attr( $args['id'] ); ?>" rows="4" name="<?php echo esc_attr( $name ); ?>"
						placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"><?php echo esc_attr( $args['value'] ); ?></textarea>

				<?php elseif ( 'checkbox' === $args['type'] ): ?>
					<div class="cancellation-offers-components__checkbox">
						<?php
							$args = wp_parse_args( $args, array(
									'checked' => false,
							) );
						?>

						<input type="checkbox" value="yes" name="<?php echo esc_attr( $name ); ?>"
							   id="<?php echo esc_attr( $args['id'] ); ?>" <?php checked( $args['checked'] ); ?> />

						<label for="<?php echo esc_attr( $args['id'] ); ?>"></label>
					</div>

				<?php elseif ( 'multiple-select' === $args['type'] ): ?>
					<?php
					$args = wp_parse_args( $args, array(
							'action'    => '',
							'options'   => array(),
							'css_class' => 'wc-product-search',
					) );
					?>
					<div>
						<select class="<?php echo esc_attr( $args['css_class'] ); ?>" multiple="multiple"
								id="<?php echo esc_attr( $args['id'] ); ?>"
								name="<?php echo esc_attr( $name ); ?>[]"
								data-placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>" <?php if ( $args['action'] ): ?>
							data-action="<?php echo esc_attr( $args['action'] ); ?>" <?php endif; ?>>

							<?php foreach ( $args['options'] as $key => $optionData ): ?>
								<option <?php selected( $optionData['is_selected'] ); ?>
										value="<?php echo esc_attr( $key ); ?>">
									<?php echo esc_attr( $optionData['label'] ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>

				<?php elseif ( 'select2' === $args['type'] ): ?>
					<?php
					$args = wp_parse_args( $args, array(
							'action'               => '',
							'options'              => array(),
							'css_class'            => 'wc-product-search',
							'minimum_input_length' => 2,
					) );
					?>
					<div>
						<select class="<?php echo esc_attr( $args['css_class'] ); ?>"
								id="<?php echo esc_attr( $args['id'] ); ?>"
								name="<?php echo esc_attr( $name ); ?>"
								data-minimum_input_length="<?php echo esc_attr( $args['minimum_input_length'] ); ?>"
								data-placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>" <?php if ( $args['action'] ): ?>
							data-action="<?php echo esc_attr( $args['action'] ); ?>" <?php endif; ?>>

							<?php foreach ( $args['options'] as $key => $optionData ): ?>
								<option <?php selected( $optionData['is_selected'] ); ?>
										value="<?php echo esc_attr( $key ); ?>">
									<?php echo esc_attr( $optionData['label'] ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>

				<?php elseif ( 'editor' === $args['type'] ): ?>
					<?php self::renderEditor( $args ); ?>

				<?php elseif ( 'tiny-editor' === $args['type'] ): ?>
					<?php self::renderEditor( $args, 'tiny' ); ?>

				<?php elseif ( 'radio' === $args['type'] ): ?>
					<div class="cancellation-offers-components__radios">
						<?php foreach ( $args['options'] as $key => $optionData ): ?>
							<div class="cancellation-offers-components__radio-item" style="margin-bottom: 5px;">
								<label>
									<input type="radio" name="<?php echo esc_attr( $name ); ?>"
										   value="<?php echo esc_attr( $key ); ?>"
											<?php checked( $optionData['is_selected'] ); ?>>
									<?php echo esc_html( $optionData['label'] ); ?>
								</label>
							</div>
						<?php endforeach; ?>
					</div>

				<?php elseif ( 'color' === $args['type'] ): ?>

					<input class="cancellation-offers-color-picker" type="text"
						   name="<?php echo esc_attr( $name ); ?>"
						   id="<?php echo esc_attr( $args['id'] ); ?>"
						   value="<?php echo esc_attr( $args['value'] ); ?>"
						   placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>">

				<?php elseif ( 'image' === $args['type'] ): ?>
					<div class="cancellation-offers-components__image-uploader"
						 data-target-id="<?php echo esc_attr( $args['id'] ); ?>">
						<div class="image-preview-wrapper" style="margin-bottom: 10px;">
							<?php
								$display = $args['value'] ? 'block' : 'none';
								$src     = $args['value'];
							?>
							<img class="image-preview" src="<?php echo esc_url( $src ); ?>"
								 style="max-width: 150px; display: <?php echo esc_attr( $display ); ?>;"/>
						</div>

						<input type="hidden" name="<?php echo esc_attr( $name ); ?>"
							   id="<?php echo esc_attr( $args['id'] ); ?>"
							   value="<?php echo esc_attr( $args['value'] ); ?>"/>

						<p class="hide-if-no-js">
							<a href="#" class="button button-secondary upload-image-button">
								<?php esc_html_e( 'Select Image',
										'cancellation-surveys-offers-woocommerce-subscriptions' ); ?>
							</a>
							<a href="#" class="button button-link-delete remove-image-button"
							   style="display: <?php echo $args['value'] ? 'inline-block' : 'none'; ?>;">
								<?php esc_html_e( 'Remove Image',
										'cancellation-surveys-offers-woocommerce-subscriptions' ); ?>
							</a>
						</p>
					</div>
				<?php endif; ?>

				<?php if ( ! empty( $args['description'] ) ): ?>
					<p class="description">
						<?php if ( is_callable( $args['description'] ) ): ?>
							<?php call_user_func( $args['description'] ); ?>
						<?php else: ?>
							<?php echo wp_kses_post( $args['description'] ); ?>
						<?php endif; ?>
					</p>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}
}
