<?php

namespace CarDealerPress\templates\admin\theme\inventory_display\detail;

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

use CarDealerPress\includes\database\Requests;
use CarDealerPress\includes\helpers\Generic;
use CarDealerPress\includes\helpers\Option;
use CarDealerPress\templates\partials\admin\Table_Rows;

function maps(): string {
	$option  = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'maps',
			'custom',
		)
	);
	$count   = Generic::get_count_of( $option );
	$content = '';
	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}

	$content .= maps_key();
	$content .= '<hr>';

	if ( ! empty( $option ) ) {
		$status = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				0,
				'geocode',
				'status',
			),
			'Unknown'
		);
		if ( 'ok' === strtolower( $status ) ) {
			$content .= current_maps( $option );
			$content .= '<hr>';
		}
	}
	$content .= create_map( 'Create/Update Map', $row_id );

	return $content;
}

function maps_key() {
	$content = '<h3>Map API Key</h3>';
	$content .= '<table id="maps-settings-table" class="">';
	$content .= '<tbody>';
	$map_key = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'maps',
			'key',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'id'    => 'map-key',
			'class' => 'input-long',
			'label' => 'Google Map API Key',
			'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][key]',
			'value' => $map_key,
			'notes' => '',
		)
	);
	$status  = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'maps',
			'custom',
			0,
			'geocode',
			'status',
		),
		'Unknown'
	);
	$content .= table_rows::tr_info_div(
		array(
			'id'         => 'maps-key-status',
			'label'      => 'Key Status: ',
			'textString' => "$status",
			'class'      => 'cdp-settings-map-status',
			'note'       => '',
		)
	);
	if ( 'ok' !== strtolower( $status ) ) {
		$error_message = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				0,
				'geocode',
				'status',
			),
			'No key entered'
		);
		$content       .= table_rows::tr_info_div(
			array(
				'id'         => 'maps-key-error-message',
				'label'      => 'Error Message: ',
				'textString' => "$error_message",
				'class'      => 'cdp-settings-map-error-message',
				'note'       => '',
			)
		);
	}
	if ( $map_key && 'ok' === strtolower( $status ) ) {
		$content .= '<tr><small>Key Found. Only enter a key value if you wish to update your Key.</small></tr>';
	} else {
		$content .= '<tr><td>';
		$content .= '<input id="add-map-key-button" class="button" type="button" value="Add Key"/><td>';
		$content .= '</td><tr>';
		$content .= '<tr><td>';
		$content .= '<div class="td-full-small center"><p id="dealer-ajax-message-advanced" class="data-ajax-message"></p></div>';
		$content .= '</td><tr>';
		$content .= '<tr><td><a href="https://console.developers.google.com/flows/enableapi?apiid=maps_backend,geocoding_backend,directions_backend,distance_matrix_backend,elevation_backend,places_backend&keyType=CLIENT_SIDE&reusekey=true" target="_blank" >Go to the Google Developer Console for Key.</a></td></tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function current_maps( $option ): string {
	$content = '<section id="current">';
	$content .= '<div><h3>Current Maps</h3></div>';
	$content .= '<div id="current-rows" class="inner-table-content">';
	$content .= '<table id="current-table" class="">';
	$content .= '<tbody>';
	foreach ( $option as $row_id => $item ) {
		$name      = $item['dealer']['name'];
		$dealer_id = $item['dealer']['dealer_id'];
		$content   .= "<tr id='outer-$row_id' data-tag='outer-$row_id' class='outer-row' data-label-name='$name .' '. $dealer_id'  data-option='theme' data-tab='inventory_display' 
                        data-section='detail' data-section1='maps'></tr>";
		$content   .= '<td><label>' . $name . ' ' . $dealer_id . '</label>';
		$content   .= '<input id="edit-' . $row_id . '" class="edit-row" data-row-id="' . $row_id . '" type="button" value="Edit"/>';
		$content   .= '<input id="removeTableRow" class="delete-row" data-row-id="' . $row_id . '" type="button" value="Delete"/><td>';
		$content   .= '</tr>';
		$content   .= '<tr id="inner-' . $row_id . '" tag="maps-inner-' . $row_id . '" class="detail inner-row">';
		$content   .= '<td>';
		$content   .= '<div class="">';
		$content   .= '<table class="table-wrap current">';
		$content   .= '<tbody>';
		$content   .= row_map( $row_id, true );
		$content   .= '</tbody>';
		$content   .= '</table>';
		$content   .= '</div>';
		$content   .= '</td>';
		$content   .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</div>';
	$content .= '</section>';

	return $content;
}

function create_map( $title, $row_id ): string {
	$content = '<section id="new">';
	$content .= '<h3>' . $title . '</h3>';
	$content .= '<input id="addNewTableButton" class="maps add-row toggle-row" type="button" value="Add +"/>';
	$content .= '<div class="table-content" style="display:none;"></div>';
	$content .= '</section>';

	$script = '<script>jQuery("#addNewTableButton").click(function (e) { jQuery(".table-content").append("' . table_map( $row_id ) . '"); });</script>';
	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', $script );

	return $content;
}

function table_map( $row_id ) {
	$content = "<table id='addNewTable' class='table-wrap add'>";
	$content .= '<tbody>';
	$content .= row_map( $row_id );
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function row_map( $row_id, $show = false ): string {
	$content = '';
	// todo add dealer id, or change to just a text field like legacy, also check that dealer id exists.
	/*
	$dealers = Main::$db_request->cdp_db_init('dealers', array('pass'=>'1'));
	$dealers_list = Generic::build_list(array('data'=>$dealers,'key'=>'Dealer', 'first_option' => 'Select Dealer'));
		$value = Option::check_for_option('cdp_theme_options', array('theme','inventory_display','detail', 'maps', 'custom', $row_id, 'dealer_id'));
		$content .= helpers::tr_select_dropown(array('label' => 'Dealer', 'id' => "$row_id-dealer",
			'name' => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer]', 'value' => $value, 'list' => $dealers_list));*/

	$dealer_id = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'maps',
			'custom',
			$row_id,
			'dealer',
			'dealer_id',
		)
	);
	$content   .= table_rows::tr_setting_text(
		array(
			'label' => 'Dealer Id',
			'id'    => "$row_id-dealer-id",
			'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][dealer_id]',
			'value' => $dealer_id,
			'class' => 'input-long',
			'type'  => 'number',
		)
	);

	if ( true === $show ) {
		if ( $dealer_id ) {
			$dealer = ( new Requests() )->cdp_company_dealer_request( $dealer_id )[0];
		} else {
			$dealer = array(
				'name'   => '',
				'street' => '',
				'city'   => '',
				'state'  => '',
				'zip'    => '',
			);
		}
		$name = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'dealer',
				'name',
			),
			$dealer['name']
		);
		if ( empty( $name ) ) {
			$required = 'true';
		} else {
			$required = '';
		}
		$content .= table_rows::tr_setting_text(
			array(
				'label'    => 'Name',
				'id'       => "$row_id-name",
				'name'     => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][name]',
				'value'    => $name,
				'class'    => 'input-long',
				'required' => $required,
			)
		);

		$street  = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'dealer',
				'street',
			),
			$dealer['street']
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label' => 'Street',
				'id'    => "$row_id-street",
				'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][street]',
				'value' => $street,
				'class' => 'input-long',
			)
		);

		$city    = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'dealer',
				'city',
			),
			$dealer['city']
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label' => 'City',
				'id'    => "$row_id-city",
				'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][city]',
				'value' => $city,
				'class' => 'input-long',
			)
		);

		$state   = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'dealer',
				'state',
			),
			$dealer['state']
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label' => 'State',
				'id'    => "$row_id-state",
				'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][state]',
				'value' => $state,
				'class' => 'input-long',
			)
		);

		$zip     = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'dealer',
				'zip',
			),
			$dealer['zip']
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label' => 'Zip',
				'id'    => "$row_id-zip",
				'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][zip]',
				'value' => $zip,
				'class' => 'input-long',
				'type'  => 'number',
			)
		);

		$zoom    = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'dealer',
				'zoom',
			),
			14
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label' => 'Zoom',
				'id'    => "$row_id-zoom",
				'name'  => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][zoom]',
				'value' => $zoom,
				'class' => 'input-long',
				'type'  => 'number',
			)
		);

		$geo_lat = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'geocode',
				'results',
				0,
				'geometry',
				'location',
				'lat',
			)
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label'     => 'Lat',
				'id'        => 'map-lat',
				'row_class' => '',
				'name'      => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][lat]',
				'value'     => $geo_lat,
			)
		);

		$geo_lng = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'geocode',
				'results',
				0,
				'geometry',
				'location',
				'lng',
			)
		);
		$content .= table_rows::tr_setting_text(
			array(
				'label'     => 'Long',
				'id'        => 'map-long',
				'row_class' => '',
				'name'      => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][dealer][lng]',
				'value'     => $geo_lng,
			)
		);

		$geo     = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'detail',
				'maps',
				'custom',
				$row_id,
				'geocode',
			)
		);
		$content .= table_rows::tr_setting_textarea(
			array(
				'label'     => 'geo',
				'id'        => 'map-geo',
				'row_class' => 'hidden',
				'name'      => 'cdp_theme_options[theme][inventory_display][detail][maps][custom][' . $row_id . '][geocode]',
				'value'     => wp_json_encode( $geo ),
			)
		);

		$content .= table_rows::edit_update( $show, $row_id );
	}
	if ( false === $show ) {
		$content .= '<tr><td>';
		$content .= "<input id='add-additional-dealer-map-button' class='button' type='button' data-row-id='$row_id' value='Add'/><td>";
		$content .= '</td><tr>';
	}

	return $content;
}

