<?php
/**
 * @package     productive-forms
 * @author      productiveminds.com
 * @copyright   productiveminds.com
*/

function productive_forms_render_google_map( $cpt_section_args ) {
    
    $section_content_header_is_show_section_header = 1;
    $section_title = esc_html__('How to Locate Us', 'productive-forms');
    $section_title_html_tag = 'h2';
    $section_intro = esc_html__('Leave heading fields empty to hide the heading.', 'productive-forms');
    $section_header_alignment = '';
    $section_content_settings_unique_id = 'section_content_unique_id_' . rand();
    $section_initiator = 'std';
    $section_gtbg_align = '';
    $section_content_wrap_in_small_screen = 'wrap_to_one_column_in_small_screen';
    $google_map_display_physical_address = 1;
    
    if ( isset( $cpt_section_args['section_content_header_is_show_section_header'] ) ) {
        $section_content_header_is_show_section_header = $cpt_section_args['section_content_header_is_show_section_header'];
    }
    if ( isset( $cpt_section_args['section_title'] ) ) {
        $section_title = $cpt_section_args['section_title'];
    }
    if ( isset( $cpt_section_args['section_title_html_tag'] ) ) {
        $section_title_html_tag = $cpt_section_args['section_title_html_tag'];
    }
    if ( isset( $cpt_section_args['section_intro'] ) ) {
        $section_intro = $cpt_section_args['section_intro'];
    }
    if ( isset( $cpt_section_args['section_header_alignment'] ) ) {
        $section_header_alignment = $cpt_section_args['section_header_alignment'];
    }
    if ( isset( $cpt_section_args['google_map_display_physical_address'] ) ) {
        $google_map_display_physical_address = intval( $cpt_section_args['google_map_display_physical_address'] );
    }
    if ( isset( $cpt_section_args['section_content_settings_unique_id'] ) ) {
        $section_content_settings_unique_id = $cpt_section_args['section_content_settings_unique_id'];
    }
    if ( isset( $cpt_section_args['section_initiator'] ) ) {
        $section_initiator = $cpt_section_args['section_initiator'];
    }
    if ( isset( $cpt_section_args['section_gtbg_align'] ) ) {
        $section_gtbg_align = $cpt_section_args['section_gtbg_align'];
    }
    if ( isset( $cpt_section_args['section_content_wrap_in_small_screen'] ) ) {
        $section_content_wrap_in_small_screen = $cpt_section_args['section_content_wrap_in_small_screen'];
    }
    
    $google_map_api_key = 'AIzaSyAMeoGFRxOQ_pyv9uIvvNOo5SbNiVHnL-E';
    $google_map_address = '1 Westminster Road, London, London, WC1 1AA';
    
    if ( isset( $cpt_section_args['google_map_api_key'] ) ) {
        $google_map_api_key = $cpt_section_args['google_map_api_key'];
    }
    if ( isset( $cpt_section_args['google_map_address'] ) ) {
        $google_map_address = $cpt_section_args['google_map_address'];
    }
    
    $productiveminds_section_display = 'grided';
    
    ?>
    <div class="productiveminds_section google-map <?php echo esc_attr( $section_content_wrap_in_small_screen ); ?> <?php echo esc_attr( $productiveminds_section_display ); ?> <?php echo esc_attr( $section_initiator ); ?>" id="<?php echo esc_attr( $section_content_settings_unique_id ); ?>">
        <div class="productiveminds_section_uno">
    <?php
    
    // Header
    if ( $section_content_header_is_show_section_header ) {
        $physical_address = '';
        if( $google_map_display_physical_address ) {
            $physical_address = productive_forms_contact_get_full_address_textonly_all_in_one_line();
            if( !empty($google_map_address) ) {
                $physical_address = $google_map_address;
            }
        }
        productive_forms_render_header_v_1( $section_title, $section_title_html_tag, $section_intro, $section_header_alignment, $physical_address );
    }
    
    $google_map_container_width = 100;
    $google_map_container_height = 700;
    $google_map_map_magnification = 17;
    
    if ( isset( $cpt_section_args['google_map_container_width'] ) ) {
        $google_map_container_width = intval( $cpt_section_args['google_map_container_width'] );
    }
    if ( isset( $cpt_section_args['google_map_container_height'] ) ) {
        $google_map_container_height = intval( $cpt_section_args['google_map_container_height'] );
    }
    if ( isset( $cpt_section_args['google_map_map_magnification'] ) ) {
        $google_map_map_magnification = intval( $cpt_section_args['google_map_map_magnification'] );
    }
    
    $misc = array(
        'google_map_api_key'                                => $google_map_api_key,
        'google_map_address'                                => $google_map_address,
        'google_map_container_width'                        => $google_map_container_width,
        'google_map_container_height'                       => $google_map_container_height,
        'google_map_map_magnification'                      => $google_map_map_magnification,
        'section_content_settings_unique_id'                => $section_content_settings_unique_id,
        'section_initiator'                                 => $section_initiator,
    );
    
    productive_forms_render_google_map_content( $misc );
    
    ?>
        </div><!-- productiveminds_section_uno -->
    </div><!-- productiveminds_section -->
    <?php
}

function productive_forms_render_google_map_content( $misc = array() ) {
    $google_map_api_key                                 = $misc['google_map_api_key'];
    $google_map_address                                 = $misc['google_map_address'];
    $google_map_container_width                         = $misc['google_map_container_width'];
    $google_map_container_height                        = $misc['google_map_container_height'];
    $google_map_map_magnification                       = $misc['google_map_map_magnification'];
    $section_content_settings_unique_id                 = $misc['section_content_settings_unique_id'] . rand();
    $section_initiator                                  = $misc['section_initiator'];
    
    $url = "https://www.google.com/maps/embed/v1/place?key=" . $google_map_api_key . 
            '&zoom=' . $google_map_map_magnification .
            '&q=' . $google_map_address;
    if ( !empty($google_map_api_key) ) {
    ?>
        <div class="contact-map-box no_margin productive_google_map_container <?php echo esc_attr($section_initiator); ?>">
            <div class="contact-map-box-field">
                <div id="<?php echo esc_attr($section_content_settings_unique_id); ?>" 
                     style="width: <?php echo esc_attr($google_map_container_width); ?>%; height: <?php echo esc_attr($google_map_container_height); ?>px;">
                    <iframe id="<?php echo esc_attr($section_content_settings_unique_id); ?>" 
                            width="100%%" 
                            height="<?php echo esc_attr($google_map_container_height); ?>" frameborder="0" style="border:0" referrerpolicy="no-referrer-when-downgrade"
                            src="<?php echo esc_url($url); ?>"
                            allowfullscreen>
                        </iframe>
                </div>
            </div>
        </div>
    <?php } else {
        productive_global_render_no_content_found( 'google-map', esc_html__("Map display is currently unavailable. Please enter your Google Maps API Key", 'productive-forms'), '' );
    }
}

function productive_forms_render_google_map_content_v1( $misc = array() ) {
    $google_map_container_width                         = $misc['google_map_container_width'];
    $google_map_container_height                        = $misc['google_map_container_height'];
    $google_map_map_magnification                       = $misc['google_map_map_magnification'];
    $section_content_settings_unique_id                 = $misc['section_content_settings_unique_id'] . rand();
    $section_initiator                                  = $misc['section_initiator'];
    
    if ( productive_forms_integration_ask_show_google_map() ) {
        
        $google_map_long = productive_forms_integration_google_map_longitude();
        $google_map_lat = productive_forms_integration_google_map_latitude();
?>
        <div class="contact-map-box no_margin productive_google_map_container <?php echo esc_attr($section_initiator); ?>"
             data-long="<?php echo esc_attr($google_map_long); ?>"
             data-lat="<?php echo esc_attr($google_map_lat); ?>"
             data-map_zoom="<?php echo esc_attr($google_map_map_magnification); ?>"
             data-g_map_container_id="<?php echo esc_attr($section_content_settings_unique_id); ?>"
             data-map_title="<?php echo esc_attr( productive_forms_contact_full_address_textonly_all_in_one_line() ); ?>"
             >
            <div class="contact-map-box-field">
                <div id="<?php echo esc_attr($section_content_settings_unique_id); ?>" style="width: <?php echo esc_attr($google_map_container_width); ?>%; height: <?php echo esc_attr($google_map_container_height); ?>px;"></div>
            </div>
        </div>
    <?php } else {
        productive_global_render_no_content_found( 'google-map', esc_html__("Map display is currently unavailable. Please configure the Google Maps settings on the plugin's options page.", 'productive-forms'), '' );
    }
}
