<?php

namespace CarDealerPress\Includes\Functions;

use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\Includes\Init\Default_Options;

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

/**
 * Class Alter Options.
 * This class is for when the options in the plugin change.
 */
class Alter_Options {
	use Logger;

	public $current_option_version;
	public $admin_options;
	public $default_current_option_version;

	public function __construct() {
		$this->admin_options                  = Option::get_cdp_admin_options();
		$this->current_option_version         = Option::check_for_option( 'cdp_admin_options', array(
			'admin',
			'database',
			'options',
			'current',
		) );
		$this->default_current_option_version = Default_Options::$default_options_admin['admin']['database']['options']['current'];
		$this->options_update();
		$this->better_options_update();
	}

	public function options_update() {
		if ( version_compare( CDP_VERSION, '6.5.2405.00', '>' ) ) {
			$cdp_theme_options = get_option( 'cdp_theme_options' );
			$display_similar   = $cdp_theme_options['theme']['inventory_display']['detail']['general']['display_similar'] ?? '';

			if ( $display_similar ) {
				$cdp_theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] = $cdp_theme_options['theme']['inventory_display']['detail']['general']['display_similar'];
				unset( $cdp_theme_options['theme']['inventory_display']['detail']['general']['display_similar'] );
				update_option( 'cdp_theme_options', $cdp_theme_options );
			}
			if ( empty( $cdp_theme_options['theme']['inventory_display']['detail']['similar_vehicles']['price_add'] ) ) {
				$cdp_theme_options['theme']['inventory_display']['detail']['similar_vehicles']['price_add']      = 1000;
				$cdp_theme_options['theme']['inventory_display']['detail']['similar_vehicles']['price_subtract'] = 3000;
				$cdp_theme_options['theme']['inventory_display']['detail']['similar_vehicles']['link_text']      = 'More Information';
				update_option( 'cdp_theme_options', $cdp_theme_options );
			}
		}
		if ( version_compare( CDP_VERSION, '6.5.2406.01', '>' ) ) {
			$cdp_theme_options = get_option( 'cdp_theme_options' );
			$option            = $cdp_theme_options['theme']['inventory_display']['prices']['display']['show_decimals'] ?? '';

			if ( empty( $option ) ) {
				$cdp_theme_options['theme']['inventory_display']['prices']['display']['show_decimals'] = Default_Options::$default_options_theme['theme']['inventory_display']['prices']['display']['show_decimals'];
				update_option( 'cdp_theme_options', $cdp_theme_options );
			}
		}
		if ( version_compare( CDP_VERSION, '6.6.2408.00', '>' ) ) {
			$cdp_admin_options = get_option( 'cdp_admin_options' );
			$option            = $cdp_admin_options['admin']['general']['vdp_url_separator'] ?? '';

			if ( empty( $option ) ) {
				$cdp_admin_options['admin']['general']['vdp_url_separator'] = Default_Options::$default_options_admin['admin']['general']['vdp_url_separator'];
				update_option( 'cdp_admin_options', $cdp_admin_options );
			}
		}
		if ( version_compare( CDP_VERSION, '6.6.2409.04', '>' ) ) {
			$cdp_shortcode_options = get_option( 'cdp_shortcode_options' );
			if ( empty( $cdp_shortcode_options ) ) {
				add_option( 'cdp_shortcode_options', Default_Options::$default_options_shortcode );
				update_option( 'cdp_shortcode_options', Default_Options::$default_options_shortcode );
			}
		}

	}

	public function better_options_update() {
		// correct way
		if ( version_compare( $this->default_current_option_version, $this->current_option_version, '>' ) ) {
			$cdp_theme_options = get_option( 'cdp_theme_options' );

			if ( isset( $cdp_theme_options['theme']['inventory_display']['prices']['text']['used']['price'] ) ) {
				$price_range                                                                                            = Default_Options::$default_options_theme['theme']['inventory_display']['list']['search_filters']['general']['price_range'];
				$odometer_range                                                                                         = Default_Options::$default_options_theme['theme']['inventory_display']['list']['search_filters']['general']['odometer_range'];
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['price_range']    = $price_range;
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['odometer_range'] = $odometer_range;

				$saleclasses = array( 'new', 'used' );
				foreach ( $saleclasses as $saleclass ) {
					$option = $cdp_theme_options['theme']['inventory_display']['prices']['display'][ $saleclass ] ?? array();
					if ( empty( $option ) ) {
						$cdp_theme_options['theme']['inventory_display']['prices']['display'][ $saleclass ] = Default_Options::$default_options_theme['theme']['inventory_display']['prices']['display'][ $saleclass ];
						update_option( 'cdp_theme_options', $cdp_theme_options );
					}

				}
				$cdp_theme_options['theme']['inventory_display']['prices']['text']['new']['msrp']  = Default_Options::$default_options_theme['theme']['inventory_display']['prices']['text']['new']['msrp'];
				$cdp_theme_options['theme']['inventory_display']['prices']['text']['used']['msrp'] = Default_Options::$default_options_theme['theme']['inventory_display']['prices']['text']['used']['msrp'];
				unset( $cdp_theme_options['theme']['inventory_display']['prices']['text']['new']['price'] );
				unset( $cdp_theme_options['theme']['inventory_display']['prices']['text']['used']['price'] );
			}

			if ( ! isset( $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'] ) ) {
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'] = Default_Options::$default_options_theme['theme']['inventory_display']['list']['search_filters']['general']['filters'];
				update_option( 'cdp_theme_options', $cdp_theme_options );

				$price_range = $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['price_range'];
				$odometer_range = $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['odometer_range'];

				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'][1]['step'] = $price_range;
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'][2]['step'] = $odometer_range;
				unset( $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['price_range'] );
				unset( $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['odometer_range'] );

			}

			if ( $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'][0]['name'] == 'year' ) {
				//get current values
				$price_step = $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'][1]['step'];
				$odometer_step = $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'][2]['step'];

				$filters = $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'];

				[
					[$year_display_type, $year_display],
					[$price_display_type, $price_display],
					[$odometer_display_type, $odometer_display]
				] = array_map(
					fn($f) => [$f['display_type'], $f['display']],
					array_slice($filters, 0, 3)
				);

				$old_hide     = ['hide_saleclass', 'hide_features', 'hide_vehicleclass'];
				$new_hide     = ['saleclass', 'features', 'vehicleclass'];

				$old_display  = ['display_bodystyle', 'display_drivetrain', 'display_mpg', 'display_transmission'];
				$new_display  = ['bodystyle', 'drivetrain', 'mpg', 'transmission'];

				$options_hide = array_combine(
					$old_hide,
					array_map(fn($key) => $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general'][$key] ?? '', $old_hide)
				);

				$options_display = array_combine(
					$old_display,
					array_map(fn($key) => $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general'][$key] ?? '', $old_display)
				);

				$color_enabled = $cdp_theme_options['theme']['inventory_display']['list']['search_filters']['color_mapping']['enable'];

				// reset to defaults, so new filters will be added.
				$filters = Default_Options::$default_options_theme['theme']['inventory_display']['list']['search_filters']['general']['filters'];

				// repopulate old display values
				foreach ($new_hide as $i => $name) {
					if (($idx = array_search($name, array_column($filters, 'name'), true)) !== false) {
						if ($options_hide[$old_hide[$i]] == 'on') {
							$filters[ $idx ]['display'] = '';
						}
					}
				}

				foreach ($new_display as $i => $name) {
					if (($idx = array_search($name, array_column($filters, 'name'), true)) !== false) {
						$filters[$idx]['display'] = $options_display[$old_display[$i]];
					}
				}

				// repopulate price/year/odometer data
				$filterData = [
					[11, $year_display_type, $year_display],
					[12, $price_display_type, $price_display],
					[13, $odometer_display_type, $odometer_display]
				];

				foreach ($filterData as [$index, $type, $display]) {
					$filters[$index]['display_type'] = $type;
					$filters[$index]['display'] = $display;
				}
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['price_step'] = $price_step;
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['odometer_step'] = $odometer_step;
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'] = $filters;
				$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['filters'][14]['display'] = $color_enabled;

				foreach (array_merge($old_hide, $old_display) as $key) {
					unset($cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general'][$key]);
				}
				unset($cdp_theme_options['theme']['inventory_display']['list']['search_filters']['color_mapping']['enable']);
				unset($cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['hide_certified_saleclass']);
				unset($cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['show_saleclass_tag']);


			}

			update_option( 'cdp_theme_options', $cdp_theme_options );

		}

		$this->admin_options['admin']['database']['options']['current'] = $this->default_current_option_version;
		update_option( 'cdp_admin_options', $this->admin_options );
	}
}