<?php
/**
 * Classic WordPress widget for RSM Footer.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_WP_Widget_Footer
 */
class Rsmspark_Content_Widgets_WP_Widget_Footer extends WP_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'rsm_footer',
			__( 'RSM Footer', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Multi-column footer with brand, social icons, and categorized links.', 'rsm-content-widgets' ),
				'classname'   => 'widget_rsm_footer rsmspark-footer-widget-wrap',
			)
		);
	}

	/**
	 * @param array<string, mixed> $args     Sidebar args.
	 * @param array<string, mixed> $instance Widget instance.
	 * @return void
	 */
	public function widget( $args, $instance ) {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Widget_Footer', false ) ) {
			require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-footer.php';
		}

		$html = Rsmspark_Content_Widgets_Widget_Footer::render(
			self::instance_to_config( $instance ),
			(string) ( $args['id'] ?? '' )
		);
		if ( '' === trim( $html ) ) {
			return;
		}

		echo wp_kses_post( $args['before_widget'] );
		echo wp_kses( $html, Rsmspark_Content_Widgets_Kses::allowed_html() );
		echo wp_kses_post( $args['after_widget'] );
	}

	/**
	 * @param array<string, mixed> $instance Widget instance.
	 * @return void
	 */
	public function form( $instance ) {
		$config = self::instance_to_config( $instance );
		$brand  = $config['brand'];
		$show_on = Rsmspark_Content_Widgets_Footer_Visibility::read_page_type_flags( $instance, 'show_on' );
		$hide_on = Rsmspark_Content_Widgets_Footer_Visibility::read_page_type_flags( $instance, 'hide_on' );
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'brand_name' ) ); ?>"><?php esc_html_e( 'Brand name', 'rsm-content-widgets' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'brand_name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'brand_name' ) ); ?>" type="text" value="<?php echo esc_attr( $brand['name'] ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'brand_description' ) ); ?>"><?php esc_html_e( 'Brand description', 'rsm-content-widgets' ); ?></label>
			<textarea class="widefat" rows="3" id="<?php echo esc_attr( $this->get_field_id( 'brand_description' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'brand_description' ) ); ?>"><?php echo esc_textarea( $brand['description'] ); ?></textarea>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'copyright' ) ); ?>"><?php esc_html_e( 'Copyright', 'rsm-content-widgets' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'copyright' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'copyright' ) ); ?>" type="text" value="<?php echo esc_attr( $config['copyright'] ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'region' ) ); ?>"><?php esc_html_e( 'Region slug', 'rsm-content-widgets' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'region' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'region' ) ); ?>" type="text" value="<?php echo esc_attr( $config['region'] ); ?>" />
		</p>
		<p><strong><?php esc_html_e( 'Show footer on page types', 'rsm-content-widgets' ); ?></strong></p>
		<p class="description"><?php esc_html_e( 'Leave all unchecked to show everywhere (unless hidden below).', 'rsm-content-widgets' ); ?></p>
		<?php self::render_page_type_checkboxes( $this, 'show_on', $show_on ); ?>
		<p><strong><?php esc_html_e( 'Hide footer on page types', 'rsm-content-widgets' ); ?></strong></p>
		<?php self::render_page_type_checkboxes( $this, 'hide_on', $hide_on ); ?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'config_json' ) ); ?>"><?php esc_html_e( 'Advanced config (JSON)', 'rsm-content-widgets' ); ?></label>
			<textarea class="widefat code" rows="8" id="<?php echo esc_attr( $this->get_field_id( 'config_json' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'config_json' ) ); ?>" placeholder="<?php esc_attr_e( 'Optional full footer JSON (columns, social links, design).', 'rsm-content-widgets' ); ?>"><?php echo esc_textarea( (string) ( $instance['config_json'] ?? '' ) ); ?></textarea>
		</p>
		<p class="description"><?php esc_html_e( 'Set the theme footer to one column, then add this widget. Use SiteOrigin for full column editing.', 'rsm-content-widgets' ); ?></p>
		<?php
	}

	/**
	 * @param WP_Widget            $widget Widget object.
	 * @param string               $field  show_on|hide_on.
	 * @param array<int, string>   $selected Selected slugs.
	 * @return void
	 */
	private static function render_page_type_checkboxes( WP_Widget $widget, $field, array $selected ) {
		foreach ( Rsmspark_Content_Widgets_Footer_Visibility::page_type_options() as $slug => $label ) {
			$key = Rsmspark_Content_Widgets_Footer_Visibility::checkbox_field_name( $field, $slug );
			?>
			<p>
				<label for="<?php echo esc_attr( $widget->get_field_id( $key ) ); ?>">
					<input
						type="checkbox"
						id="<?php echo esc_attr( $widget->get_field_id( $key ) ); ?>"
						name="<?php echo esc_attr( $widget->get_field_name( $key ) ); ?>"
						value="1"
						<?php checked( in_array( $slug, $selected, true ) ); ?>
					/>
					<?php echo esc_html( $label ); ?>
				</label>
			</p>
			<?php
		}
	}

	/**
	 * @param array<string, mixed> $new_instance New instance.
	 * @param array<string, mixed> $old_instance Old instance.
	 * @return array<string, mixed>
	 */
	public function update( $new_instance, $old_instance ) {
		unset( $old_instance );

		$stored = array(
			'brand_name'        => sanitize_text_field( (string) ( $new_instance['brand_name'] ?? '' ) ),
			'brand_description' => sanitize_textarea_field( (string) ( $new_instance['brand_description'] ?? '' ) ),
			'copyright'         => sanitize_text_field( (string) ( $new_instance['copyright'] ?? '' ) ),
			'region'            => sanitize_text_field( (string) ( $new_instance['region'] ?? '' ) ),
			'config_json'       => (string) ( $new_instance['config_json'] ?? '' ),
			'show_on'           => Rsmspark_Content_Widgets_Footer_Visibility::collect_page_type_flags( $new_instance, 'show_on' ),
			'hide_on'           => Rsmspark_Content_Widgets_Footer_Visibility::collect_page_type_flags( $new_instance, 'hide_on' ),
		);

		foreach ( array_keys( Rsmspark_Content_Widgets_Footer_Visibility::page_type_options() ) as $slug ) {
			foreach ( array( 'show_on', 'hide_on' ) as $field ) {
				$key = Rsmspark_Content_Widgets_Footer_Visibility::checkbox_field_name( $field, $slug );
				unset( $stored[ $key ] );
			}
		}

		return $stored;
	}

	/**
	 * @param array<string, mixed> $instance Widget instance.
	 * @return array<string, mixed>
	 */
	public static function instance_to_config( array $instance ) {
		if ( ! empty( $instance['config_json'] ) ) {
			$config = Rsmspark_Content_Widgets_Footer_Config::sanitize_instance( $instance['config_json'] );
		} else {
			$config = Rsmspark_Content_Widgets_Footer_Config::demo_instance();
		}

		if ( isset( $instance['brand_name'] ) ) {
			$config['brand']['name'] = sanitize_text_field( (string) $instance['brand_name'] );
		}
		if ( isset( $instance['brand_description'] ) ) {
			$config['brand']['description'] = sanitize_textarea_field( (string) $instance['brand_description'] );
		}
		if ( isset( $instance['copyright'] ) ) {
			$config['copyright'] = sanitize_text_field( (string) $instance['copyright'] );
		}

		return Rsmspark_Content_Widgets_Footer_Config::sanitize_instance(
			array_merge(
				$config,
				array(
					'region'  => $instance['region'] ?? $config['region'],
					'show_on' => Rsmspark_Content_Widgets_Footer_Visibility::read_page_type_flags( $instance, 'show_on' ),
					'hide_on' => Rsmspark_Content_Widgets_Footer_Visibility::read_page_type_flags( $instance, 'hide_on' ),
				)
			)
		);
	}
}
