<?php

namespace MasterAddons\Addons;

use MasterAddons\Inc\Classes\Base\Master_Widget;
use \Elementor\Controls_Stack;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Typography;
use \Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use \Elementor\Group_Control_Background;

use MasterAddons\Inc\Classes\Helper;
use MasterAddons\Inc\Admin\Config;

if (!defined('ABSPATH'))
	exit; // If this file is called directly, abort.

class Contact_Form_7 extends Master_Widget
{
	use \MasterAddons\Inc\Traits\Widget_Notice;
	use \MasterAddons\Inc\Traits\Widget_Assets_Trait;

	public function get_name()
	{
		return 'ma-contact-form-7';
	}
	public function get_title()
	{
		return esc_html__('Contact Form 7', 'master-addons');
	}

	public function get_icon()
	{
		return 'jltma-icon ' . Config::get_addon_icon('contact-form-7');
	}


	protected function register_controls()
	{

		/**
		 * Contact Form 7
		 * Title
		 * Form Name/ID
		 */

		$this->start_controls_section(
			'ma_cf7',
			[
				'label' => esc_html__('Contact Form 7', 'master-addons')
			]
		);

		$this->add_control(
			'ma_cf7_list',
			[
				'label' => esc_html__('Select Contact Form', 'master-addons'),
				'type' => Controls_Manager::SELECT,
				'label_block' => true,
				'options' => Helper::maad_el_retrive_cf7(),
				'default' => '0',
			]
		);

		$this->end_controls_section();

		/**
		 * Contact Form 7
		 * Layout Design
		 */

		$this->start_controls_section(
			'ma_cf7_section_style',
			[
				'label' => esc_html__('Design Layout', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		// Define default free options - Pro version overrides via filter
		$layout_style_options = apply_filters('master_addons/addons/contact_form_7/layout_style', [
			'1'         => __('Style One', 'master-addons'),
			'2'         => __('Style Two', 'master-addons'),
			'3'         => __('Style Three', 'master-addons'),
			'4'         => __('Style Four', 'master-addons'),
			'cf7-pro-1' => __('Style Five (Pro)', 'master-addons'),
			'cf7-pro-2' => __('Style Six (Pro)', 'master-addons'),
			'cf7-pro-3' => __('Style Seven (Pro)', 'master-addons'),
			'cf7-pro-4' => __('Style Eight (Pro)', 'master-addons'),
			'cf7-pro-5' => __('Style Nine (Pro)', 'master-addons'),
			'cf7-pro-6' => __('Style Ten (Pro)', 'master-addons'),
			'cf7-pro-7' => __('Style Eleven (Pro)', 'master-addons'),
		]);

		$this->add_control(
			'ma_cf7_layout_style',
			[
				'label'       => __('Design Variations', 'master-addons'),
				'type'        => Controls_Manager::SELECT,
				'default'     => '1',
				'options'     => $layout_style_options,
				'description' => Helper::upgrade_to_pro('10+ more Variations on'),
			]
		);

		$this->end_controls_section();

		/**
		 * Contact Form 7
		 * Error Messages
		 */

		$this->start_controls_section(
			'ma_cf7_section_errors',
			[
				'label' => esc_html__('Errors Style', 'master-addons')
			]
		);
		$this->add_control(
			'ma_cf7_error_messages',
			[
				'label' => __('Error Messages', 'master-addons'),
				'type' => Controls_Manager::SELECT,
				'default' => 'show',
				'options' => [
					'show' => __('Show', 'master-addons'),
					'hide' => __('Hide', 'master-addons'),
				],
				'selectors_dictionary' => [
					'show' => 'block',
					'hide' => 'none',
				],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-not-valid-tip' => 'display: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'ma_cf7_validation_errors',
			[
				'label' => __('Validation Errors', 'master-addons'),
				'type' => Controls_Manager::SELECT,
				'default' => 'show',
				'options' => [
					'show' => __('Show', 'master-addons'),
					'hide' => __('Hide', 'master-addons'),
				],
				'selectors_dictionary' => [
					'show' => 'block',
					'hide' => 'none',
				],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-validation-errors' => 'display: {{VALUE}} !important;',
				],
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Form Design
		 */
		$this->start_controls_section(
			'ma_cf7_section_container_style',
			[
				'label' => __('Form Container', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'ma_cf7_background',
				'label' => esc_html__('Background', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'selector' => '{{WRAPPER}} .master-addons-cf7',
			]
		);

		$this->add_control(
			'ma_cf7ainer_border_top',
			[
				'label' => esc_html__('Border Top Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#6e00e9',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7::before' => 'background: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'ma_cf7_width',
			[
				'label' => esc_html__('Form Width', 'master-addons'),
				'type' => Controls_Manager::SLIDER,
				'size_units' => ['px', 'em', '%'],
				'range' => [
					'px' => [
						'min' => 10,
						'max' => 1500,
					],
					'em' => [
						'min' => 1,
						'max' => 80,
					],
				],
				'default' => [
					'unit' => '%',
					'size' => '100'
				],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7' => 'width: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'ma_cf7_padding',
			[
				'label' => esc_html__('Form Padding', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'default' => [
					'top' => 40,
					'right' => 40,
					'bottom' => 40,
					'left' => 40,
					'unit' => 'px'
				]
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Title & Description
		 */
		$this->start_controls_section(
			'ma_cf7_section_title',
			[
				'label' => __('Title', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'title_text_color',
			[
				'label' => esc_html__('Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .master-addons-cf7-title' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_responsive_control(
			'ma_cf7_heading_alignment',
			[
				'label' => esc_html__('Alignment', 'master-addons'),
				'type' => Controls_Manager::CHOOSE,
				'options' => Helper::jltma_content_alignment(),
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .master-addons-cf7-title' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'ma_cf7_title_typography',
				'label' => esc_html__('Typography', 'master-addons'),
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_ACCENT,
				],
				'selector' => '{{WRAPPER}} .master-addons-cf7 .master-addons-cf7-title',
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Input & Textarea
		 */
		$this->start_controls_section(
			'section_fields_style',
			[
				'label' => esc_html__('Input & Textarea', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'ma_cf7_field_bg',
			[
				'label' => esc_html__('Background Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-text, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-textarea, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-select' => 'background-color: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'ma_cf7_field_text_color',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-text,
					 {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-textarea, 
					 {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-select' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'ma_cf7_field_padding',
			[
				'label' => esc_html__('Padding', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'default' => [
					'unit' => 'px',
					'size' => 15,
				],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-text, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-textarea' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'ma_cf7_field_width',
			[
				'label' => esc_html__('Field Width', 'master-addons'),
				'type' => Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 1200,
						'step' => 1,
					],
				],
				'size_units' => ['px', 'em', '%'],
				'default' => [
					'unit' => '%',
					'size' => 100,
				],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-text, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-textarea' => 'width: {{SIZE}}{{UNIT}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'field_border',
				'label' => esc_html__('Border', 'master-addons'),
				'placeholder' => '1px',
				'default' => '1px',
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-text, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-textarea, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-select',
				'separator' => 'before',
			]
		);

		$this->add_control(
			'ma_cf7_field_radius',
			[
				'label' => esc_html__('Border Radius', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-text, {{WRAPPER}} .master-addons-cf7 .wpcf7-form-control.wpcf7-textarea' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} .master-addons-cf7-9 .wpcf7-form-control,
                    {{WRAPPER}} .master-addons-cf7-10 .wpcf7-form-control,
                    {{WRAPPER}} .master-addons-cf7-11 .wpcf7-form-control' => 'border-radius:2em'

				],
			]
		);
		$this->end_controls_section();

		/**
		 * Style Tab: Label Section
		 */
		$this->start_controls_section(
			'ma_cf7_section_label_style',
			[
				'label' => esc_html__('Labels', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'text_color_label',
			[
				'label' => esc_html__('Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form label' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'ma_cf7_typography_label',
				'label' => esc_html__('Typography', 'master-addons'),
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_ACCENT,
				],
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-form label',
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Submit Button
		 */
		$this->start_controls_section(
			'ma_cf7_section_submit_button_style',
			[
				'label' => esc_html__('Submit Button', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'ma_cf7_button_typography',
				'label' => esc_html__('Button Typography', 'master-addons'),
				'global' => [
					'default' => Global_Typography::TYPOGRAPHY_ACCENT,
				],
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]',
			]
		);

		$this->start_controls_tabs('tabs_button_style');

		$this->start_controls_tab(
			'ma_cf7_tab_button_normal',
			[
				'label' => esc_html__('Normal', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'ma_cf7_button_bg_color_normal',
				'label' => esc_html__('Background', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'default' => '#6e00e9',
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]',
			]
		);

		$this->add_control(
			'ma_cf7_button_text_color_normal',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#FFF',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]' => 'color: {{VALUE}}',
				],
			]
		);

		$this->add_control(
			'ma_cf7_button_border_color_normal',
			[
				'label' => esc_html__('Border Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => 'transparent',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]' => 'border-color: {{VALUE}}',
				],
			]
		);

		$this->end_controls_tab();

		/*
		 * Hover Styles
		 */
		$this->start_controls_tab(
			'tab_button_hover',
			[
				'label' => esc_html__('Hover', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'ma_cf7_button_bg_color_hover',
				'label' => esc_html__('Background', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'default' => '#fff',
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]:hover',
			]
		);

		$this->add_control(
			'ma_cf7_button_text_color_hover',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#6e00e9',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]:hover' => 'color: {{VALUE}}',
				],
			]
		);
		//
		//		$this->add_group_control(
		//			Group_Control_Border::get_type(),
		//			[
		//				'name'                  => 'ma_cf7_button_border_width',
		//				'label'                 => esc_html__( 'Border', 'master-addons' ),
		//				'placeholder'           => '1px',
		//				'default'               => '1px',
		////				'selector'              => '{{WRAPPER}} .master-addons-cf7 .wpcf7-validation-errors',
		//				'selectors'             => '{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]:hover',
		//				'separator'             => 'before',
		//			]
		//		);

		$this->add_control(
			'ma_cf7_button_border_width',
			[
				'label' => esc_html__('Border Width', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'separator' => 'before',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-submit' => 'border-width: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'default' => [
					'top' => 0,
					'right' => 0,
					'bottom' => 0,
					'left' => 0,
					'unit' => 'px'
				]
			]
		);

		$this->add_control(
			'ma_cf7_button_border_color_hover',
			[
				'label' => esc_html__('Border Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#6e00e9',
				'selectors' => [
					//					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]:hover' => 'border-color:{{VALUE}}',
					'{{WRAPPER}} .ma-cf input[type="submit"]:hover' => 'border-color:{{VALUE}}',
				],
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control(
			'ma_cf7_button_border_radius',
			[
				'label' => esc_html__('Border Radius', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'separator' => 'before',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'default' => [
					'top' => 0,
					'right' => 0,
					'bottom' => 0,
					'left' => 0,
					'unit' => 'px'
				]
			]
		);

		$this->add_control(
			'button_padding',
			[
				'label' => esc_html__('Padding', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-form input[type="submit"]' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'default' => [
					'top' => 20,
					'right' => 50,
					'bottom' => 20,
					'left' => 50,
					'unit' => 'px'
				]
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Errors
		 */
		$this->start_controls_section(
			'ma_cf7_section_error_style',
			[
				'label' => esc_html__('Errors', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'ma_cf7_error_messages_heading',
			[
				'label' => esc_html__('Error Messages', 'master-addons'),
				'type' => Controls_Manager::HEADING,
				'condition' => [
					'ma_cf7_error_messages' => 'show',
				],
			]
		);

		$this->add_control(
			'ma_cf7_error_alert_text_color',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-not-valid-tip' => 'color: {{VALUE}}',
				],
				'condition' => [
					'ma_cf7_error_messages' => 'show',
				],
			]
		);

		$this->add_control(
			'ma_cf7_error_field_bg_color',
			[
				'label' => esc_html__('Background Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-not-valid-tip' => 'background: {{VALUE}}',
				],
				'condition' => [
					'ma_cf7_error_messages' => 'show',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'error_field_border',
				'label' => esc_html__('Border', 'master-addons'),
				'placeholder' => '1px',
				'default' => '1px',
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-not-valid-tip',
				'separator' => 'before',
				'condition' => [
					'ma_cf7_error_messages' => 'show',
				],
			]
		);

		$this->add_control(
			'ma_cf7_validation_errors_heading',
			[
				'label' => esc_html__('Validation Errors', 'master-addons'),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => [
					'ma_cf7_validation_errors' => 'show',
				],
			]
		);

		$this->add_control(
			'ma_cf7_validation_errors_bg_color',
			[
				'label' => esc_html__('Background Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-validation-errors' => 'background: {{VALUE}}',
				],
				'condition' => [
					'ma_cf7_validation_errors' => 'show',
				],
			]
		);

		$this->add_control(
			'ma_cf7_validation_errors_color',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} .master-addons-cf7 .wpcf7-validation-errors' => 'color: {{VALUE}}',
				],
				'condition' => [
					'ma_cf7_validation_errors' => 'show',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'validation_errors_border',
				'label' => esc_html__('Border', 'master-addons'),
				'placeholder' => '1px',
				'default' => '1px',
				'selector' => '{{WRAPPER}} .master-addons-cf7 .wpcf7-validation-errors',
				'separator' => 'before',
				'condition' => [
					'ma_cf7_validation_errors' => 'show',
				],
			]
		);

		$this->end_controls_section();

		// Help Docs section (links from config.php)
		$this->jltma_help_docs('contact-form-7');

		$this->upgrade_to_pro_message();
	}

	protected function render()
	{
		$settings = $this->get_settings();

		// if Contact Form 7 Missing
		if (!function_exists('wpcf7')) {
			Helper::jltma_elementor_plugin_missing_notice(array(
				'title'       => esc_html__('Contact Form 7 is Not Activated!', 'master-addons'),
				'description' => esc_html__('To use this widget, please install and activate the "Contact Form 7" plugin.', 'master-addons'),
			));
			return;
		}

		$this->add_render_attribute(
			'master-addons-cf7',
			'class',
			[
				'master-addons-cf7',
				'ma-cf',
				'ma-cf-' . esc_attr($settings['ma_cf7_layout_style']),
				'master-addons-cf7-' . esc_attr($this->get_id())
			]
		);

		if (function_exists('wpcf7')) {
			if (!empty($settings['ma_cf7_list'])) { ?>
				<div <?php echo $this->get_render_attribute_string('master-addons-cf7'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor render_attribute_string returns sanitized HTML attributes ?>>
					<?php echo do_shortcode('[contact-form-7 id="' . sanitize_text_field($settings['ma_cf7_list']) . '" ]'); ?>
				</div>
				<?php
			}
		}
	}

	protected function content_template()
	{
	}
}
