<?php
/**
 * Accordion widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Accordion
Description: Collapsible content sections.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
	return;
}

/**
 * Rsmspark_Content_Widgets_Accordion_Widget
 */
class Rsmspark_Content_Widgets_Accordion_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'accordion',
			__( 'RSM Accordion', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Expand and collapse content sections.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-list-view',
				'has_preview' => true,
				'keywords'    => array( 'accordion', 'collapse', 'faq', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * SiteOrigin wrapper chrome reset (default SO widget border).
	 */
	public function initialize() {
		parent::initialize();
		if ( class_exists( 'Rsmspark_Content_Widgets_Widget_Accordion', false ) ) {
			Rsmspark_Content_Widgets_Widget_Accordion::register_so_shell_hooks();
		}
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'accordion',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Sections', 'rsm-content-widgets' ),
				'fields' => array(
					'faq_mode' => array(
						'type'        => 'checkbox',
						'label'       => __( 'FAQ mode', 'rsm-content-widgets' ),
						'description' => __( 'Adds FAQ styling hooks and outputs FAQPage structured data for search engines.', 'rsm-content-widgets' ),
						'default'     => false,
					),
					'items' => array(
						'type'      => 'repeater',
						'label'     => __( 'Accordion items', 'rsm-content-widgets' ),
						'item_name' => __( 'Section', 'rsm-content-widgets' ),
						'fields'    => array(
							'item_type' => array(
								'type'    => 'select',
								'label'   => __( 'Row type', 'rsm-content-widgets' ),
								'default' => 'item',
								'options' => array(
									'item'     => __( 'Question / section', 'rsm-content-widgets' ),
									'category' => __( 'Category heading (FAQ)', 'rsm-content-widgets' ),
								),
							),
							'title' => array(
								'type'  => 'text',
								'label' => __( 'Section title', 'rsm-content-widgets' ),
							),
							'content' => array_merge(
								Rsmspark_Rich_Text_Registry::siteorigin_tinymce_field( Rsmspark_Rich_Text_Registry::PROFILE_BODY, 12 ),
								array(
									'label'          => __( 'Section content', 'rsm-content-widgets' ),
									'default'        => __( 'Section content goes here.', 'rsm-content-widgets' ),
									'rsm_rt_profile' => Rsmspark_Rich_Text_Registry::PROFILE_BODY,
								)
							),
						),
						'default'   => array(
							array(
								'title'   => __( 'First section', 'rsm-content-widgets' ),
								'content' => __( 'Content for the first section.', 'rsm-content-widgets' ),
							),
							array(
								'title'   => __( 'Second section', 'rsm-content-widgets' ),
								'content' => __( 'Content for the second section.', 'rsm-content-widgets' ),
							),
						),
					),
				),
			),
			'appearance' => array(
				'type'        => 'section',
				'label'       => __( 'Colors', 'rsm-content-widgets' ),
				'description' => __( 'Leave a color empty to use the theme default for that state.', 'rsm-content-widgets' ),
				'fields'      => array(
					'item_bg' => array(
						'type'        => 'color',
						'label'       => __( 'Item background', 'rsm-content-widgets' ),
						'description' => __( 'Background behind each accordion row.', 'rsm-content-widgets' ),
					),
					'trigger_bg' => array(
						'type'  => 'color',
						'label' => __( 'Selector background', 'rsm-content-widgets' ),
					),
					'trigger_color' => array(
						'type'  => 'color',
						'label' => __( 'Selector text', 'rsm-content-widgets' ),
					),
					'trigger_bg_hover' => array(
						'type'  => 'color',
						'label' => __( 'Selector hover background', 'rsm-content-widgets' ),
					),
					'trigger_color_hover' => array(
						'type'  => 'color',
						'label' => __( 'Selector hover text', 'rsm-content-widgets' ),
					),
					'trigger_bg_active' => array(
						'type'        => 'color',
						'label'       => __( 'Active selector background', 'rsm-content-widgets' ),
						'description' => __( 'Expanded section title. Empty uses the global primary color.', 'rsm-content-widgets' ),
					),
					'trigger_color_active' => array(
						'type'    => 'color',
						'label'   => __( 'Active selector text', 'rsm-content-widgets' ),
						'default' => '#ffffff',
					),
					'panel_bg' => array(
						'type'  => 'color',
						'label' => __( 'Expanded content background', 'rsm-content-widgets' ),
					),
					'panel_color' => array(
						'type'  => 'color',
						'label' => __( 'Expanded content text', 'rsm-content-widgets' ),
					),
				),
			),
			'borders' => array(
				'type'        => 'section',
				'label'       => __( 'Borders', 'rsm-content-widgets' ),
				'description' => __( 'Use the checkboxes to turn borders on or off. Colors and corners apply only when a border is enabled.', 'rsm-content-widgets' ),
				'fields'      => array(
					'item_border_enable' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Row borders', 'rsm-content-widgets' ),
						'default' => false,
					),
					'item_border_width' => array(
						'type'    => 'number',
						'label'   => __( 'Row border width (px)', 'rsm-content-widgets' ),
						'default' => 1,
						'min'     => 1,
						'max'     => 8,
					),
					'item_border_color' => array(
						'type'    => 'color',
						'label'   => __( 'Row border color', 'rsm-content-widgets' ),
						'default' => '#e5e7eb',
					),
					'item_border_radius' => array(
						'type'    => 'select',
						'label'   => __( 'Row corners', 'rsm-content-widgets' ),
						'default' => 'square',
						'options' => array(
							'square'  => __( 'Square', 'rsm-content-widgets' ),
							'rounded' => __( 'Rounded', 'rsm-content-widgets' ),
						),
					),
					'panel_border_enable' => array(
						'type'        => 'checkbox',
						'label'       => __( 'Open section divider', 'rsm-content-widgets' ),
						'default'     => false,
						'description' => __( 'Line between the open selector and its content.', 'rsm-content-widgets' ),
					),
					'panel_border_color' => array(
						'type'    => 'color',
						'label'   => __( 'Divider color', 'rsm-content-widgets' ),
						'default' => '#e5e7eb',
					),
					'container_border' => array(
						'type'        => 'checkbox',
						'label'       => __( 'Widget border', 'rsm-content-widgets' ),
						'default'     => false,
						'description' => __( 'Optional border around the entire accordion. Off by default.', 'rsm-content-widgets' ),
					),
					'container_border_width' => array(
						'type'    => 'number',
						'label'   => __( 'Widget border width (px)', 'rsm-content-widgets' ),
						'default' => 1,
						'min'     => 1,
						'max'     => 8,
					),
					'container_border_color' => array(
						'type'    => 'color',
						'label'   => __( 'Widget border color', 'rsm-content-widgets' ),
						'default' => '#e5e7eb',
					),
					'container_border_radius' => array(
						'type'    => 'select',
						'label'   => __( 'Widget corners', 'rsm-content-widgets' ),
						'default' => 'square',
						'options' => array(
							'square'  => __( 'Square', 'rsm-content-widgets' ),
							'rounded' => __( 'Rounded', 'rsm-content-widgets' ),
						),
					),
				),
			),
		)
		);
	}

	/**
	 * Repair SiteOrigin repeater rows that inherited the last default title for every item.
	 *
	 * @param array<string, mixed> $instance Widget instance.
	 * @return array<string, mixed>
	 */
	public function modify_instance( $instance ) {
		if ( empty( $instance['content']['items'] ) || ! is_array( $instance['content']['items'] ) ) {
			return $instance;
		}

		$items  = $instance['content']['items'];
		$titles = array();
		foreach ( $items as $item ) {
			$titles[] = is_array( $item ) ? trim( (string) ( $item['title'] ?? '' ) ) : '';
		}

		$second_label = __( 'Second section', 'rsm-content-widgets' );
		$unique       = array_unique( array_filter( $titles, 'strlen' ) );
		if ( count( $items ) < 2 || count( $unique ) !== 1 || $unique[0] !== $second_label ) {
			return $instance;
		}

		$defaults = array(
			array(
				'title'   => __( 'First section', 'rsm-content-widgets' ),
				'content' => __( 'Content for the first section.', 'rsm-content-widgets' ),
			),
			array(
				'title'   => $second_label,
				'content' => __( 'Content for the second section.', 'rsm-content-widgets' ),
			),
		);

		foreach ( $items as $index => $item ) {
			if ( ! is_array( $item ) || ! isset( $defaults[ $index ] ) ) {
				continue;
			}
			$items[ $index ]['title'] = $defaults[ $index ]['title'];
			if ( '' === trim( wp_strip_all_tags( (string) ( $item['content'] ?? '' ) ) ) ) {
				$items[ $index ]['content'] = $defaults[ $index ]['content'];
			}
		}

		$instance['content']['items'] = $items;

		return $instance;
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_dir( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_style_name( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_name( $instance ) {
		unset( $instance );
		return 'accordion.tpl';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args     Args.
	 * @return array<string, mixed>
	 */
	public function get_template_variables( $instance, $args ) {
		unset( $args );
		return Rsmspark_Content_Widgets_Widget_Accordion::prepare_template_vars(
			Rsmspark_Content_Widgets_Widget_Accordion::prepare_from_siteorigin_instance(
				is_array( $instance ) ? $instance : array()
			)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
			Rsmspark_Rich_Text_Assets::enqueue_admin();
		}
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_frontend_scripts( $instance ) {
		unset( $instance );
		if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
			Rsmspark_Rich_Text_Assets::enqueue_siteorigin_frontend();
		}
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}
}

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'accordion', __FILE__, 'Rsmspark_Content_Widgets_Accordion_Widget' );
}
