<?php

namespace WPT\MLSL\Blocks\LocationMap;

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

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

    /**
     * Init block
     */
    public function init() {
        wp_register_script(
            'mlsl-store-locator-frontend',
            $this->container['plugin_url'] . '/blocks/location-map/build/frontend.js',
            ['jquery'],
            $this->container['plugin_version'],
            true
        );
        register_block_type( $this->container['plugin_dir'] . '/blocks/location-map/', [
            'render_callback' => [$this, 'render_callback'],
            'script'          => 'mlsl-store-locator-frontend',
        ] );
    }

    public function render_callback( $attributes, $content ) {
        $module_class = $attributes['module_class'];
        $gesture_handling = $attributes['gesture_handling'];
        $map_type = $attributes['map_type'];
        $show_fullscreen_control = $attributes['show_fullscreen_control'];
        $fullscreen_control_position = $attributes['fullscreen_control_position'];
        $show_rotate_control = $attributes['show_rotate_control'];
        $rotate_control_position = $attributes['rotate_control_position'];
        $show_street_view_control = $attributes['show_street_view_control'];
        $street_view_control_position = $attributes['street_view_control_position'];
        $show_scale_control = $attributes['show_scale_control'];
        $show_map_type_control = $attributes['show_map_type_control'];
        $map_control_position = $attributes['map_control_position'];
        $show_zoom_control = $attributes['show_zoom_control'];
        $zoom_control_position = $attributes['zoom_control_position'];
        $marker_icon = $attributes['marker_icon']['url'];
        $map_control_map_types = $attributes['map_control_map_types'];
        $show_address = ( $attributes['show_address'] ? 'on' : 'off' );
        $show_direction_link = ( $attributes['show_direction_link'] ? 'on' : 'off' );
        $show_description = ( $attributes['show_description'] ? 'on' : 'off' );
        $show_title = ( $attributes['show_title'] ? 'on' : 'off' );
        $show_info_window = ( $attributes['show_info_window'] ? 'on' : 'off' );
        $marker_animation = $attributes['marker_animation'];
        $map_height = $attributes['map_height'];
        $map_style = $attributes['map_style'];
        $direction_image_alt_title = $attributes['direction_image_alt_title'];
        if ( $map_style ) {
            $map_style = wp_strip_all_tags( trim( $map_style ) );
        }
        $meta_keys = [
            '_mlsl_map|||0|lat',
            '_mlsl_map|||0|lng',
            '_mlsl_map|||0|zoom',
            '_mlsl_street_address',
            '_mlsl_locality',
            '_mlsl_postal_code',
            '_mlsl_region',
            '_mlsl_country'
        ];
        $lat = 0;
        $lng = 0;
        $zoom = 8;
        global $post;
        $post_id = $post->ID;
        $location_post = $post;
        $location_title = $post->post_title;
        if ( $attributes['use_current_post'] ) {
            if ( isset( $_GET['context'] ) and $_GET['context'] == 'edit' and isset( $_GET['post_id'] ) ) {
                $post_id = $_GET['post_id'];
                if ( $post->ID != $post_id ) {
                    $location_post = get_post( $post_id );
                    $location_title = $location_post->post_title;
                }
            }
        } else {
            if ( $attributes['location_post'] ) {
                $location_post = get_post( $attributes['location_post'] );
                if ( !$location_post ) {
                    return '';
                }
                $location_title = $location_post->post_title;
            }
        }
        if ( !$marker_icon ) {
            $image_id = carbon_get_post_meta( $location_post->ID, 'mlsl_marker' );
            if ( $image_id ) {
                $marker_icon = wp_get_attachment_image_url( $image_id, 'full' );
            }
        }
        $location_description = $location_post->post_excerpt;
        $meta_data = $this->container['locations_crud']->get_post_meta( $location_post->ID, $meta_keys );
        $address = [];
        $address_string = '';
        $directions_link = '';
        if ( isset( $meta_data ) ) {
            $lat = ( $meta_data['_mlsl_map|||0|lat'] ? esc_attr( strtr( $meta_data['_mlsl_map|||0|lat'], ',', '.' ) ) : 0 );
            $lng = ( $meta_data['_mlsl_map|||0|lng'] ? esc_attr( strtr( $meta_data['_mlsl_map|||0|lng'], ',', '.' ) ) : 0 );
            $zoom = ( $meta_data['_mlsl_map|||0|zoom'] ? esc_attr( strtr( $meta_data['_mlsl_map|||0|zoom'], ',', '.' ) ) : 8 );
            $directions_link = sprintf( 'https://www.google.com/maps/dir/?api=1&destination=%s,%s', $lat, $lng );
            $address = [];
            if ( $meta_data['_mlsl_street_address'] ) {
                $address[] = $meta_data['_mlsl_street_address'];
            }
            if ( $meta_data['_mlsl_locality'] ) {
                $address[] = $meta_data['_mlsl_locality'];
            }
            if ( $meta_data['_mlsl_postal_code'] ) {
                $address[] = $meta_data['_mlsl_postal_code'];
            }
            if ( $meta_data['_mlsl_region'] ) {
                $address[] = $meta_data['_mlsl_region'];
            }
            if ( $meta_data['_mlsl_country'] ) {
                $address[] = $meta_data['_mlsl_country'];
            }
            $address_string = implode( ', ', $address );
        }
        $info_window_html = '';
        if ( $show_info_window == 'on' ) {
            ob_start();
            require $this->container['plugin_dir'] . '/resources/views/partials/location-map-info-window.php';
            $info_window_html = ob_get_clean();
            $info_window_html = apply_filters(
                'mlsl_location_map_info_window_html',
                $info_window_html,
                $attributes,
                $location_title,
                $location_description,
                $address_string,
                $location_post
            );
        }
        $css = '';
        $selectors = new Selectors(".{$module_class}");
        $styles = new Styles($selectors, $attributes);
        $styles->add( 'map', sprintf( 'height: %spx', $attributes['map_height'] ) );
        $css = $styles->compute();
        ob_start();
        require $this->container['plugin_dir'] . '/resources/views/location-map.php';
        $html = ob_get_clean();
        if ( isset( $_GET['context'] ) and $_GET['context'] == 'edit' ) {
            $html .= "<style>{$css}</style>";
        } else {
            wp_register_style( 'location-map-inline-css', false );
            wp_enqueue_style( 'location-map-inline-css' );
            wp_add_inline_style( 'location-map-inline-css', $css );
        }
        return "<div class='wpt-mlsl-location-map-container {$module_class}'>{$html}</div>";
    }

}
