<?php

namespace WPT\MLSL\Blocks\LocationOpeningHours;

use WPT\MLSL\Blocks\Styles;
/**
 * Block.
 */
class Block {
    protected $container;

    /**
     * Constructor.
     */
    public function __construct( $container ) {
        $this->container = $container;
    }

    /**
     * Init block
     */
    public function init() {
        register_block_type( $this->container['plugin_dir'] . '/blocks/location-opening-hours/', [
            'render_callback' => [$this, 'render_callback'],
        ] );
    }

    public function render_callback( $attributes, $content ) {
        $module_class = $attributes['module_class'];
        $show_no_location_error = 'off';
        $no_location_found_error = 'Location Not Found';
        $time_format = $attributes['time_format'];
        $show_closed_days = $attributes['show_closed_days'];
        $first_day_of_week = $attributes['first_day_of_week'];
        $odd_row_bg = $attributes['odd_row_bg'];
        $even_row_bg = $attributes['even_row_bg'];
        $closed_label = $attributes['closed_label'];
        global $post;
        $post_id = $post->ID;
        if ( $attributes['use_current_post'] ) {
            if ( isset( $_GET['context'] ) and $_GET['context'] == 'edit' and isset( $_GET['post_id'] ) ) {
                $post_id = $_GET['post_id'];
            }
        } else {
            if ( $attributes['location_post'] ) {
                $post_id = $attributes['location_post'];
            }
        }
        $opening_hours = $this->container['locations']->get_opening_hours( $post_id );
        $opening_hours_map = [];
        foreach ( $opening_hours as $opening_hour ) {
            // phpcs:ignore
            $opening_hour['opens'] = ( $time_format == '24_hour' ? $opening_hour['opens'] : date( 'h:i A', strtotime( $opening_hour['opens'] ) ) );
            // phpcs:ignore
            $opening_hour['closes'] = ( $time_format == '24_hour' ? $opening_hour['closes'] : date( 'h:i A', strtotime( $opening_hour['closes'] ) ) );
            $opening_hours_map[$opening_hour['day']][] = $opening_hour;
        }
        if ( $first_day_of_week == 'monday' ) {
            $days = [
                'Monday',
                'Tuesday',
                'Wednesday',
                'Thursday',
                'Friday',
                'Saturday',
                'Sunday'
            ];
        } else {
            $days = [
                'Sunday',
                'Monday',
                'Tuesday',
                'Wednesday',
                'Thursday',
                'Friday',
                'Saturday'
            ];
        }
        $css = '';
        ob_start();
        require $this->container['plugin_dir'] . '/resources/views/location-opening-hours.php';
        $html = ob_get_clean();
        if ( isset( $_GET['context'] ) and $_GET['context'] == 'edit' ) {
            $html .= "<style>{$css}</style>";
        } else {
            wp_register_style( 'location-opening-hours-inline-css', false );
            wp_enqueue_style( 'location-opening-hours-inline-css' );
            wp_add_inline_style( 'location-opening-hours-inline-css', $css );
        }
        return "<div class='{$module_class}'>{$html}</div>";
    }

}
