<?php
/**
 * Constant Class File
 *
 * This file contains Constant class which defines needed.
 */

namespace CarDealerPress\Includes\Init;

use CarDealerPress\includes\helpers\helpers as helpers;
use CarDealerPress\Includes\Helpers\Option;

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

class Constant {

	/**
	 * Define define_constant method in Constant class
	 *
	 * Defines all constants that you need
	 */
	public static function define_constant() {
		global $wpdb;

		/**
		 * CARDEALERPRESS_PATH constant.
		 * Used to specify plugin path
		 */
		if ( ! defined( 'CARDEALERPRESS_PATH' ) ) {
			define( 'CARDEALERPRESS_PATH', trailingslashit( plugin_dir_path( dirname( __FILE__, 2 ) ) ) );
		}

		/**
		 * CARDEALERPRESS_URL constant.
		 * Used to specify plugin urls
		 */
		if ( ! defined( 'CARDEALERPRESS_URL' ) ) {
			define( 'CARDEALERPRESS_URL', trailingslashit( plugin_dir_url( dirname( __FILE__, 2 ) ) ) );
		}

		/**
		 * CDP_INVENTORY_ db table name constant.
		 */
		if ( ! defined( 'CDP_INVENTORY_TABLE' ) ) {
			define( 'CDP_INVENTORY_TABLE', $wpdb->prefix . 'cdp_inventory' );
		}

		/**
		 * CDP_INVENTORY_IMPORT_ db table name constant.
		 */
		if ( ! defined( 'CDP_INVENTORY_IMPORT_TABLE' ) ) {
			define( 'CDP_INVENTORY_IMPORT_TABLE', $wpdb->prefix .'cdp_inventory_import' );
		}

		/**
		 * CDP_COMPANIES db table name constant.
		 */
		if ( ! defined( 'CDP_COMPANIES' ) ) {
			define( 'CDP_COMPANIES', $wpdb->prefix . 'cdp_companies' );
		}

		/**
		 * CDP_DATA db table name constant.
		 */
		if ( ! defined( 'CDP_DATA' ) ) {
			define( 'CDP_DATA', $wpdb->prefix .'cdp_data' );
		}

		/**
		 * CDP_IMG constant.
		 * Used to specify image urls inside assets/images directory.
		 */
		if ( ! defined( 'CDP_IMG' ) ) {
			define( 'CDP_IMG', trailingslashit( CARDEALERPRESS_URL ) . 'assets/images/' );
		}

		/**
		 * CDP_CSS constant.
		 * Used to specify css urls inside assets/css directory.
		 */
		if ( ! defined( 'CDP_CSS' ) ) {
			define( 'CDP_CSS', trailingslashit( CARDEALERPRESS_URL ) . 'assets/css/' );
		}

		/**
		 * CDP_JS constant.
		 * Used to specify JS urls inside assets/js directory.
		 */
		if ( ! defined( 'CDP_JS' ) ) {
			define( 'CDP_JS', trailingslashit( CARDEALERPRESS_URL ) . 'assets/js/' );
		}

		/**
		 * CDP_INCLUDES constant.
		 * Used to specify include path inside includes directory.
		 */
		if ( ! defined( 'CDP_INCLUDES' ) ) {
			define( 'CDP_INCLUDES', trailingslashit( CARDEALERPRESS_PATH . 'includes' ) );
		}

		// **
		// * PLUGIN_NAME_LANG constant.
		// * Used to specify language path inside languages' directory.
		// */
		// if ( ! defined( 'PLUGIN_NAME_LANG' ) ) {
		// define( 'PLUGIN_NAME_LANG', trailingslashit( CARDEALERPRESS_PATH . 'languages' ) );
		// }

		/**
		 * PLUGIN_NAME_TPL constant.
		 * Used to specify template urls inside templates' directory.
		 */
		if ( ! defined( 'PLUGIN_NAME_LOGS' ) ) {
			define( 'PLUGIN_NAME_LOGS', trailingslashit( CARDEALERPRESS_PATH . 'logs' ) );
		}

		$blog_id = get_current_blog_id();
		if ( ! defined( 'CDP_LOGS' ) ) {
			define( 'CDP_LOGS', CARDEALERPRESS_PATH . 'logs/cdp-logs-'.$blog_id.'.txt');
		}

		/**
		 * BASE_INVENTORY_API_URL constant
		 */
		if ( ! defined( 'BASE_INVENTORY_API_URL' ) ) {
			// TODO - remove at future point, initially need to help with Transfer of options.
			if ( empty( Option::check_for_option( 'cdp_admin_options', array( 'admin', 'help', 'host' ) ) ) ) {
				define( 'BASE_INVENTORY_API_URL', 'https://api.dealertrend.com' );
			} else {
				define(
					'BASE_INVENTORY_API_URL',
					urldecode(
						Option::check_for_option(
							'cdp_admin_options',
							array(
								'admin',
								'help',
								'host',
							)
						)
					)
				);
			}
		}

		/**
		 * MASTER_FILE constant.
		 */
		if ( ! defined( 'MASTER_FILE' ) ) {
			define( 'MASTER_FILE', CARDEALERPRESS_PATH . '/cardealerpress.php' );
		}

		$plugin_data = array(
			'Name'        => 'Plugin Name',
			'PluginURI'   => 'Plugin URI',
			'Version'     => 'Version',
			'Description' => 'Description',
			'Author'      => 'Author',
			'AuthorURI'   => 'Author URI',
		);

		$plugin_meta = get_file_data( MASTER_FILE, $plugin_data, 'plugin' );

		if ( ! defined( 'CDP_VERSION' ) ) {
			define( 'CDP_VERSION', $plugin_meta['Version'] );
		}

		if ( ! defined( 'PER_PAGE_LIMIT' ) ) {
			define( 'PER_PAGE_LIMIT', 50 );
		}

		if ( ! defined( 'INVENTORY_DB_COLUMNS' ) ) {
			$inventory_db_columns = array(
				"blog_id" => "int(11) NOT NULL DEFAULT '1'",
				"vms_id" => "int(11) NOT NULL",
				"company_id" => "int(11) NOT NULL",
				"dealer_id" => "int(11) NOT NULL",
				"dealer" => "varchar(255) NOT NULL",
				"description" => "text NOT NULL",
				"stock" => "varchar(255) NOT NULL",
				"vin" => "varchar(255) NOT NULL",
				"saleclass" => "varchar(255) NOT NULL",
				"certified" => "tinyint(1) NOT NULL DEFAULT '0'",
				"make" => "varchar(255) NOT NULL",
				"model_code" => "varchar(255) NOT NULL",
				"model" => "varchar(255) NOT NULL",
				"trim" => "varchar(255) NOT NULL",
				"year" => "int(11) NOT NULL",
				"askingprice" => "decimal(18,0) NOT NULL",
				"saleprice" => "decimal(18,0) NOT NULL",
				"retailprice" => "decimal(18,0) NOT NULL",
				"msrp" => "decimal(18,0) NOT NULL",
				"invoice" => "decimal(18,0) NOT NULL",
				"sortprice" => "decimal(18,0) NOT NULL",
				"pricesort" => "int(11) NOT NULL DEFAULT '0'",
				"sale_expire" => "varchar(255) NOT NULL",
				"headline" => "varchar(512) NOT NULL",
				"exterior" => "varchar(255) NOT NULL",
				"interior" => "varchar(255) NOT NULL",
				"color_map" => "varchar(255) NOT NULL",
				"color_map_int" => "varchar(255) NOT NULL",
				"body" => "varchar(255) NOT NULL",
				"vehicleclass" => "varchar(255) NOT NULL",
				"tags" => "varchar(350) NOT NULL",
				"drivetrain" => "varchar(255) NOT NULL",
				"doors" => "varchar(10) NOT NULL",
				"engine" => "varchar(255) NOT NULL",
				"transmission" => "varchar(255) NOT NULL",
				"fuel_type" => "varchar(255) NOT NULL",
				"highway" => "varchar(255) NOT NULL",
				"city" => "varchar(255) NOT NULL",
				"odometer" => "int(11) NOT NULL",
				"carfax" => "varchar(255) NOT NULL",
				"carfax_image_url" => "varchar(255) NOT NULL",
				"autocheck" => "varchar(255) NOT NULL",
				"acode" => "varchar(255) NOT NULL",
				"thumbnail" => "varchar(350) NOT NULL",
				"image_medium" => "varchar(350) NOT NULL",
				"geo_state" => "varchar(255) NOT NULL",
				"geo_city" => "varchar (255) NOT NULL",
				"geo_zip" => "varchar (255) NOT NULL",
				"location" => "varchar (255) NOT NULL",
				"created" => "datetime NOT NULL",
				"updated" => "datetime NOT NULL",
				"sold_on" => "varchar (10) DEFAULT ''",
				"sort" => "int(11) NOT NULL DEFAULT '0'",
				"extra" => "text NOT NULL",
				"vehicle_url" => "varchar (255) NOT NULL",
				"payment" => "decimal(18,0) NOT NULL",
				"video" => "varchar(255) NOT NULL,"
			);
			define( 'INVENTORY_DB_COLUMNS', $inventory_db_columns );
		}

	}
}
