<?php

namespace CarDealerPress\Templates\Inventory;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\Includes\Helpers\Remote_Requests;
use CarDealerPress\templates\partials\inventory\Extra_Boxes;
use CarDealerPress\templates\partials\inventory\Image_Display;
use CarDealerPress\templates\partials\inventory\Prices;
use CarDealerPress\templates\partials\inventory\Forms_Display;
use CarDealerPress\templates\partials\inventory\Schema_Island;
use CarDealerPress\templates\partials\inventory\Vehicle_Attributes;

if ( $vehicle ) {

    $companies = ( new Requests() )->cdp_db_init( 'company' );
    $company   = array();
    foreach ( $companies as $key => $info ) {
        if ( $info['dealer_id'] === $vehicle['dealer_id'] ) {
            $company = $companies[ $key ];
        }
    }

    $price                    = $fn_inventory->get_price_display(
            array(
                    'company' => $this->company,
                    'vehicle' => $vehicle,
                    'theme'   => 'inventory',
            )
    );
    $vehicle['primary_price'] = $price['primary_price'];

    $parameters['saleclass'] = $vehicle['saleclass'];
    $fn_inventory->apply_gravity_form_hooks( $vehicle );
    // splide.
    wp_enqueue_script( 'cdp_splide_js', CDP_JS . 'splide/splide.min.js', array( 'jquery' ), '4.1.4', true );
    wp_enqueue_script( 'cdp_splide_js', CDP_JS . 'splide/splide-extension-video.min.js', array( 'jquery' ), '0.8.0', true );

    wp_enqueue_style( 'cdp_splide_style', CDP_CSS . 'splide/splide.min.css', array(), '4.1.4' );
    wp_enqueue_style( 'cdp_splide_style', CDP_CSS . 'splide/splide-extension-video.min.css', array(), '0.8.0' );

    // Map.
    if ( $this->theme_options['theme']['inventory_display']['detail']['maps']['key'] ) {
        $map_output = $ext_functions->extender_map_generator( $vehicle['dealer_id'] );
    } else {
        $map_output = null;
    }
    $ext_functions->load_parameters( $this->parameters );

    helpers::output_wp_kses( $ext_functions->extender_google_tag_script( $vehicle ) );

    ?>

    <div id="inventory-wrapper">
        <?php
        if ( $this->theme_options['theme']['inventory_display']['general']['saved_vehicles']['display'] ) {
            helpers::output_wp_kses( $ext_functions->extender_saved_vehicle_container() );
        }
        ?>
        <div id="inventory-detail" class="inventory-main-container">
            <div id="page-link" class="vehicle-save" style="display: none;" data-name="link"
                 href="<?php echo esc_url( $page_url ); ?>"></div>
            <div id="inventory-detail-navigation">
                <div class="navigation-container previous-link" onclick="window.history.go(-1); return false;">
                    <span class="previous-icon banner-icon"></span>
                    <span class="previous-text banner-text">Previous Search</span>
                </div>
                <div class="navigation-container friendly-print"
                     onclick="window.open('?print_page','popup','width=800,height=900,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=yes,status=no,left=0,top=0'); return false">
                    <span class="print-icon banner-icon"></span>
                    <span class="print-text banner-text">Friendly Print</span>
                </div>
                <div class="navigation-container dealer-number">
                    <a href="tel: <?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ); ?>">
                        <span class="phone-icon banner-icon"></span>
                        <span class="phone-text banner-text">Sales: <?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ); ?> </span>
                    </a>
                </div>
                <?php
                if ( 'on' === $this->theme_options['theme']['inventory_display']['detail']['general']['social']['facebook']['enable'] ) {
                    echo '<div class="navigation-container facebook-share">';
                    echo '<div class="fb-share-button" data-href="' . esc_url( $this->page_url ) . '" data-layout="button" data-size="small" data-mobile-iframe="true">
								<a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&amp;src=sdkpreparse">Share</a>
								</div>';
                    echo '</div>';
                }
                ?>
            </div>
            <?php
            $banners = Option::check_for_option( 'cdp_theme_options', array(
                    'theme',
                    'inventory_display',
                    'banners'
            ) );
            if ( ! empty( $banners ) ) {
                $content        = '<div id="cdp-banner-top-container">';
                $banner_results = $fn_inventory->cdp_get_banners( 'detail', 'top', $banners, $this->parameters );
                foreach ( $banner_results as $banner ) {
                    $content .= $banner;
                }
                $content .= '</div>';
                echo wp_kses( $content, 'post' );
            }
            helpers::output_wp_kses( $ext_functions->detail_page_sold_vehicle( $vehicle ) );
            ?>
            <div id="<?php helpers::output_wp_kses( $vehicle['vin'] . '" class="inventory-detail-vehicle-container vehicle-container company-' . helpers::company_id() . ' location-' . $vehicle['dealer_id'] ); ?>">
                <?php helpers::output_wp_kses( ( empty( $vehicle['headline'] ) ? '' : '<div class="inventory-vehicle-headline">' . $vehicle['headline'] . '</div>' ) ); ?>
                <div id="title-price-wrapper" class="detail-wrapper">
                    <div class="title-price-container">
                        <?php
                        if ( $vehicle['certified'] ) {
                            $saleclass = 'Certified';
                        } else {
                            $saleclass = ucfirst( $vehicle['saleclass'] );
                        }
                        echo '<div class="title-price-item header-saleclass-container">' . esc_html( $saleclass ) . '</div>';
                        ?>
                        <div class="title-price-item header-vehicle-title-container">
                            <?php
                            $content = '<span class="header-year vehicle-save form-value" data-name="year">' . $vehicle['year'] . '</span>';
                            $content .= '<span class="header-make form-value vehicle-save" data-name="make">' . $vehicle['make'] . '</span>';
                            $content .= '<span class="header-model form-value vehicle-save" data-name="model">' . $vehicle['model'] . '</span>';
                            $content .= '<span class="header-trim form-value vehicle-save" data-name="trim">' . $vehicle['trim'] . '</span>';
                            helpers::output_wp_kses( $content );
                            ?>
                        </div>
                        <?php
                        $prices = '<div class="title-price-item price-display-container">' . ( new Prices( $vehicle ) )->price_box() . '</div>';
                        helpers::output_wp_kses( $prices );
                        $odometer_value  = '<span data-odometer="' . $vehicle['odometer'] . '">' . number_format( $vehicle['odometer'], 0, '', ',' ) . '</span>';
                        $loan_parameters = array(
                                'make'      => $vehicle['make'],
                                'model'     => $vehicle['model'],
                                'trim'      => $vehicle['trim'],
                                'year'      => $vehicle['year'],
                                'saleclass' => $vehicle['saleclass'],
                        );
                        $prices          = $fn_inventory->get_prices( $vehicle );
                        helpers::output_wp_kses( '<div class="title-price-item price-loan-payment-container">' . $fn_inventory->get_loan_value( Option::check_for_option( 'cdp_theme_options', array(
                                        'theme',
                                        'inventory_display',
                                        'payment_calculator'
                                ) ), $prices['main_price'], $loan_parameters ) . '</div>' );
                        ?>
                    </div>
                    <?php
                    $contact = $ext_functions->contact_wrapper( $vehicle, $company, 'detail' );
                    if ( ! empty( $contact ) ) {
                        echo '<div class="dealer-contact-wrapper">';
                        helpers::output_wp_kses( $contact );
                        echo '</div>';
                    }
                    ?>
                </div>
                <div id="detail-photos-wrapper" class="detail-wrapper">
                    <?php echo wp_kses( ( new Image_Display() )->images_with_thumbs_vertical( $vehicle ), 'post' ); ?>
                </div>
                <div id="middle-container">
                    <?php
                    $extra_boxes = ( new Extra_Boxes() )->boxes_horizontal( $vehicle, 'detail' );
                    if ( ! empty( $extra_boxes ) ) {
                        echo '<div id="extra-boxes-wrapper" class="detail-wrapper">';
                        helpers::output_wp_kses( $extra_boxes );
                        echo '</div>';
                    }
                    ?>
                    <?php if ( ! empty( $vehicle['description'] ) ) {
                        echo '<div id="vehicle-description-wrapper" class="detail-wrapper">';
                        echo '<div class="section-heading-title">Description</div>';
                        echo '<div class="vehicle-description"><p>' . wp_kses( $vehicle['description'], 'post' ) . '</p></div>';
                        echo '</div>';
                    }
                    ?>
                    <?php
                    $forms[]                   = $ext_functions->extender_detail_forms( $vehicle );
                    $payment_calculator_form[] = $ext_functions->extender_detail_loan( $vehicle );
                    $show_pc_form              = Option::check_for_option( 'cdp_theme_options', array(
                            'theme',
                            'inventory_display',
                            'payment_calculator',
                            $vehicle['saleclass'],
                            'default',
                            'display_calc'
                    ) );
                    switch ( true ) {
                        case ( ! empty( $forms[0] ) && $show_pc_form ):
                            echo '<div id="forms-wrapper" class="detail-wrapper">';
                            echo ( new Forms_Display() )->tabs( $forms, 'forms' );
                            helpers::output_wp_kses( ( new Forms_Display() )->tabs( $payment_calculator_form, 'pc' ) );
                            echo '</div>';
                            break;
                        case ( ! empty( $forms[0] ) ):
                            echo '<div id="forms-wrapper" class="detail-wrapper">';
                            echo ( new Forms_Display() )->tabs( $forms, 'forms' );
                            echo '</div>';
                            break;
                        case ( $show_pc_form ):
                            echo '<div id="forms-wrapper" class="detail-wrapper">';
                            helpers::output_wp_kses( ( new Forms_Display() )->tabs( $payment_calculator_form, 'pc' ) );
                            echo '</div>';
                            break;
                    }
                    ?>
                    <?php
                    $wrapper_style = '';
                    if ( ! empty( $forms[0] ) || 'on' == $show_pc_form ) {
                        $wrapper_style = 'has_form';
                    }
                    ?>
                    <div id="vehicle-attributes-wrapper"
                         class="detail-wrapper <?php echo esc_attr( $wrapper_style ); ?>">
                        <div class="section-heading-title">Details</div>
                        <div class="vehicle-attributes-container">
                            <?php helpers::output_wp_kses( Vehicle_Attributes::call(
                                    array(
                                            'vehicle' => $vehicle,
                                            'layout'  => 'list',
                                            'page'    => 'detail',
                                    )
                            ) ); ?>
                        </div>
                    </div>
                </div>
                <?php if ( ! empty( $vehicle['dealer_options'] ) ) {
                    echo '<div id="dealer-options-wrapper" class="detail-wrapper">';
                    echo '<div class="section-heading-title">Features</div>';
                    echo '<div class="dealer-options-container">';
                    echo '<div class="dealer-options-list"><ul>';
                    foreach ( $vehicle['dealer_options'] as $option ) {
                        helpers::output_wp_kses( '<li class="value">' . $option . '</li>' );
                    }
                    echo '</div></ul>';
                    echo '</div>';
                    echo '</div>';
                } ?>
                <?php if ( Option::check_for_option( 'cdp_theme_options', array(
                        'theme',
                        'inventory_display',
                        'detail',
                        'general',
                        'show_standard_eq'
                ) ) ) {
                    $equipment = $fn_inventory->display_equipment( $vehicle['standard_equipment'], $vehicle['saleclass'] );
                    echo '<div id="standard-features-wrapper" class="standard-equipment-container tab-content-inner detail-wrapper">';
                    if ( ! empty( $equipment ) ) {
                        echo '<div class="section-heading-title">Standard Equipment</div>';
                        helpers::output_wp_kses( $equipment );
                    }
                    echo '</div>';
                } ?>
                <?php
                if ( $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] ) {
                    $similar = $ext_functions->extender_similar_vehicle_pull( $vehicle );
                    if ( ! str_contains( $similar, 'No Similar Vehicles Found' ) ) {
                        helpers::output_wp_kses( '<div id="similar-vehicle-wrapper" class="detail-wrapper"><div id="similar-title">Similar Vehicles</div>' . $similar . '</div>' );
                    }
                }
                ?>
                <?php
                echo empty( $map_output ) ? '' : '<div id="map-container" class="detail-wrapper"></div>';
                wp_register_script( 'cdp_injector_js', CDP_JS . 'templates/inventory/injector.js', 'jquery', CDP_VERSION, true ); //injector.js is a placeholder file.
                wp_enqueue_script( 'cdp_injector_js' );
                wp_add_inline_script( 'cdp_injector_js', $map_output ); ?>
                <?php helpers::output_wp_kses( '<div id="disclaimer-wrapper" class="detail-wrapper"><p>' . $fn_inventory->get_disclaimer() . '</p></div>' ); ?>
                <?php helpers::output_wp_kses( Schema_Island::island( $vehicle, $company, $price ) ); ?>
            </div>
        </div>
    </div>
    <?php
}