<?php
/**
 * Created by PhpStorm.
 * User: Villatheme-Thanh
 * Date: 20-03-19
 * Time: 9:35 AM
 */

namespace WACVP\Inc\Execute;

use WACVP\Inc\Query_DB;
use WACVP\Inc\Data;

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

class Guest {

	protected static $instance = null;

	public $query;
	public $settings;
	protected $language;

	private function __construct() {
		$this->settings = Data::get_params();
		$this->query    = Query_DB::get_instance();
		$this->language = '';
		if ( ! isset( $_COOKIE['wacv_skip_track_data'] ) ) {

			add_action( 'wp_ajax_nopriv_wacv_get_info', array( $this, 'get_info' ) );

			add_action( 'wp_ajax_wacv_get_info', array( $this, 'get_info' ) );

			add_action( 'wp_ajax_nopriv_wacv_update_gdpr', array( $this, 'update_gdpr' ) );
			add_action( 'wp_ajax_wacv_update_gdpr', array( $this, 'update_gdpr' ) );

			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_file' ) );

			add_filter( 'woocommerce_checkout_fields', array( $this, 'guest_checkout_fields' ) );

			add_action( 'init', array( $this, 'get_email' ) );
			add_action( 'woo_lucky_wheel_get_email', array( $this, 'woo_lucky_wheel_get_email' ), 10, 3 );
		}
	}

	public static function get_instance() {

		// If the single instance hasn't been set, set it now.
		if ( null == self::$instance ) {
			self::$instance = new self;
		}

		return self::$instance;
	}

	public function enqueue_file() {
		if ( is_checkout() ) {
			$guest_info = $this->query::get_session( 'guest_info' );
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
				$default_lang     = apply_filters( 'wpml_default_language', null );
				$current_language = apply_filters( 'wpml_current_language', null );

				if ( $current_language && $current_language !== $default_lang ) {
					$this->language = $current_language;
				}
			} else if ( class_exists( 'Polylang' ) ) {
				$default_lang     = pll_default_language( 'slug' );
				$current_language = pll_current_language( 'slug' );
				if ( $current_language && $current_language !== $default_lang ) {
					$this->language = $current_language;
				}
			}
			wp_enqueue_script( WACVP_SLUG . '-get-guest-info', WACVP_JS . 'get-guest-info.js', array( 'jquery' ), WACVP_VERSION, true );
			wp_localize_script( WACVP_SLUG . '-get-guest-info', 'wacv_localize', array(
				'ajax_url'              => admin_url( 'admin-ajax.php?action=wacv_ajax' ),
				'nonce'                 => wp_create_nonce( 'wacv-nonce' ),
				'enable_gdpr'           => $this->settings['enable_checkout_gdpr'],
				'direct_trigger'        => is_plugin_active( 'flux-checkout/flux-checkout.php' ),
				'checkout_gdpr_message' => ! empty( $this->language ) && isset( $this->settings[ 'checkout_gdpr_message_' . $this->language ] ) ? $this->settings[ 'checkout_gdpr_message_' . $this->language ] : $this->settings['checkout_gdpr_message'],
				'checkout_gdpr_cancel'  => ! empty( $this->language ) && isset( $this->settings[ 'checkout_gdpr_cancel_' . $this->language ] ) ? $this->settings[ 'checkout_gdpr_cancel_' . $this->language ] : $this->settings['checkout_gdpr_cancel'],
				'guest_info'            => $guest_info
			) );
		}
	}

	public function get_email() {
		//Compatible with Lucky wheel
		if ( isset( $_REQUEST['_wacv_admin_nonce'] ) && ! wp_verify_nonce( wc_clean( wp_unslash( $_REQUEST['_wacv_admin_nonce'] ) ), 'wacv_admin_nonce' ) ) {
			return;
		}
		if ( class_exists( 'Woocommerce_Lucky_Wheel' ) ) {
			//Compatible with Lucky wheel
			if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'wlwl_get_email' ) {
				$data                       = wc_clean( $_REQUEST );
				$data['billing_email']      = isset( $data['user_email'] ) ? $data['user_email'] : '';
				$data['billing_first_name'] = isset( $data['user_name'] ) ? $data['user_name'] : '';
				$this->subscribe_data( $data );
			}
		}
		//Compatible with coupon box
		if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'wcb_email' ) {
			$data                  = wc_clean( $_REQUEST );
			$data['billing_email'] = isset( $data['email'] ) ? $data['email'] : '';
			$this->subscribe_data( $data );
		}

		if ( is_plugin_active( 'mailchimp-for-wp/mailchimp-for-wp.php' ) && isset( $_POST['_mc4wp_form_id'] ) ) {
			$data                       = wc_clean( $_POST );
			$data['billing_email']      = isset( $data['EMAIL'] ) ? $data['EMAIL'] : '';
			$data['billing_phone']      = isset( $data['PHONE'] ) ? $data['PHONE'] : '';
			$data['billing_city']       = isset( $data['ADDRESS']['city'] ) ? $data['ADDRESS']['city'] : '';
			$data['billing_country']    = isset( $data['ADDRESS']['country'] ) ? $data['ADDRESS']['country'] : '';
			$data['billing_first_name'] = isset( $data['FNAME'] ) ? $data['FNAME'] : '';
			$data['billing_last_name']  = isset( $data['LNAME'] ) ? $data['LNAME'] : '';
			$this->subscribe_data( $data );
		}
	}

	public function woo_lucky_wheel_get_email( $email, $name, $mobile ) {
		if ( ! ( $email || $mobile ) ) {
			return;
		}
		if ( isset( $_REQUEST['_wacv_admin_nonce'] ) && ! wp_verify_nonce( wc_clean( wp_unslash( $_REQUEST['_wacv_admin_nonce'] ) ), 'wacv_admin_nonce' ) ) {
			return;
		}
		//Compatible with Lucky wheel
		if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'wlwl_get_email' ) {
			$data['billing_email']      = $email;
			$data['billing_first_name'] = $name;
			$data['billing_phone']      = $mobile;
			$this->subscribe_data( $data );
		}
	}

	public function subscribe_data( $data ) {
		$this->save_info( $data );
	}

	public function save_info( $data ) {
		$update_user = '';
		$cookie_time = current_time( 'timestamp' ) + 86400;
		setCookie( 'wacv_get_email', true, $cookie_time, '/' );

		if ( ! is_user_logged_in() ) {
			if ( session_id() == '' ) {
				session_start();
			}

			WC()->session->set_customer_session_cookie( true );

			$guest_info = array();
			$patterns   = array(
				'billing_first_name',
				'billing_last_name',
				'billing_company',
				'billing_address_1',
				'billing_address_2',
				'billing_city',
				'billing_state',
				'billing_postcode',
				'billing_country',
				'billing_phone',
				'billing_phone_short',
				'billing_email',
				'order_notes',
				'shipping_first_name',
				'shipping_last_name',
				'shipping_company',
				'shipping_address_1',
				'shipping_address_2',
				'shipping_city',
				'shipping_state',
				'shipping_postcode',
				'shipping_country',
				'ship_to_billing',
				'user_ref',
				'status',
			);

			foreach ( $data as $key => $value ) {
				if ( in_array( $key, $patterns ) && $data[ $key ] != '' ) {
					if ( $key != 'billing_phone' ) {
						$guest_info[ $key ] = sanitize_text_field( $data[ $key ] );
					} else {
						$guest_info[ $key ] = preg_replace( '/[^+|0-9]/', '', sanitize_text_field( $data[ $key ] ) );
						if ( isset( $data['billing_phone_full_number'] ) && ! empty( $data['billing_phone_full_number'] ) ) {
							$phone_full_number  = sanitize_text_field( $data['billing_phone_full_number'] );
							$guest_info[ $key ] = preg_replace( '/[^+|0-9]/', '', $phone_full_number );
						}
					}
				}
			}

			$this->query::set_session( 'guest_info', $guest_info );

			$guest_info = $this->query::get_session( 'guest_info' );

			$email_address = $guest_info['billing_email'] ?? '';
			$user_ref      = $guest_info['user_ref'] ?? '';
			$user_phone    = isset( $guest_info['billing_phone'] ) && $guest_info['billing_phone'] && strlen( $guest_info['billing_phone'] ) > 5 ? $guest_info['billing_phone'] : '';

			$user_id = '';
			if ( $this->settings['tracking_require_email'] && ( empty( $email_address ) || ! is_email( $email_address ) ) ) {
				return false;
			}
			if ( is_email( $email_address ) || ! empty( $user_ref ) || $user_phone ) {
				if ( $this->query::get_session( 'user_id' ) ) {
					$user_id     = $this->query::get_session( 'user_id' );
					$update_user = $this->query->update_guest_info( $user_id, $guest_info );
				} else {
					$user_id = ! $user_id && $email_address ? $this->query->get_user_by_email( $email_address ) : '';
					$user_id = ! $user_id && $user_phone ? $this->query->get_user_by_phone( $user_phone ) : $user_id;
					$user_id = ! $user_id && $user_ref ? $this->query->get_user_by_user_ref( $user_ref ) : $user_id;

					if ( $user_id ) {
						$update_user = $this->query->update_guest_info( $user_id, $guest_info );
					} else {
						$user_id = $this->query->insert_guest_info( $guest_info );
					}
				}

				if ( is_email( $email_address ) ) {
					wc()->customer->set_billing_email( $email_address );
				}
				wc()->session->set( 'user_id', $user_id );
				$abdc_id = wc()->session->get( 'wacv_cart_record_id' );

				$user = $this->query->get_guest_info( $user_id );
				if ( is_object( $user ) && property_exists( $user, 'status' ) && $user->status !== 'unsubscribe' ) {
					$this->query->update_abd_cart_record(
						array( 'abandoned_cart_time' => WACVP_CURRENT_TIME, 'user_id' => $user_id ),
						array( 'id' => $abdc_id ) );
				}

				if ( $email_address ) {
					$guests = $this->query->get_guest_info_rows( $email_address );
					if ( $guests ) {
						foreach ( $guests as $guest ) {
							if ( count( $this->query->get_guest_same_email( $guest->id, 0 ) ) ) {
								if ( $guest->id == $user_id ) {
									continue;
								}
								$this->query->update_abd_cart_record(
									array( 'cart_ignored' => 1 ),
									array( 'user_id' => $guest->id, 'user_type' => 'guest' )
								);
							}
						}
					}
				}

				$get_cookie = WC()->session->get_session_cookie();

				if ( isset( $get_cookie[0] ) && '' != $get_cookie[0] ) {
					$this->query->update_cart_log( array( 'user_id' => $user_id ), array( 'user_id' => $get_cookie[0] ) );
				}

			}
		} else {
			if ( isset( $data['user_ref'] ) ) {
				$user_ref    = sanitize_text_field( $data['user_ref'] );
				$user_id     = get_current_user_id();
				$update_user = update_user_meta( $user_id, 'wacv_user_ref', $user_ref );
				$guest_info  = array( 'user_ref' => $user_ref );
				$this->query::set_session( 'guest_info', $guest_info );
			}
		}

		return $update_user;
	}

	public function get_info() {
		check_ajax_referer( 'wacv-nonce', 'nonce' );

		$data = wc_clean( $_POST );
		if ( empty( $data['billing_country'] ) ) {
			$ip                      = \WC_Geolocation::get_ip_address();
			$user_geo                = \WC_Geolocation::geolocate_ip( $ip );
			$data['billing_country'] = $user_geo['country'];
		}
		$update_user = $this->save_info( $data );
		if ( ! $update_user && is_email( $data['billing_email'] ) && isset( $data['status'] ) && $data['status'] == 'subscribe' && $this->settings['enable_email_immediate'] ) {
			$headers [] = "Content-Type: text/html";

			if ( $this->settings['email_reply_address'] ) {
				$headers [] = "Reply-To: " . $this->settings['email_reply_address'];
			}
			if ( ! class_exists( 'WC_Email' ) ) {
				include_once dirname( WC_PLUGIN_FILE ) . '/includes/emails/class-wc-email.php';
			}

			$mail_subject = $this->settings['email_immediate_subject'];
			$mail_content = html_entity_decode( $this->settings['email_immediate_content'] );

			$multi_lang   = [];
			$lang_src     = '';
			$default_lang = '';
			$current_lang = '';
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
				global $sitepress;
				$lang_src       = 'wpml';
				$default_lang   = $sitepress->get_default_language();
				$wpml_languages = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );

				if ( count( $wpml_languages ) ) {
					foreach ( $wpml_languages as $key => $language ) {
						if ( $key != $default_lang ) {
							$multi_lang[] = $key;
						}
					}
				}
			} elseif ( class_exists( 'Polylang' ) ) {
				/*Polylang*/
				$lang_src      = 'polylang';
				$pll_languages = pll_languages_list();
				$default_lang  = pll_default_language( 'slug' );
				foreach ( $pll_languages as $language ) {
					if ( $language == $default_lang ) {
						continue;
					}
					$multi_lang[] = $language;
				}
			}
			if ( $lang_src == 'wpml' ) {
				$current_lang = apply_filters( 'wpml_current_language', null );;
			} elseif ( $lang_src == 'polylang' ) {
				$current_lang = pll_current_language();
			}
			if ( $multi_lang && ! empty( $current_lang ) ) {
				if ( $default_lang != $current_lang ) {
					$mail_subject = isset( $this->settings[ 'email_immediate_subject_' . $current_lang ] ) ?: $mail_subject;
					$mail_content = isset( $this->settings[ 'email_immediate_content_' . $current_lang ] ) ?: html_entity_decode( $mail_content );
				}
			}

			$mailer = new \WC_Email();
			$mailer->send( $data['billing_email'], $mail_subject, $mail_content, $headers, '' );
		}
		wp_send_json_success( $update_user );
		wp_die();
	}

	public function update_gdpr() {
		check_ajax_referer( 'wacv-nonce', 'nonce' );

		$abdc_id = wc()->session->get( 'wacv_cart_record_id' );

		$result = $this->query->get_abdc_record( $abdc_id ); //select record with cart_ignore = 0
		if ( $result ) {
			$this->query->remove_abd_record( $abdc_id );
		}

		setcookie( 'wacv_skip_track_data', 'true', 0, '/' );

		wp_send_json_success();
	}

	public function guest_checkout_fields( $fields ) {
		$guest_info = $this->query::get_session( 'guest_info' );
		if ( isset( $guest_info ) && $this->settings['enable_checkout_initial'] ) {
			if ( ! empty( $guest_info['billing_email'] ) ) {
				$_POST['billing_email'] = esc_html( $guest_info['billing_email'] );
			}
			if ( ! empty( $guest_info['billing_first_name'] ) ) {
				$_POST['billing_first_name'] = esc_html( $guest_info['billing_first_name'] );
			}
			if ( ! empty( $guest_info['billing_last_name'] ) ) {
				$_POST['billing_last_name'] = esc_html( $guest_info['billing_last_name'] );
			}
			if ( ! empty( $guest_info['billing_phone'] ) ) {
				if ( ! $this->settings['enable_fill_full_phone'] && ! empty( $guest_info['billing_phone_short'] ) ) {
					$_POST['billing_phone'] = esc_html( $guest_info['billing_phone_short'] );
				} else {
					$_POST['billing_phone'] = esc_html( $guest_info['billing_phone'] );
				}
			}
			if ( ! empty( $guest_info['billing_address_1'] ) ) {
				$_POST['billing_address_1'] = esc_html( $guest_info['billing_address_1'] );
			}
			if ( ! empty( $guest_info['billing_city'] ) ) {
				$_POST['billing_city'] = esc_html( $guest_info['billing_city'] );
			}
		}

		return $fields;
	}
}

