<?php

namespace CarDealerPress\templates\partials\inventory;

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\Includes\Functions\Fn_Inventory;
use CarDealerPress\Includes\Helpers\Option;

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

// for non ajax themes.
class Prices {

	public $fn_inventory;
	public $company;
	public $theme_options;
	public array $price;
	public $vehicle;

	public function __construct($vehicle) {
		$this->fn_inventory = new Fn_Inventory();
		$this->company = ( new Requests())->cdp_company_request()[0];
		$this->theme_options = Option::get_cdp_theme_options();
		$this->vehicle = $vehicle;
		$this->price       = $this->fn_inventory->get_price_display(
			array(
				'company' => $this->company,
				'vehicle' => $this->vehicle,
				'theme'   => 'inventory',
			)
		);
	}

	public function price_box() {

		$price_scope = ( isset( $this->price['no_price'] ) ? '' : 'itemprop="offers" itemscope itemtype="https://schema.org/Offer"' );
		$content     = "<div class='inventory-vehicle-pricing ' $price_scope>";
		$content .= $this->build_prices();
		$content .= '</div>';

		return $content;
	}

	public function price_box_detail_view() {

		$price_scope = ( isset( $this->price['no_price'] ) ? '' : 'itemprop="offers" itemscope itemtype="https://schema.org/Offer"' );
		$content     = "<div class='detail-vehicle-pricing' $price_scope>";
		$content .= $this->build_prices( );
		$loan_parameters = array(
			'make'      => $this->vehicle['make'],
			'model'     => $this->vehicle['model'],
			'trim'      => $this->vehicle['trim'],
			'year'      => $this->vehicle['year'],
			'saleclass' => $this->vehicle['saleclass'],
		);
		$content         .= '<div class="price-item price-loan-payment">' . $this->fn_inventory->get_loan_value( $this->theme_options['theme']['inventory_display']['payment_calculator'], $this->price['primary_price'], $loan_parameters ) . '</div>';
		$content         .= '</div>';

		return $content;
	}

	public function build_prices() {
		$content = '';
		$price_labels = ['msrp', 'compare_price', 'discount', 'sale_price', 'call_for_price', 'expire'];
		$custom_price_on_off = $this->theme_options['theme']['inventory_display']['prices']['display'][ strtolower( $this->vehicle['saleclass'] ) ] ?? array();
		foreach ( $price_labels as $label ) {
			$price_text = $label . '_text';
			if ( ! empty( $this->price[ $price_text ] ) ) {
				$status = $custom_price_on_off[$label] ?? '';
				if ( $status == 'on' ) {
					$price = $this->price[ $price_text ];
					$content .= "<div class='price-item price-$label'>$price</div>";
				}
			}
		}
		$content         .= '<div class="price-item price-hidden-values" style="display:none;">' . $this->price['hidden_prices'] . '</div>';
		return $content;
	}

	public function loan_box( $vehicle ) {

	}

	public function discount_and_price( $vehicle ) {

	}
}
