<?php

/*
Class Name: WOOMULTI_CURRENCY_Admin_Admin
Author: Andy Ha (support@villatheme.com)
Author URI: http://villatheme.com
Copyright 2015 villatheme.com. All rights reserved.
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class WOOMULTI_CURRENCY_Admin_Admin {
	protected $settings;

	function __construct() {
//		$this->settings = new WOOMULTI_CURRENCY_Data();
		$this->settings = WOOMULTI_CURRENCY_Data::get_ins();;
		add_filter(
			'plugin_action_links_woocommerce-multi-currency/woocommerce-multi-currency.php', array(
				$this,
				'settings_link'
			)
		);
		add_action( 'init', array( $this, 'init' ) );
		add_action( 'admin_init', array( $this, 'admin_init' ) );
		add_action( 'admin_notices', array( $this, 'cache_plugins_note' ) );
		add_action( 'admin_menu', array( $this, 'menu_page' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 99 );
		add_filter( 'woocommerce_general_settings', array( $this, 'woocommerce_general_settings' ) );
	}

	public function cache_plugins_note() {
		$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
		if ( $page != 'woocommerce-multi-currency' ) {
			return;
		}
		if ( is_plugin_active( 'wp-super-cache/wp-cache.php' ) && ! is_plugin_active( 'country-caching-extension-for-wp-super-cache/cc_wpsc_init.php' ) ) { ?>
			<div class="notice notice-warning">

				<div class="villatheme-content">
					<p>
						<?php echo __( 'You are using <strong>WP Super Cache</strong>. Please install and active <strong>Country Caching For WP Super Cache</strong> that helps <strong>WooCommerce Multi Currency</strong> is working fine with WP Super Cache.', 'woocommerce-multi-currency' ) ?>
					</p>
				</div>

			</div>
		<?php }

		if ( is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) { ?>
			<div class="notice notice-warning">

				<div class="villatheme-content">
					<p>
						<?php echo __( 'You are using <strong>WP Fastest Cache</strong>. Please make follow these steps these help <strong>WooCommerce Multi Currency</strong> is working fine with WP Fastest Cache.', 'woocommerce-multi-currency' ) ?>
					</p>
					<ul>
						<li><?php echo __( '1. In <strong>WooCommerce → Settings → General → Default customer location</strong> make sure you have selected: <strong>Geolocate with page caching support</strong>', 'woocommerce-multi-currency' ) ?></li>
						<li><?php echo __( '2. Open wp-config.php file via FTP then insert <strong>define(\'WPFC_CACHE_QUERYSTRING\', true);</strong>', 'woocommerce-multi-currency' ) ?></li>
					</ul>

				</div>

			</div>
		<?php }
	}

	/**
	 * Remove currency, decimal, posistion, setting in backend
	 *
	 * @param $datas
	 *
	 * @return mixed
	 */
	public function woocommerce_general_settings( $datas ) {
		foreach ( $datas as $k => $data ) {
			if ( isset( $data['id'] ) ) {
				if ( $data['id'] == 'woocommerce_currency' || $data['id'] == 'woocommerce_price_num_decimals' || $data['id'] == 'woocommerce_currency_pos' ) {
					unset( $datas[ $k ] );
				}
				if ( $data['id'] == 'pricing_options' ) {
					$datas[ $k ]['desc'] = esc_html__( 'The following options affect how prices are displayed on the frontend. Woo Multi Currency is working. Please go to ', 'woocommerce-multi-currency' ) . '<a href="' . admin_url( '?page=woocommerce-multi-currency' ) . '">' . esc_html__( 'WooCommerce Multi Currency setting page', 'woocommerce-multi-currency' ) . '</a>' . esc_html__( ' to set default currency.', 'woocommerce-multi-currency' );
				}
			}
		}

		return $datas;
	}

	/*Check Auto update*/
	public function admin_init() {

		$old_data = get_option( 'wmc_selected_currencies', array() );
		if ( count( $old_data ) ) {
			$currency         = $currency_rate = $currency_decimals = $currency_custom = $currency_pos = array();
			$currency_default = '';
			$by_countries     = json_decode( get_option( 'wmc_currency_by_country', array() ), true );

			/*Move Data Currency*/

			foreach ( $old_data as $k => $data ) {
				if ( $data['is_main'] == 1 ) {
					$currency_default = $k;
				}
				$currency[]          = $k;
				$currency_rate[]     = $data['rate'];
				$currency_decimals[] = $data['num_of_dec'];
				$currency_pos[]      = $data['pos'];
				if ( strpos( $data['custom_symbol'], '#PRICE#' ) === false ) {
					$currency_custom[] = '';
				} else {
					$currency_custom[] = $data['custom_symbol'];
				}
			}
			$by_country_args = array();
			/*Move Data Currency By Country*/
			foreach ( $by_countries as $code => $by_country ) {
				$by_country_args[ $code . '_by_country' ] = $by_country;
			}
			/*Move Key data*/
			if ( get_option( 'wmc_oder_id' ) && get_option( 'wmc_email' ) ) {
				$key = trim( get_option( 'wmc_oder_id' ) ) . ',' . trim( get_option( 'wmc_email' ) );
			} else {
				$key = '';
			}

			$args = array(
				'enable'                     => 1,
				'enable_fixed_price'         => 0,
				'currency_default'           => $currency_default,
				'currency'                   => $currency,
				'currency_rate'              => $currency_rate,
				'currency_decimals'          => $currency_decimals,
				'currency_custom'            => array(),
				'currency_pos'               => $currency_pos,
				'auto_detect'                => get_option( 'wmc_enable_approxi' ) ? get_option( 'wmc_enable_approxi' ) : 0,
				'enable_currency_by_country' => get_option( 'wmc_price_by_currency' ) == 'yes' ? 1 : 0,
				'enable_multi_payment'       => get_option( 'wmc_allow_multi' ) == 'yes' ? 1 : 0,
				'key'                        => $key,
				'update_exchange_rate'       => get_option( 'wmc_price_by_currency' ) == 'yes' ? 2 : 0,
				'enable_design'              => 0,
				'title'                      => '',
				'design_position'            => 0,
				'text_color'                 => '#fff',
				'background_color'           => '#212121',
				'main_color'                 => '#f78080',
				'flag_custom'                => ''
			);

			$args = array_merge( $args, $by_country_args );
			update_option( 'woo_multi_currency_params', $args );
			update_option( 'woo_multi_currency_old_version', 1 );
			delete_option( 'wmc_selected_currencies' );
		}
		/*Set currency again in backend*/
		if ( ! is_ajax() ) {
			$current_currency = get_option( 'woocommerce_currency' );
			$this->settings->set_current_currency( $current_currency );
		}
//		$params = new WOOMULTI_CURRENCY_Data();
		$params = WOOMULTI_CURRENCY_Data::get_ins();

		/*Check update*/
		$key         = $params->get_key();
		$setting_url = admin_url( '?page=woocommerce-multi-currency' );

		new VillaTheme_Plugin_Check_Update (
			WOOMULTI_CURRENCY_VERSION,                    // current version
			'https://villatheme.com/wp-json/downloads/v3',  // update path
			'woocommerce-multi-currency/woocommerce-multi-currency.php',                  // plugin file slug
			'woocommerce-multi-currency', '5455', $key, $setting_url
		);
		new VillaTheme_Plugin_Updater( 'woocommerce-multi-currency/woocommerce-multi-currency.php', 'woocommerce-multi-currency', $setting_url );
	}


	/**
	 * Init Script in Admin
	 */
	public function admin_enqueue_scripts() {
		$page = isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '';
		if ( $page == 'woocommerce-multi-currency' ) {
			global $wp_scripts;
			$scripts = $wp_scripts->registered;
			//			print_r($scripts);
			foreach ( $scripts as $k => $script ) {
				preg_match( '/^\/wp-/i', $script->src, $result );
				if ( count( array_filter( $result ) ) < 1 ) {
					wp_dequeue_script( $script->handle );
				}
			}

			/*Stylesheet*/
			wp_enqueue_style( 'woocommerce-multi-currency-button', WOOMULTI_CURRENCY_CSS . 'button.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-table', WOOMULTI_CURRENCY_CSS . 'table.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-transition', WOOMULTI_CURRENCY_CSS . 'transition.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-form', WOOMULTI_CURRENCY_CSS . 'form.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-icon', WOOMULTI_CURRENCY_CSS . 'icon.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-dropdown', WOOMULTI_CURRENCY_CSS . 'dropdown.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-checkbox', WOOMULTI_CURRENCY_CSS . 'checkbox.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-segment', WOOMULTI_CURRENCY_CSS . 'segment.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-menu', WOOMULTI_CURRENCY_CSS . 'menu.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-tab', WOOMULTI_CURRENCY_CSS . 'tab.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-input', WOOMULTI_CURRENCY_CSS . 'input.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-popup', WOOMULTI_CURRENCY_CSS . 'popup.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency-message', WOOMULTI_CURRENCY_CSS . 'message.min.css' );
			wp_enqueue_style( 'woocommerce-multi-currency', WOOMULTI_CURRENCY_CSS . 'woocommerce-multi-currency-admin.css' );
			wp_enqueue_style( 'select2', WOOMULTI_CURRENCY_CSS . 'select2.min.css' );

			wp_enqueue_script( 'select2' );
			wp_enqueue_script( 'woocommerce-multi-currency-transition', WOOMULTI_CURRENCY_JS . 'transition.min.js', array( 'jquery' ) );
			wp_enqueue_script( 'woocommerce-multi-currency-dropdown', WOOMULTI_CURRENCY_JS . 'dropdown.js', array( 'jquery' ) );
			wp_enqueue_script( 'woocommerce-multi-currency-checkbox', WOOMULTI_CURRENCY_JS . 'checkbox.js', array( 'jquery' ) );
			wp_enqueue_script( 'woocommerce-multi-currency-tab', WOOMULTI_CURRENCY_JS . 'tab.js', array( 'jquery' ) );
			wp_enqueue_script( 'woocommerce-multi-currency-address', WOOMULTI_CURRENCY_JS . 'jquery.address-1.6.min.js', array( 'jquery' ) );
			wp_enqueue_script( 'jquery-ui-sortable' );
			wp_enqueue_script( 'woocommerce-multi-currency', WOOMULTI_CURRENCY_JS . 'woocommerce-multi-currency-admin.js', array( 'jquery' ), WOOMULTI_CURRENCY_VERSION );
			/*Color picker*/
			wp_enqueue_script(
				'iris', admin_url( 'js/iris.min.js' ), array(
				'jquery-ui-draggable',
				'jquery-ui-slider',
				'jquery-touch-punch'
			), false, 1
			);

		}

	}

	/**
	 * Link to Settings
	 *
	 * @param $links
	 *
	 * @return mixed
	 */
	public function settings_link( $links ) {
		$settings_link = '<a href="admin.php?page=woocommerce-multi-currency" title="' . __( 'Settings', 'woocommerce-multi-currency' ) . '">' . __( 'Settings', 'woocommerce-multi-currency' ) . '</a>';
		array_unshift( $links, $settings_link );

		return $links;
	}


	/**
	 * Function init when run plugin+
	 */
	function init() {
		/*Register post type*/

		load_plugin_textdomain( 'woocommerce-multi-currency' );
		$this->load_plugin_textdomain();


	}


	/**
	 * load Language translate
	 */
	public function load_plugin_textdomain() {
		$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-multi-currency' );
		// Global + Frontend Locale
		load_textdomain( 'woocommerce-multi-currency', WOOMULTI_CURRENCY_LANGUAGES . "woocommerce-multi-currency-$locale.mo" );
		load_plugin_textdomain( 'woocommerce-multi-currency', false, WOOMULTI_CURRENCY_LANGUAGES );
	}

	/**
	 * Register a custom menu page.
	 */
	public function menu_page() {
		add_menu_page(
			esc_html__( 'WooCommerce Multi Currency', 'woocommerce-multi-currency' ), esc_html__( 'Multi Currency', 'woocommerce-multi-currency' ), 'manage_options', 'woocommerce-multi-currency', array(
			'WOOMULTI_CURRENCY_Admin_Settings',
			'page_callback'
		), WOOMULTI_CURRENCY_IMAGES . 'icon.svg', 2
		);

	}

}

?>