<?php
/**
 * WPForm widget class
 *
 * @package Banana_Addons
 */
use Banana_Addons\Elementor\Helper;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;

if ( ! defined( 'ABSPATH' ) )
	exit; // Exit if accessed directly

class Banae_WPForm_Control {

	/**
	 * The main widget controls registration method
	 * 
	 * @param mixed $widget
	 * 
	 * @return void
	 */
	public static function add_register_controls( $widget ) {

		// Check if WP Form is active
		if ( ! Helper::is_wpforms_activated() ) {

			// WP Form is not active
			$widget->start_controls_section(
				'section_wpform_not_active',
				[
					'label' => __( 'WP Form Not Active', 'banana-addons-for-elementor' ),
					'tab' => Controls_Manager::TAB_CONTENT,
				]
			);

			$widget->add_control(
				'wpform_not_active_message',
				[
					'type' => Controls_Manager::RAW_HTML,
					'raw' => __( 'Please install and activate the WP Form plugin to use this widget.', 'banana-addons-for-elementor' ),
					'content_classes' => 'elementor-panel-alert elementor-panel-alert-danger',
				]
			);

			$widget->end_controls_section();

			return;
		}

		$widget->start_controls_section(
			'wpform_content_section',
			[
				'label' => __( 'WPForms', 'banana-addons-for-elementor' ),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$widget->add_control(
			'wpform_form_id',
			[
				'label' => __( 'Select Your Form', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::SELECT,
				'label_block' => true,
				'options' => [ '' => __( '', 'banana-addons-for-elementor' ) ] + Helper::get_wpforms_as_options(),
				'description' => __( 'Select the WP Form form you want to display.', 'banana-addons-for-elementor' ),
			]
		);

		$widget->end_controls_section();

		// WPForm Style Controls Section
		$widget->start_controls_section(
			'wpform_fields_style_section',
			[
				'label' => __( 'Form Fields', 'banana-addons-for-elementor' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$widget->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'wpform_field_typography',
				'label' => __( 'Typography', 'banana-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .wpforms-field input, {{WRAPPER}} .wpforms-field-textarea textarea',
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_TEXT,
				],
			]
		);

		$widget->add_control(
			'wpform_field_textcolor',
			[
				'label' => __( 'Text Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-field input, {{WRAPPER}} .wpforms-field-textarea textarea' => 'color: {{VALUE}}',
				],
			]
		);

		$widget->add_control(
			'wpform_field_placeholder_color',
			[
				'label' => __( 'Placeholder Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} ::-webkit-input-placeholder' => 'color: {{VALUE}};',
					'{{WRAPPER}} ::-moz-placeholder' => 'color: {{VALUE}};',
					'{{WRAPPER}} ::-ms-input-placeholder' => 'color: {{VALUE}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpform_field_margin',
			[
				'label' => __( 'Margin', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-field:not(.wpforms-submit), .wpforms-field-required:not(.wpforms-submit)' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpform_field_padding',
			[
				'label' => __( 'Padding', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-field input' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} .wpforms-field textarea' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpform_field_border_radius',
			[
				'label' => __( 'Border Radius', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-field input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} .wpforms-field textarea' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->start_controls_tabs( 'tabs_field_state' );

		$widget->start_controls_tab(
			'wpform_tab_field_normal',
			[
				'label' => __( 'Normal', 'banana-addons-for-elementor' ),
			]
		);

		$widget->add_control(
			'wpform_field_bg_color',
			[
				'label' => __( 'Background Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-field input, {{WRAPPER}} .wpforms-field-textarea textarea' => 'background-color: {{VALUE}}',
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'wpform_field_border',
				'selector' => '{{WRAPPER}} .wpforms-field input, {{WRAPPER}} .wpforms-field-textarea textarea',
			]
		);

		$widget->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'wpform_field_box_shadow',
				'selector' => '{{WRAPPER}} .wpforms-field input, {{WRAPPER}} .wpforms-field-textarea textarea',
			]
		);

		$widget->end_controls_tab();

		$widget->start_controls_tab(
			'wpform_tab_field_focus',
			[
				'label' => __( 'Focus', 'banana-addons-for-elementor' ),
			]
		);

		$widget->add_control(
			'wpform_field_focus_bg_color',
			[
				'label' => __( 'Background Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-field input:focus, {{WRAPPER}} .wpforms-field-textarea textarea:focus' => 'background-color: {{VALUE}}',
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'wpform_field_focus_border',
				'selector' => '{{WRAPPER}} .wpforms-field input:focus, {{WRAPPER}} .wpforms-field-textarea textarea:focus',
			]
		);

		$widget->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'wpform_field_focus_box_shadow',
				'exclude' => [
					'box_shadow_position',
				],
				'selector' => '{{WRAPPER}} .wpforms-field input:focus, {{WRAPPER}} .wpforms-field-textarea textarea:focus',
			]
		);

		$widget->end_controls_tab();
		$widget->end_controls_tabs();

		$widget->end_controls_section();

		// Form Fields Label Style Controls Section
		$widget->start_controls_section(
			'wpf-form-label',
			[
				'label' => __( 'Form Fields Label', 'banana-addons-for-elementor' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$widget->add_control(
			'label_options',
			[
				'label' => esc_html__( 'Label Options', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$widget->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'wpf_label_typography',
				'label' => __( 'Typography', 'banana-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .wpforms-field-container label.wpforms-field-label',
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_TEXT,
				],
			]
		);

		$widget->add_control(
			'sub_label_options',
			[
				'label' => esc_html__( 'Sub Label Options', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$widget->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'wpf_sublabel_typography',
				'label' => __( 'Typography', 'banana-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .wpforms-field-sublabel',
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_TEXT,
				],
			]
		);

		$widget->add_control(
			'desc_options',
			[
				'label' => esc_html__( 'Description Options', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$widget->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'wpf_desc_typography',
				'label' => __( 'Description Typography', 'banana-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .wpforms-field-description',
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_TEXT,
				],
			]
		);

		$widget->add_control(
			'label_color_options',
			[
				'label' => esc_html__( 'Label Color Options', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$widget->add_control(
			'wpf_label_color_popover',
			[
				'label' => __( 'Colors', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::POPOVER_TOGGLE,
				'label_off' => __( '', 'banana-addons-for-elementor' ),
				'label_on' => __( 'Custom', 'banana-addons-for-elementor' ),
				'return_value' => 'yes',
			]
		);

		$widget->start_popover();

		$widget->add_control(
			'wpf_label_color',
			[
				'label' => __( 'Label Text Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-field-container label.wpforms-field-label' => 'color: {{VALUE}}',
				],
				'condition' => [
					'wpf_label_color_popover' => 'yes'
				],
			]
		);

		$widget->add_control(
			'wpf_required_label',
			[
				'label' => __( 'Required Label Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-required-label' => 'color: {{VALUE}}',
				],
				'condition' => [
					'wpf_label_color_popover' => 'yes'
				],
			]
		);

		$widget->add_control(
			'wpf_sublabel_color',
			[
				'label' => __( 'Sub Label Text Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-field-sublabel' => 'color: {{VALUE}}',
				],
				'condition' => [
					'wpf_label_color_popover' => 'yes'
				],
			]
		);

		$widget->add_control(
			'wpf_desc_label_color',
			[
				'label' => __( 'Description Text Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-field-description' => 'color: {{VALUE}}',
				],
				'condition' => [
					'wpf_label_color_popover' => 'yes'
				],
			]
		);

		$widget->end_popover();

		$widget->add_responsive_control(
			'wpf_label_margin',
			[
				'label' => __( 'Margin', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-field-container label.wpforms-field-label' => 'display: inline-block; padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpf_label_padding',
			[
				'label' => __( 'Padding', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-field-container label.wpforms-field-label' => 'display: inline-block; padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->end_controls_section();

		// Submit Button Style Controls Section
		$widget->start_controls_section(
			'wpf_submit_style_section',
			[
				'label' => __( 'Submit Button', 'banana-addons-for-elementor' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$widget->add_control(
			'wpf_submit_btn_width',
			[
				'label' => __( 'Button Full Width?', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => __( 'Yes', 'banana-addons-for-elementor' ),
				'label_off' => __( 'No', 'banana-addons-for-elementor' ),
				'return_value' => 'yes',
				'default' => 'yes',
			]
		);

		$widget->add_responsive_control(
			'wpf_button_width',
			[
				'label' => __( 'Button Width', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'condition' => [
					'wpf_submit_btn_width' => 'yes'
				],
				'default' => [
					'unit' => '%',
					'size' => 100
				],
				'range' => [
					'%' => [
						'min' => 1,
						'max' => 100,
					],
					'px' => [
						'min' => 1,
						'max' => 800,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .wpforms-submit' => 'display: block; width: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpf_submit_btn_position',
			[
				'label' => __( 'Button Position', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => __( 'Left', 'banana-addons-for-elementor' ),
						'icon' => 'eicon-h-align-left',
					],
					'center' => [
						'title' => __( 'Center', 'banana-addons-for-elementor' ),
						'icon' => 'eicon-h-align-center',
					],
					'right' => [
						'title' => __( 'Right', 'banana-addons-for-elementor' ),
						'icon' => 'eicon-h-align-right',
					],
				],
				'condition' => [
					'wpf_submit_btn_width' => '',

				],
				'desktop_default' => 'left',
				'toggle' => false,
				'prefix_class' => 'banae-form-btn--%s',
				'selectors' => [
					'{{WRAPPER}} .wpforms-submit-container' => 'text-align: {{Value}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpf_submit_margin',
			[
				'label' => __( 'Margin', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-submit' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'wpf_submit_padding',
			[
				'label' => __( 'Padding', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'wpf_submit_typography',
				'selector' => '{{WRAPPER}} .wpforms-submit',
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_ACCENT,
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'wpf_submit_border',
				'selector' => '{{WRAPPER}} .wpforms-form .wpforms-submit-container button[type=submit]',
			]
		);

		$widget->add_control(
			'wpf_submit_border_radius',
			[
				'label' => __( 'Border Radius', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} .wpforms-submit' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'wpf_submit_box_shadow',
				'selector' => '{{WRAPPER}} .wpforms-submit',
			]
		);

		$widget->add_control(
			'wpf_hr4',
			[
				'type' => Controls_Manager::DIVIDER,
				'style' => 'thick',
			]
		);

		$widget->start_controls_tabs( 'wpf_tabs_button_style' );

		// Submit Button Normal State Tab
		$widget->start_controls_tab(
			'wpf_tab_button_normal',
			[
				'label' => __( 'Normal', 'banana-addons-for-elementor' ),
			]
		);

		$widget->add_control(
			'wpf_submit_color',
			[
				'label' => __( 'Text Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} .wpforms-container .wpforms-submit' => 'color: {{VALUE}};',
				],
			]
		);

		$widget->add_control(
			'wpf_submit_bg_color',
			[
				'label' => __( 'Background Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-container .wpforms-submit' => 'background-color: {{VALUE}};',
				],
			]
		);

		$widget->end_controls_tab();

		// Hover Tab
		$widget->start_controls_tab(
			'wpf_tab_button_hover',
			[
				'label' => __( 'Hover', 'banana-addons-for-elementor' ),
			]
		);

		$widget->add_control(
			'wpf_submit_hover_color',
			[
				'label' => __( 'Text Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-container .wpforms-submit:hover, {{WRAPPER}} .wpforms-container .wpforms-submit:focus' => 'color: {{VALUE}};',
				],
			]
		);

		$widget->add_control(
			'wpf_submit_hover_bg_color',
			[
				'label' => __( 'Background Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-container .wpforms-submit:hover, {{WRAPPER}} .wpforms-container .wpforms-submit:focus' => 'background-color: {{VALUE}};',
				],
			]
		);

		$widget->add_control(
			'wpf_submit_hover_border_color',
			[
				'label' => __( 'Border Color', 'banana-addons-for-elementor' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpforms-container .wpforms-submit:hover, {{WRAPPER}} .wpforms-container .wpforms-submit:focus' => 'border-color: {{VALUE}};',
				],
			]
		);

		$widget->end_controls_tab();
		$widget->end_controls_tabs();

		$widget->end_controls_section();
	}
}