<?php

namespace IGD_PRO;

use Elementor\Controls_Manager;
use Elementor\Plugin;
use Elementor\Widget_Base;
use MetForm\Traits\Common_Controls;
use MetForm\Traits\Conditional_Controls;
use MetForm\Utils\Util;
use MetForm\Widgets\Widget_Notice;

defined( 'ABSPATH' ) || exit();

class MetForm extends Widget_Base {
	use Common_Controls;
	use Conditional_Controls;
	use Widget_Notice;

	public function get_name() {
		return 'mf-igd-uploader';
	}

	public function get_title() {
		return __( 'Google Drive Upload', 'integrate-google-drive-premium' );
	}

	public function get_icon() {
		return 'igd-uploader';
	}

	public function show_in_panel() {
		return 'metform-form' == get_post_type();
	}

	public function get_categories() {
		return [ 'metform' ];
	}

	public function get_keywords() {
		return [
			"file uploader",
			"uploader",
			"google drive",
			"drive",
			"module",
			"integrate google drive",
		];
	}

	public function get_script_depends() {
		return [
			'igd-frontend',
		];
	}

	public function get_style_depends() {
		return [
			'igd-frontend',
		];
	}

	public function register_controls() {

		$shortcodes = \IGD\Shortcode::get_shortcodes();

		$shortcodes = array_filter( $shortcodes, function ( $shortcode ) {
			return 'browser' === $shortcode['type'] || 'uploader' === $shortcode['type'];
		} );

		$options = [
			'' => __( 'Select Module', 'integrate-google-drive-premium' ),
		];

		if ( ! empty( $shortcodes ) ) {
			foreach ( $shortcodes as $shortcode ) {
				$options[ $shortcode['id'] ] = $shortcode['title'];
			}
		}

		$this->start_controls_section(
			'content_section',
			[
				'label' => esc_html__( 'Content', 'integrate-google-drive-premium' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control( 'module_id', [
			'label'       => __( 'Select Module', 'integrate-google-drive-premium' ),
			'type'        => Controls_Manager::SELECT2,
			'label_block' => true,
			'options'     => $options,
		] );

		//Edit button
		$this->add_control( 'edit_field_metform', [
			'type'        => Controls_Manager::BUTTON,
			'label'       => '',
			'text'        => '<i class="eicon eicon-settings"></i>' . __( 'Configure', 'integrate-google-drive-premium' ),
			'event'       => 'igd:editor:edit_module',
			'description' => __( 'Select a module or create a new one.', 'integrate-google-drive-premium' ),
		] );

		// Label controls
		$this->input_content_controls( [ 'NO_PLACEHOLDER' ] );

		// Required controls
		$this->input_setting_controls();

		$this->add_control(
			'mf_input_validation_type',
			[
				'label'   => __( 'Validation Type', 'integrate-google-drive-premium' ),
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'none',
			]
		);

		$this->add_control(
			'mf_input_file_size_status',
			[
				'label'        => esc_html__( 'File Size Limit : ', 'integrate-google-drive-premium' ),
				'type'         => Controls_Manager::HIDDEN,
				'label_off'    => 'Off',
				'label_on'     => 'On',
				'return_value' => 'off',
			]
		);

		$this->end_controls_section();


		if ( class_exists( '\MetForm_Pro\Base\Package' ) ) {
			$this->input_conditional_control();
		}

		$this->start_controls_section(
			'label_section',
			[
				'label'      => esc_html__( 'Label', 'integrate-google-drive-premium' ),
				'tab'        => Controls_Manager::TAB_STYLE,
				'conditions' => [
					'relation' => 'or',
					'terms'    => [
						[
							'name'     => 'mf_input_label_status',
							'operator' => '===',
							'value'    => 'yes',
						],
						[
							'name'     => 'mf_input_required',
							'operator' => '===',
							'value'    => 'yes',
						],
					],
				],
			]
		);

		$this->input_label_controls( [ 'FILE_SIZE_WARNING' ] );

		$this->end_controls_section();

	}

	public function render() {
		$settings = $this->get_settings_for_display();
		extract( $settings );


		$render_on_editor = false;
		$is_edit_mode     = 'metform-form' === get_post_type() && Plugin::$instance->editor->is_edit_mode();

		$default_data = [
			'type'           => 'uploader',
			'isFormUploader' => 'elementor',
			'isRequired'     => ! empty( $item['required'] ),
		];

		$module_data = \IGD\Shortcode::get_shortcode_data( $module_id );

		$data = wp_parse_args( $module_data, $default_data );

		$configData = [
			'message'  => isset( $mf_input_validation_warning_message ) ? ! empty( $mf_input_validation_warning_message ) ? $mf_input_validation_warning_message : esc_html__( 'This field is required.', 'integrate-google-drive-premium' ) : esc_html__( 'This field is required.', 'integrate-google-drive-premium' ),
			'required' => isset( $mf_input_required ) && $mf_input_required == 'yes',

		];

		?>

        <div class="mf-input-wrapper ${validation.errors['<?php echo esc_attr( $mf_input_name ); ?>'] ? 'has-error' : '' }">

			<?php if ( 'yes' == $mf_input_label_status ) : ?>
            <label class="mf-input-label"
                   for="mf-input-google-drive-upload-<?php echo esc_attr( $this->get_id() ); ?>">
				<?php echo esc_html( Util::react_entity_support( $mf_input_label, $render_on_editor ) ); ?>
                <span class="mf-input-required-indicator"><?php echo esc_html( ( $mf_input_required === 'yes' ) ? '*' : '' ); ?></span>
            </label>
			<?php endif; ?>

			<?php

			echo \IGD\Shortcode::instance()->render_shortcode( [], $data );

			?>

            <input type="text" class="mf-input upload-file-list igd-hidden"
                   id="mf-input-google-drive-upload-<?php echo esc_attr( $this->get_id() ); ?>"
                   name="<?php echo esc_attr( $mf_input_name ); ?>"

				<?php if ( ! $is_edit_mode ): ?>
                   aria-invalid=${validation.errors['<?php echo esc_attr( $mf_input_name ); ?>'] ? 'true' : 'false' }
            ref=${ el => parent.activateValidation(<?php echo json_encode( $configData ); ?>, el) }
			<?php endif; ?>

            />

			<?php if ( ! $is_edit_mode ) : ?>
            <${validation.ErrorMessage}
            errors=${validation.errors}
            name="<?php echo esc_attr( $mf_input_name ); ?>"
            as=${html`<span className="mf-error-message"></span>`}
            />
			<?php endif; ?>

			<?php echo( '' !== trim( $mf_input_help_text ) ? sprintf( '<span class="mf-input-help"> %s </span>', esc_html( Util::react_entity_support( trim( $mf_input_help_text ), $render_on_editor ) ) ) : '' ); ?>
        </div>
		<?php

	}

}
