<?php

/**
 * Register resource service.
 *
 * @package Super_Advent_Calendar
 * @author  Verdant Studio
 * @since   1.0.0
 */
namespace SuperAdventCalendar\Services;

/**
 * Exit when accessed directly.
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
use SuperAdventCalendar\Interfaces\Services\ResourceServiceInterface;
/**
 * Register resource service.
 *
 * @since 1.0.0
 */
class ResourceService extends Service implements ResourceServiceInterface {
    /**
     * @inheritDoc
     */
    public function register() {
        add_action( 'init', array($this, 'register_block_scripts') );
    }

    /**
     * Get the modal markup.
     *
     * @since 1.1.0
     */
    public function get_modal_markup( $block_attributes ) : string {
        $attrs = $block_attributes;
        $modal_content_classes = '';
        $modal_content_style = '';
        if ( 'content-type-text' !== $attrs['contentType'] || !$attrs['showInModal'] ) {
            return '';
        }
        // Add extra modal classes.
        if ( !empty( $attrs['modalMaxWidth'] ) && in_array( $attrs['modalMaxWidth'], array(
            'lg',
            'xl',
            '2xl',
            '3xl',
            '4xl',
            'full'
        ), true ) ) {
            $modal_content_classes .= ' superac-dialog-content-max-width-' . $attrs['modalMaxWidth'];
        }
        if ( !empty( $attrs['modalFontSize'] ) && in_array( $attrs['modalFontSize'], array('small', 'large'), true ) ) {
            $modal_content_classes .= ' superac-dialog-content-font-' . $attrs['modalFontSize'];
        }
        // Add modal text color if attribute is set.
        if ( !empty( $attrs['modalTextColor'] ) ) {
            $modal_content_style .= 'color: ' . esc_attr( $attrs['modalTextColor'] ) . ';';
        }
        // Add modal background color if attribute is set.
        if ( !empty( $attrs['modalBackgroundColor'] ) ) {
            $modal_content_style .= 'background-color: ' . esc_attr( $attrs['modalBackgroundColor'] ) . ';';
        }
        return '<div class="superac-dialog-container" aria-hidden="true">
			<div class="superac-dialog-overlay" data-a11y-dialog-hide></div>
			<div class="superac-dialog-content' . esc_attr( $modal_content_classes ) . '" role="document"  style="' . esc_attr( $modal_content_style ) . '">
				<div class="superac-dialog-top-bar">
					<button class="superac-dialog-button" type="button" data-a11y-dialog-hide aria-label="Close dialog" style="' . esc_attr( $modal_content_style ) . '">
						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6M9 9l6 6"/></svg>
					</button>
				</div>
				<h1 class="superac-dialog-title"></h1>
				<div class="superac-dialog-body"></div>
			</div>
		</div>';
    }

    /**
     * Render the advent calendar day block because we need interactivity.
     *
     * @since 1.0.0
     *
     * @return string;
     */
    public function render_advent_calendar_day_block( $block_attributes ) : string {
        $attrs = $block_attributes;
        $post_id_attribute = 'data-post-id=' . esc_attr( get_the_ID() ) . '';
        // Only use the attributes we need as a data attribute, these are attributes we need before calling the API.
        $filtered_block_attributes = array_filter( $block_attributes, function ( $key ) use($block_attributes) {
            return in_array( $key, array(
                'blockId',
                'contentType',
                'showInModal',
                'scheduleStart',
                'scheduleEnd'
            ), true );
        }, ARRAY_FILTER_USE_KEY );
        $animation_classes = array(
            'none' => 'superac-day--animation-none',
            'flip' => 'superac-day--animation-flip',
        );
        // Encode the attributes as a JSON string using wp_json_encode.
        $data_attributes = wp_json_encode( $filtered_block_attributes );
        // Determine the block class based on the animation attribute value.
        if ( isset( $attrs['animation'] ) && isset( $animation_classes[$attrs['animation']] ) ) {
            $block_class = $animation_classes[$attrs['animation']];
        } else {
            $block_class = $animation_classes['none'];
        }
        // Add is-unlocked class based on scheduleStart and scheduleEnd logic.
        $now = current_time( 'timestamp' );
        $start = ( !empty( $attrs['scheduleStart'] ) ? strtotime( $attrs['scheduleStart'] ) : false );
        $end = ( !empty( $attrs['scheduleEnd'] ) ? strtotime( $attrs['scheduleEnd'] ) : false );
        if ( empty( $attrs['scheduleStart'] ) ) {
            if ( empty( $attrs['scheduleEnd'] ) || $now <= $end ) {
                $block_class .= ' is-unlocked';
            }
        } elseif ( empty( $attrs['scheduleEnd'] ) ) {
            if ( $now >= $start ) {
                $block_class .= ' is-unlocked';
            }
        } elseif ( $now >= $start && $now <= $end ) {
            $block_class .= ' is-unlocked';
        }
        $block_style_front = '';
        $block_style_back = '';
        // Add front text color if attribute is set.
        if ( !empty( $block_attributes['frontTextColor'] ) ) {
            $block_style_front .= 'color: ' . esc_attr( $block_attributes['frontTextColor'] ) . ';';
        }
        // Add front background color if attribute is set.
        if ( !empty( $block_attributes['frontBackgroundColor'] ) ) {
            $block_style_front .= 'background-color: ' . esc_attr( $block_attributes['frontBackgroundColor'] ) . ';';
        }
        // Add back text color if attribute is set.
        if ( !empty( $block_attributes['backTextColor'] ) ) {
            $block_style_back .= 'color: ' . esc_attr( $block_attributes['backTextColor'] ) . ';';
        }
        // Add back background color if attribute is set.
        if ( !empty( $block_attributes['backBackgroundColor'] ) ) {
            $block_style_back .= 'background-color: ' . esc_attr( $block_attributes['backBackgroundColor'] ) . ';';
        }
        // Get the block wrapper attributes for the front and back cards.
        $front_card_attributes = get_block_wrapper_attributes( array(
            'class' => 'superac-card superac-card--front',
            'style' => $block_style_front,
        ) );
        $back_card_attributes = get_block_wrapper_attributes( array(
            'class' => 'superac-card superac-card--back',
            'style' => $block_style_back,
        ) );
        // Construct the day output.
        $output = '';
        if ( 'content-type-link' === $attrs['contentType'] ) {
            $output .= '<a href="#" class="superac-day ' . esc_attr( $block_class ) . '" data-attributes=' . esc_attr( $data_attributes ) . ' ' . esc_attr( $post_id_attribute ) . '>';
        } else {
            $output .= '<div class="superac-day ' . esc_attr( $block_class ) . '" data-attributes=' . esc_attr( $data_attributes ) . ' ' . esc_attr( $post_id_attribute ) . '>';
        }
        $output .= '
			<div ' . $front_card_attributes . '>
				<div class="superac-card-title">
					' . esc_html( $attrs['frontTitle'] ) . '
				</div>
			</div>
			<div ' . $back_card_attributes . '>
				<div class="superac-card-title"></div>';
        // Only show the content in the back of the card when not in a modal.
        if ( !$attrs['showInModal'] && 'content-type-text' === $attrs['contentType'] ) {
            $output .= '<div class="superac-card-content"></div>';
        } else {
            switch ( $attrs['contentType'] ) {
                case 'content-type-text':
                    $output .= '<div class="superac-card-content">' . esc_html( apply_filters( 'superac_text_reopen_modal', __( 'Re-open this modal', 'super-advent-calendar' ), $attrs ) ) . '</div>';
                    break;
                case 'content-type-link':
                    $output .= '<div class="superac-card-content">' . esc_html( apply_filters( 'superac_text_reopen_link', __( 'Re-open this link', 'super-advent-calendar' ), $attrs ) ) . '</div>';
                    break;
                default:
                    break;
            }
        }
        $output .= '</div>';
        // Close either the <div> or <a> tag.
        $output .= ( 'content-type-link' === $attrs['contentType'] ? '</a>' : '</div>' );
        $output .= $this->get_modal_markup( $attrs );
        return $output;
    }

    /**
     * Register blocks.
     *
     * @since 1.0.0
     *
     * @return void;
     */
    public function register_block_scripts() {
        // Block editor is not available.
        if ( !function_exists( 'register_block_type' ) ) {
            return;
        }
        register_block_type( SUPER_ADVENT_CALENDAR_DIR_PATH . 'dist/blocks/advent-calendar/block.json' );
        register_block_type( SUPER_ADVENT_CALENDAR_DIR_PATH . 'dist/blocks/advent-calendar-day/block.json', array(
            'render_callback' => array($this, 'render_advent_calendar_day_block'),
        ) );
        // Pass the vsSacSettings variable to super-advent-calendar block in the editor and view script.
        $settings = array(
            'isPremium' => vs_sac_fs()->can_use_premium_code(),
            'nonce'     => wp_create_nonce( 'wp_rest' ),
        );
        $inline_js = 'var vsSacSettings = ' . wp_json_encode( $settings ) . ';';
        wp_add_inline_script( 'super-advent-calendar-advent-calendar-editor-script', $inline_js, 'before' );
        wp_add_inline_script( 'super-advent-calendar-advent-calendar-day-view-script', $inline_js, 'before' );
    }

}
