<?php
/**
 * Widget Name: My Account Page
 * Description: Renders the full WooCommerce My Account area (dashboard navigation
 *              and content, or the login/register forms when logged out) with a
 *              selectable navigation position, deep styling controls for the
 *              menu, content, buttons and form fields, and multiple styles.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.4.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

/**
 * Class Captwiki_Woo_Account_Page
 *
 * @since 1.4.0
 */
class Captwiki_Woo_Account_Page extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-account-page';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'My Account Page', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-my-account captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-woo-commerce' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'account', 'my account', 'dashboard', 'orders', 'login', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.4.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Available visual styles.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Classic', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Boxed Menu', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Pills', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Minimal', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Card', 'captain-widgets-kit' ),
			'style-6' => esc_html__( 'Style 6 – Sidebar Fill', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register controls.
	 *
	 * @since 1.4.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_nav_style_controls();
		$this->register_content_style_controls();
		$this->register_button_style_controls();
		$this->register_field_style_controls();
	}

	/**
	 * Content tab controls.
	 *
	 * @since 1.4.0
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'My Account', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'nav_position',
			array(
				'label'   => esc_html__( 'Navigation Position', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'left',
				'options' => array(
					'left'  => esc_html__( 'Left', 'captain-widgets-kit' ),
					'right' => esc_html__( 'Right', 'captain-widgets-kit' ),
					'top'   => esc_html__( 'Top (Horizontal)', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_responsive_control(
			'nav_width',
			array(
				'label'      => esc_html__( 'Navigation Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array( 'min' => 150, 'max' => 400 ),
					'%'  => array( 'min' => 15, 'max' => 50 ),
				),
				'default'    => array( 'unit' => 'px', 'size' => 240 ),
				'condition'  => array( 'nav_position!' => 'top' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-account' => '--captwiki-acc-nav: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'layout_gap',
			array(
				'label'      => esc_html__( 'Gap', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 80 ) ),
				'default'    => array( 'unit' => 'px', 'size' => 30 ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-account' => '--captwiki-acc-gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'editor_hint',
			array(
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => esc_html__( 'Logged-out visitors see the login / register forms here; logged-in users see the account dashboard.', 'captain-widgets-kit' ),
				'content_classes' => 'elementor-descriptor',
				'separator'       => 'before',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Navigation style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_nav_style_controls(): void {

		$link   = '{{WRAPPER}} .captwiki-account .woocommerce-MyAccount-navigation-link a';
		$active = '{{WRAPPER}} .captwiki-account .woocommerce-MyAccount-navigation-link.is-active a';
		$nav    = '{{WRAPPER}} .captwiki-account .woocommerce-MyAccount-navigation';

		$this->start_controls_section(
			'section_nav_style',
			array(
				'label' => esc_html__( 'Navigation', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'nav_bg',
			array(
				'label'     => esc_html__( 'Menu Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$nav => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'nav_typography',
				'selector' => $link,
			)
		);

		$this->start_controls_tabs( 'nav_tabs' );

		$this->start_controls_tab( 'nav_tab_normal', array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'nav_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $link => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'nav_item_bg',
			array(
				'label'     => esc_html__( 'Item Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $link => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'nav_tab_hover', array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'nav_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $link . ':hover' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'nav_item_bg_hover',
			array(
				'label'     => esc_html__( 'Item Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $link . ':hover' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'nav_tab_active', array( 'label' => esc_html__( 'Active', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'nav_color_active',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $active => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'nav_item_bg_active',
			array(
				'label'     => esc_html__( 'Item Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $active => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'nav_item_padding',
			array(
				'label'      => esc_html__( 'Item Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'separator'  => 'before',
				'selectors'  => array(
					$link => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'nav_radius',
			array(
				'label'      => esc_html__( 'Item Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$link => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content area style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_content_style_controls(): void {

		$content = '{{WRAPPER}} .captwiki-account .woocommerce-MyAccount-content';

		$this->start_controls_section(
			'section_content_style',
			array(
				'label' => esc_html__( 'Content', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'content_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$content                       => 'color: {{VALUE}};',
					$content . ' p'                => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'content_link_color',
			array(
				'label'     => esc_html__( 'Link Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$content . ' a:not(.button)' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'content_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					$content => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'content_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$content => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'content_border',
				'selector' => $content,
			)
		);

		$this->add_responsive_control(
			'content_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$content => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Button style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_button_style_controls(): void {

		$btn = '{{WRAPPER}} .captwiki-account .woocommerce .button, {{WRAPPER}} .captwiki-account .woocommerce-Button';

		$this->start_controls_section(
			'section_button_style',
			array(
				'label' => esc_html__( 'Buttons', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->start_controls_tabs( 'acc_btn_tabs' );

		$this->start_controls_tab( 'acc_btn_normal', array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'acc_btn_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'acc_btn_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'acc_btn_hover', array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) ) );

		$this->add_control(
			'acc_btn_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn . ':hover' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'acc_btn_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $btn . ':hover' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'acc_btn_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'separator'  => 'before',
				'selectors'  => array(
					$btn => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Form field style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_field_style_controls(): void {

		$field = '{{WRAPPER}} .captwiki-account input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="button"]), {{WRAPPER}} .captwiki-account textarea, {{WRAPPER}} .captwiki-account select';

		$this->start_controls_section(
			'section_field_style',
			array(
				'label' => esc_html__( 'Form Fields', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'field_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $field => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'field_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $field => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'      => 'field_border',
				'selector'  => $field,
				'separator' => 'before',
			)
		);

		$this->add_responsive_control(
			'field_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'selectors'  => array(
					$field => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'field_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$field => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.4.0
	 * @param string $message Notice text.
	 */
	private function captwiki_render_editor_notice( string $message ): void {

		if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		echo '<div class="captwiki-account captwiki-account--notice"><p>' . esc_html( $message ) . '</p></div>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.4.0
	 */
	protected function render(): void {

		if ( ! class_exists( 'WooCommerce' ) || ! shortcode_exists( 'woocommerce_my_account' ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'WooCommerce is required for the My Account Page widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings = $this->get_settings_for_display();

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = ! empty( $settings['widget_style'] ) && in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		wp_enqueue_style(
			'captwiki-woo-account-page-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-account-page/captwiki-woo-account-page-' . $style . '.css',
			array( 'captwiki-woo-account-page' ),
			CAPTWIKI_VERSION
		);

		$allowed_pos = array( 'left', 'right', 'top' );
		$position    = ! empty( $settings['nav_position'] ) && in_array( $settings['nav_position'], $allowed_pos, true ) ? $settings['nav_position'] : 'left';

		$wrapper_classes = array(
			'captwiki-account',
			'captwiki-' . $style,
			'captwiki-account--' . $position,
			'captwiki-widget-' . $this->get_id(),
		);

		echo '<div class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '">';
		echo do_shortcode( '[woocommerce_my_account]' );
		echo '</div>';
	}
}
