<?php
namespace WPT\MLSL\Blocks\StoreLocator;

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/store-locator/',
            [
                'render_callback' => [$this, 'render_callback'],
                'script'          => 'mlsl-store-locator-frontend',
            ]
        );
    }

    public function render_callback(
        $attributes,
        $content
    ) {
        $module_class = $this->get_attribute_value('module_class', $attributes);

        $attrs = $attributes;

        $map_style                    = $this->get_attribute_value('map_style', $attributes);
        $map_control_map_types        = $this->get_attribute_value('map_control_map_types', $attributes);
        $map_control_position         = $this->get_attribute_value('map_control_position', $attributes);
        $show_zoom_control            = $attributes['show_zoom_control'] ? 'on' : 'off';
        $zoom_control_position        = $this->get_attribute_value('zoom_control_position', $attributes);
        $show_street_view_control     = $attributes['show_street_view_control'] ? 'on' : 'off';
        $street_view_control_position = $this->get_attribute_value('street_view_control_position', $attributes);
        $show_fullscreen_control      = $attributes['show_fullscreen_control'] ? 'on' : 'off';
        $fullscreen_control_position  = $this->get_attribute_value('fullscreen_control_position', $attributes);
        $show_rotate_control          = $attributes['show_rotate_control'] ? 'on' : 'off';
        $rotate_control_position      = $this->get_attribute_value('rotate_control_position', $attributes);
        $no_results_text              = $attributes['no_results_text_message'];
        $show_scale_control           = $attributes['show_scale_control'] ? 'on' : 'off';
        $show_map_type_control        = $attributes['show_map_type_control'] ? 'on' : 'off';
        $show_info_window             = $attributes['show_info_window'] ? 'on' : 'off';
        $show_address                 = $attributes['show_address'] ? 'on' : 'off';
        $show_tel                     = $attributes['show_tel'] ? 'on' : 'off';
        $show_email                   = $attributes['show_email'] ? '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';
        $distance_unit                = $attributes['distance_unit'];
        $restrict_by_countries        = $attributes['restrict_by_countries'];
        $your_location_icon           = $attributes['location_icon']['url'];
        $directions_icon              = $attributes['directions_icon']['url'];
        $pin_icon                     = $attributes['address_icon']['url'];
        $email_icon                   = $attributes['email_icon']['url'];
        $tel_icon                     = $attributes['tel_icon']['url'];
        $layout                       = $this->get_attribute_value('map_and_listing_container_layout', $attributes);

        $cluster_markers           = $attributes['cluster_markers'] ? 'on' : 'off';
        $locations_per_page        = $this->get_attribute_value('locations_per_page', $attributes);
        $post_not_in               = $this->get_attribute_value('post__not_in', $attributes);
        $post_in                   = $this->get_attribute_value('post__in', $attributes);
        $filter_by_categories      = $this->get_attribute_value('filter_by_categories', $attributes) ? 'on' : 'off';
        $default_zoom              = $this->get_attribute_value('default_zoom', $attributes);
        $default_lat               = $this->get_attribute_value('default_lat', $attributes);
        $default_lat               = $this->get_attribute_value('default_lat', $attributes);
        $default_lng               = $this->get_attribute_value('default_lng', $attributes);
        $ask_to_share_location     = $this->get_attribute_value('ask_to_share_location', $attributes);
        $gesture_handling          = $this->get_attribute_value('gesture_handling', $attributes);
        $map_type                  = $this->get_attribute_value('map_type', $attributes);
        $direction_image_alt_title = $attributes['direction_image_alt_title'];

        $show_category_dropdown = $attributes['show_category_dropdown'] ? 'on' : 'off';
        $show_radius_dropdown   = $attributes['show_radius_dropdown'] ? 'on' : 'off';

        $categories_array = $this->get_attribute_value('categories', $attributes);
        $categories       = '';

        if (is_array($categories_array)) {
            $categories = implode(',', $categories_array);
        }

        if ($attrs['show_radius_dropdown'] == 'on') {
            $radiuses      = explode(',', $attrs['radius_options']);
            $radiuses      = array_map('trim', $radiuses);
            $radius_labels = array_map(
                function ($item) use ($attrs) {
                    return $item . ' ' . $attrs['distance_unit'];
                },
                $radiuses
            );

            $radius_options = array_combine($radiuses, $radius_labels);
        }

        $button_data_icon = '';

        $selectors = new Selectors(".{$module_class}");

        $styles = new Styles($selectors, $attributes);

        $this->style_form_container($styles);
        $this->style_input_container($styles);
        $this->style_search_input_fields($styles);

        $this->style_map_and_listing_parent_container($styles);
        $this->style_map_container($styles);
        $this->style_map($styles);
        $this->style_listing_container($styles);
        $this->style_single_location_parent_container($styles);
        $this->style_location_title($styles);
        $this->style_location_description($styles);
        $this->style_location_address($styles);
        $this->style_location_telephone($styles);
        $this->style_location_email($styles);
        $this->style_formatted_distance($styles);
        $this->style_empty_results_text($styles);

        $css = $styles->compute();

        $button_wrapper_class = '';
        $button_class         = '';

        $show_locations_on_page_load = 'off';

        ob_start();
        require $this->container['plugin_dir'] . '/resources/views/store-locator.php';
        $html = ob_get_clean();

        if (isset($_GET['context']) and ($_GET['context'] == 'edit')) {
            $html .= "<style>{$css}</style>";
        } else {
            wp_register_style('mlsl-store-locator-inline-css', false);
            wp_enqueue_style('mlsl-store-locator-inline-css');
            wp_add_inline_style('mlsl-store-locator-inline-css', $css);
        }

        return "<div class='{$module_class}'>{$html}</div>";
    }

    public function get_attribute_value(
        $name,
        $attributes
    ) {
        return isset($attributes[$name]) ? $attributes[$name] : '';
    }

    /**
     * Style form container
     */
    public function style_form_container(
        &$styles
    ) {
        $styles->addMargin('form_container', 'form_container_margin');
        $styles->addPadding('form_container', 'form_container_padding');
        $styles->addBackground('form_container', 'form_container_bg');
        $styles->addBorder('form_container', 'form_container_border');
        $styles->addBorderRadius('form_container', 'form_container_border_radius');
    }

    /**
     * Input container styles
     */
    public function style_input_container(
        &$styles
    ) {
        $styles->addMargin('input_container', 'input_container_margin');
        $styles->addPadding('input_container', 'input_container_padding');
    }

    /**
     * Search form input field styles
     */
    public function style_search_input_fields(
        &$styles
    ) {
        $styles->addMargin('input_and_dropdown', 'input_and_dropdown_margin');
        $styles->addPadding('input_and_dropdown', 'input_and_dropdown_padding');
        $styles->addTypography('input_and_dropdown', 'input_and_dropdown_typography');
        $styles->addBorder('input_and_dropdown', 'input_and_dropdown_border');
        $styles->addBorderRadius('input_and_dropdown', 'input_and_dropdown_border_radius');
        $styles->addStyle('input_and_dropdown', 'input_and_dropdown_bg', 'background-color');
    }

    public function style_map_and_listing_parent_container(
        &$styles
    ) {
        $styles->addPadding('map_and_listing_container', 'map_and_listing_container_padding');
        $styles->addMargin('map_and_listing_container', 'map_and_listing_container_margin');
        $styles->addBorder('map_and_listing_container', 'map_and_listing_container_border');
        $styles->addBorderRadius('map_and_listing_container', 'map_and_listing_container');

    }

    /**
     * Style map container
     */
    public function style_map_container(
        &$styles
    ) {
        $styles->addMargin('map_container', 'map_container_margin');
        $styles->addPadding('map_container', 'map_container_padding');
    }

    public function style_map(&$styles)
    {
        $styles->addStyle('map', 'map_height', 'height', [
            'postfix' => 'px',
        ]);
    }

    public function style_listing_container(&$styles)
    {
        $styles->addMargin('listing_container', 'listing_container_margin');
        $styles->addPadding('listing_container', 'listing_container_padding');
        $styles->addBorder('listing_container', 'listing_container_border');
        $styles->addBorderRadius('listing_container', 'listing_container_border_radius');

        $styles->addStyle('listing_container_inline', 'map_height', 'overflow:hidden; overflow-y:auto; max-height', [
            'postfix' => 'px',
        ]);

    }

    public function style_single_location_parent_container(&$styles)
    {
        $styles->addMargin('location_text_container', 'location_text_container_margin');
        $styles->addPadding('location_text_container', 'location_text_container_padding');
        $styles->addBorder('location_text_container', 'location_text_container_border');
        $styles->addBorderRadius('location_text_container', 'location_text_container_border_radius');

    }

    public function style_location_title(&$styles)
    {
        $styles->addMargin('title', 'title_margin');
        $styles->addPadding('title', 'title_padding');
        $styles->addTypography('title', 'title');
    }

    public function style_location_description(&$styles)
    {
        $styles->addMargin('description', 'description_margin');
        $styles->addPadding('css_selector_name', 'description_padding');
        $styles->addTypography('description', 'description');
    }

    public function style_location_address(&$styles)
    {
        $styles->addTypography('address', 'address_typography');
    }

    public function style_location_telephone(&$styles)
    {
        $styles->addTypography('telephone', 'telephone_typography');
    }

    public function style_location_email(&$styles)
    {
        $styles->addTypography('email', 'email_typography');
    }

    public function style_formatted_distance(&$styles)
    {
        $styles->addStyle('direction_image', 'direction_image_height', 'height', ['postfix' => 'px']);
        $styles->addTypography('formatted_distance', 'formatted_distance_typography');
    }

    public function style_empty_results_text(&$styles)
    {
        $styles->addMargin('empty_results_text', 'empty_results_margin');
        $styles->addPadding('empty_results_text', 'empty_results_padding');
        $styles->addTypography('empty_results_text', 'empty_results_text');
    }
}
