<?php
/**
 * SiteOrigin RSM Footer widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Footer
Description: Multi-column footer with brand, social icon links, and categorized link columns.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

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

/**
 * Rsmspark_Content_Widgets_Footer_Widget
 */
class Rsmspark_Content_Widgets_Footer_Widget extends SiteOrigin_Widget {

	/**
	 * @param array<string, mixed> $args     Sidebar args.
	 * @param array<string, mixed> $instance Widget instance.
	 * @return void
	 */
	public function widget( $args, $instance ) {
		$config = Rsmspark_Content_Widgets_Footer_Config::from_siteorigin_instance( is_array( $instance ) ? $instance : array() );
		if ( ! Rsmspark_Content_Widgets_Footer_Visibility::instance_is_visible( $config, (string) ( $args['id'] ?? '' ) ) ) {
			return;
		}

		parent::widget( $args, $instance );
	}

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'footer',
			__( 'RSM Footer', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Build footers with brand graphics, social icons, and link columns.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-align-wide',
				'has_preview' => true,
				'keywords'    => array( 'footer', 'links', 'social', 'RSM', 'RSMSpark' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);

		$this->register_frontend_styles(
			array(
				array(
					'rsmspark-content-widgets-footer-fontawesome',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/vendor/fontawesome/css/all.min.css',
					array(),
					'6.5.2',
				),
				array(
					'rsmspark-content-widgets-footer',
					RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/rsm-footer.css',
					array( 'rsmspark-content-widgets-footer-fontawesome', 'dashicons' ),
					RSMSPARK_CONTENT_WIDGETS_VERSION,
				),
			)
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		$demo = Rsmspark_Content_Widgets_Footer_Config::demo_instance();

		return array(
			'import_export' => array(
				'type'   => 'section',
				'label'  => __( 'Import / export JSON', 'rsm-content-widgets' ),
				'fields' => array(
					'export_json' => array(
						'type'        => 'textarea',
						'label'       => __( 'Export JSON', 'rsm-content-widgets' ),
						'rows'        => 8,
						'description' => __( 'Generate JSON from the current footer settings, then copy or save the text below.', 'rsm-content-widgets' )
							. '<p class="rsmspark-cw-footer-json-actions"><button type="button" class="button rsmspark-cw-footer-export-json">'
							. esc_html__( 'Export JSON', 'rsm-content-widgets' )
							. '</button></p>',
					),
					'import_json' => array(
						'type'        => 'textarea',
						'label'       => __( 'Import JSON', 'rsm-content-widgets' ),
						'rows'        => 8,
						'description' => __( 'Paste an RSM Footer export (widget or block JSON), then click Apply import.', 'rsm-content-widgets' )
							. '<p class="rsmspark-cw-footer-json-actions"><button type="button" class="button button-primary rsmspark-cw-footer-import-json">'
							. esc_html__( 'Apply import', 'rsm-content-widgets' )
							. '</button></p>',
					),
				),
			),
			'region' => array(
				'type'        => 'text',
				'label'       => __( 'Region slug', 'rsm-content-widgets' ),
				'description' => __( 'Optional label for admin reference (e.g. default, shop). Visibility rules below control where this footer appears.', 'rsm-content-widgets' ),
				'default'     => $demo['region'],
			),
			'show_on_section' => array(
				'type'        => 'section',
				'label'       => __( 'Show footer on page types', 'rsm-content-widgets' ),
				'description' => __( 'Leave all unchecked to show everywhere (unless hidden below).', 'rsm-content-widgets' ),
				'fields'      => Rsmspark_Content_Widgets_Footer_Visibility::page_type_checkbox_fields( 'show_on' ),
			),
			'hide_on_section' => array(
				'type'        => 'section',
				'label'       => __( 'Hide footer on page types', 'rsm-content-widgets' ),
				'description' => __( 'Hide this footer when the current page matches any checked type.', 'rsm-content-widgets' ),
				'fields'      => Rsmspark_Content_Widgets_Footer_Visibility::page_type_checkbox_fields( 'hide_on' ),
			),
			'show_on' => array(
				'type'        => 'select',
				'multiple'    => true,
				'label'       => __( 'Show footer on page types (legacy)', 'rsm-content-widgets' ),
				'description' => __( 'Deprecated — use the checkboxes above. Kept for imported instances.', 'rsm-content-widgets' ),
				'options'     => Rsmspark_Content_Widgets_Footer_Visibility::page_type_options(),
				'default'     => array(),
				'hide'        => true,
			),
			'hide_on' => array(
				'type'        => 'select',
				'multiple'    => true,
				'label'       => __( 'Hide footer on page types (legacy)', 'rsm-content-widgets' ),
				'options'     => Rsmspark_Content_Widgets_Footer_Visibility::page_type_options(),
				'default'     => array(),
				'hide'        => true,
			),
			'show_on_post_types' => array(
				'type'        => 'select',
				'multiple'    => true,
				'label'       => __( 'Show footer on post types', 'rsm-content-widgets' ),
				'description' => __( 'Applies on single posts/pages of the selected types.', 'rsm-content-widgets' ),
				'options'     => Rsmspark_Content_Widgets_Footer_Visibility::post_type_options(),
				'default'     => array(),
			),
			'hide_on_post_types' => array(
				'type'        => 'select',
				'multiple'    => true,
				'label'       => __( 'Hide footer on post types', 'rsm-content-widgets' ),
				'options'     => Rsmspark_Content_Widgets_Footer_Visibility::post_type_options(),
				'default'     => array(),
			),
			'brand' => array(
				'type'   => 'section',
				'label'  => __( 'Brand', 'rsm-content-widgets' ),
				'fields' => array(
					'logo' => array(
						'type'  => 'media',
						'label' => __( 'Logo', 'rsm-content-widgets' ),
					),
					'name' => array(
						'type'    => 'text',
						'label'   => __( 'Brand name', 'rsm-content-widgets' ),
						'default' => $demo['brand']['name'],
					),
					'description' => array(
						'type'    => 'textarea',
						'label'   => __( 'Description', 'rsm-content-widgets' ),
						'default' => $demo['brand']['description'],
					),
				),
			),
			'social' => array(
				'type'       => 'repeater',
				'label'      => __( 'Social icon links', 'rsm-content-widgets' ),
				'item_name'  => __( 'Social link', 'rsm-content-widgets' ),
				'item_label' => array(
					'selector'     => "[id*='social'][id*='label']",
					'update_event' => 'change',
					'value_method' => 'val',
				),
				'fields'     => array(
					'icon' => array(
						'type'        => 'icon',
						'label'       => __( 'Icon', 'rsm-content-widgets' ),
						'description' => __( 'Font Awesome brand icons work best (e.g. fa-brands fa-github).', 'rsm-content-widgets' ),
						'default'     => 'fa-brands fa-github',
					),
					'url' => array(
						'type'  => 'link',
						'label' => __( 'URL', 'rsm-content-widgets' ),
					),
					'label' => array(
						'type'  => 'text',
						'label' => __( 'Accessible label', 'rsm-content-widgets' ),
					),
				),
				'default'    => $demo['social'],
			),
			'columns' => array(
				'type'       => 'repeater',
				'label'      => __( 'Footer columns', 'rsm-content-widgets' ),
				'item_name'  => __( 'Column', 'rsm-content-widgets' ),
				'item_label' => array(
					'selector'     => "[id*='columns'][id*='title']",
					'update_event' => 'change',
					'value_method' => 'val',
				),
				'fields'     => array(
					'title' => array(
						'type'  => 'text',
						'label' => __( 'Column title', 'rsm-content-widgets' ),
					),
					'column_type' => array(
						'type'    => 'select',
						'label'   => __( 'Column type', 'rsm-content-widgets' ),
						'default' => 'link_list',
						'options' => array(
							'link_list' => __( 'Link list', 'rsm-content-widgets' ),
							'text'      => __( 'Text block', 'rsm-content-widgets' ),
						),
						'state_emitter' => array(
							'callback' => 'select',
							'args'     => array( 'column_type_{$repeater}' ),
						),
					),
					'body' => array(
						'type'    => 'textarea',
						'label'   => __( 'Text content', 'rsm-content-widgets' ),
						'state_handler' => array(
							'column_type_{$repeater}[text]' => array( 'show' ),
							'_else[column_type_{$repeater}]' => array( 'hide' ),
						),
					),
					'link_items' => array(
						'type'       => 'repeater',
						'label'      => __( 'Links', 'rsm-content-widgets' ),
						'state_handler' => array(
							'column_type_{$repeater}[link_list]' => array( 'show' ),
							'_else[column_type_{$repeater}]' => array( 'hide' ),
						),
						'item_name'  => __( 'Link', 'rsm-content-widgets' ),
						'item_label' => array(
							'selector'     => "[id*='link_items'][id*='label']",
							'update_event' => 'change',
							'value_method' => 'val',
						),
						'fields'     => array(
							'label' => array(
								'type'  => 'text',
								'label' => __( 'Label', 'rsm-content-widgets' ),
							),
							'url' => array(
								'type'  => 'link',
								'label' => __( 'URL', 'rsm-content-widgets' ),
							),
						),
					),
					'show_on' => array(
						'type'        => 'select',
						'multiple'    => true,
						'label'       => __( 'Show on page types', 'rsm-content-widgets' ),
						'description' => __( 'Leave empty to show everywhere (unless hidden below).', 'rsm-content-widgets' ),
						'options'     => Rsmspark_Content_Widgets_Footer_Visibility::page_type_options(),
						'default'     => array(),
					),
					'hide_on' => array(
						'type'        => 'select',
						'multiple'    => true,
						'label'       => __( 'Hide on page types', 'rsm-content-widgets' ),
						'description' => __( 'Hide this column when the current page matches any selected type.', 'rsm-content-widgets' ),
						'options'     => Rsmspark_Content_Widgets_Footer_Visibility::page_type_options(),
						'default'     => array(),
					),
				),
				'default'    => $demo['columns'],
			),
			'copyright' => array(
				'type'  => 'text',
				'label' => __( 'Copyright', 'rsm-content-widgets' ),
				'default' => $demo['copyright'],
			),
			'design' => array(
				'type'   => 'section',
				'label'  => __( 'Design', 'rsm-content-widgets' ),
				'fields' => array(
					'background_color' => array(
						'type'    => 'color',
						'label'   => __( 'Background', 'rsm-content-widgets' ),
						'default' => $demo['design']['background_color'],
					),
					'text_color' => array(
						'type'    => 'color',
						'label'   => __( 'Body text', 'rsm-content-widgets' ),
						'default' => $demo['design']['text_color'],
					),
					'heading_color' => array(
						'type'    => 'color',
						'label'   => __( 'Headings', 'rsm-content-widgets' ),
						'default' => $demo['design']['heading_color'],
					),
					'link_color' => array(
						'type'    => 'color',
						'label'   => __( 'Links', 'rsm-content-widgets' ),
						'default' => $demo['design']['link_color'],
					),
					'link_hover_color' => array(
						'type'    => 'color',
						'label'   => __( 'Link hover', 'rsm-content-widgets' ),
						'default' => $demo['design']['link_hover_color'],
					),
					'padding_y' => array(
						'type'    => 'slider',
						'label'   => __( 'Vertical padding', 'rsm-content-widgets' ),
						'min'     => 0,
						'max'     => 120,
						'default' => $demo['design']['padding_y'],
					),
					'column_gap' => array(
						'type'    => 'slider',
						'label'   => __( 'Column gap', 'rsm-content-widgets' ),
						'min'     => 0,
						'max'     => 80,
						'default' => $demo['design']['column_gap'],
					),
					'text_scale' => array(
						'type'        => 'slider',
						'label'       => __( 'Body text scale', 'rsm-content-widgets' ),
						'description' => __( 'Scales links, descriptions, and legal text. Column titles and the brand name keep their accent sizes.', 'rsm-content-widgets' ),
						'min'         => 50,
						'max'         => 135,
						'integer'     => true,
						'default'     => $demo['design']['text_scale'],
					),
					'max_width' => array(
						'type'        => 'number',
						'label'       => __( 'Max width (px)', 'rsm-content-widgets' ),
						'description' => __( '0 = use theme container width.', 'rsm-content-widgets' ),
						'default'     => 0,
					),
					'placement' => array(
						'type'    => 'select',
						'label'   => __( 'Placement', 'rsm-content-widgets' ),
						'default' => $demo['design']['placement'],
						'options' => array(
							'static'     => __( 'Static (default)', 'rsm-content-widgets' ),
							'sticky_bar' => __( 'Sticky bar (fixed to bottom)', 'rsm-content-widgets' ),
						),
						'state_emitter' => array(
							'callback' => 'select',
							'args'     => array( 'footer_placement' ),
						),
					),
					'reveal_mode' => array(
						'type'    => 'select',
						'label'   => __( 'Sticky reveal', 'rsm-content-widgets' ),
						'default' => $demo['design']['reveal_mode'],
						'options' => array(
							'always'      => __( 'Always visible', 'rsm-content-widgets' ),
							'scroll_up'   => __( 'Reveal on scroll up (hide on scroll down)', 'rsm-content-widgets' ),
							'scroll_down' => __( 'Hide on scroll up (reveal on scroll down)', 'rsm-content-widgets' ),
						),
						'state_handler' => array(
							'footer_placement[sticky_bar]' => array( 'show' ),
							'_else[footer_placement]'      => array( 'hide' ),
						),
					),
					'sticky_compact' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Compact sticky bar', 'rsm-content-widgets' ),
						'default' => ! empty( $demo['design']['sticky_compact'] ),
						'state_handler' => array(
							'footer_placement[sticky_bar]' => array( 'show' ),
							'_else[footer_placement]'      => array( 'hide' ),
						),
					),
					'back_to_top' => array(
						'type'    => 'checkbox',
						'label'   => __( 'Back to top button', 'rsm-content-widgets' ),
						'default' => ! empty( $demo['design']['back_to_top'] ),
					),
				),
			),
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args       Sidebar args.
	 * @return string
	 */
	public function get_template_variables( $instance, $args ) {
		unset( $args );
		$config = Rsmspark_Content_Widgets_Footer_Config::from_siteorigin_instance( is_array( $instance ) ? $instance : array() );
		if ( ! Rsmspark_Content_Widgets_Footer_Visibility::instance_is_visible( $config ) ) {
			return array(
				'has_brand'     => false,
				'has_columns'   => false,
				'has_copyright' => false,
			);
		}

		return Rsmspark_Content_Widgets_Footer_Config::prepare_view_model( $config );
	}

	/**
	 * @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_template_name( $instance ) {
		unset( $instance );
		return 'footer.tpl';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		if ( class_exists( 'Rsmspark_Content_Widgets_Assets', false ) ) {
			Rsmspark_Content_Widgets_Assets::enqueue_export_json_ui_assets();
		}

		wp_enqueue_style(
			'rsmspark-cw-admin-widgets',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/css/admin-widgets.css',
			array( 'rsmspark-cw-export-json-ui' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION
		);

		wp_enqueue_script(
			'rsmspark-cw-footer-admin',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-footer-admin.js',
			array( 'jquery', 'rsmspark-cw-export-json-ui' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION,
			true
		);

		wp_localize_script(
			'rsmspark-cw-footer-admin',
			'rsmsparkCwFooterAdmin',
			array(
				'ajaxUrl'  => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'rsmspark_cw_footer_admin' ),
				'version'  => RSMSPARK_CONTENT_WIDGETS_VERSION,
				'i18n'     => array(
					'exportLabel'      => __( 'Generated export (also copied to your clipboard):', 'rsm-content-widgets' ),
					'exportPanelTitle' => __( 'JSON export ready', 'rsm-content-widgets' ),
					'exportReady'      => __( 'Export generated below the Export JSON button. It was also copied to your clipboard.', 'rsm-content-widgets' ),
					'importError'   => __( 'Paste valid RSM Footer JSON export data (widget or block export).', 'rsm-content-widgets' ),
					'importApplied' => __( 'Footer import applied. Review Social and Columns repeaters if counts changed.', 'rsm-content-widgets' ),
					'confirm'       => __( 'Apply this footer import? Current settings will be overwritten.', 'rsm-content-widgets' ),
					'requestError'  => __( 'Could not process footer JSON. Try again.', 'rsm-content-widgets' ),
				),
			)
		);
	}
}

siteorigin_widget_register( 'footer', __FILE__, 'Rsmspark_Content_Widgets_Footer_Widget' );
