<?php

// Exit if accessed directly
if (!defined('ABSPATH')) exit;

class MXCPFCMetaboxesclass
{

	public static $currencies = ['usd', 'aed', 'afn', 'all', 'amd', 'ang', 'aoa', 'ars', 'aud', 'awg', 'azn', 'bam', 'bbd', 'bdt', 'bgn', 'bhd', 'bif', 'bmd', 'bnd', 'bob', 'brl', 'bsd', 'bwp', 'byn', 'bzd', 'cad', 'cdf', 'chf', 'clp', 'cny', 'cop', 'crc', 'cve', 'czk', 'djf', 'dkk', 'dop', 'dzd', 'egp', 'etb', 'eur', 'fjd', 'fkp', 'gbp', 'gel', 'gip', 'gmd', 'gnf', 'gtq', 'gyd', 'hkd', 'hnl', 'hrk', 'htg', 'huf', 'idr', 'ils', 'inr', 'isk', 'jmd', 'jod', 'jpy', 'kes', 'kgs', 'khr', 'kmf', 'krw', 'kwd', 'kyd', 'kzt', 'lak', 'lbp', 'lkr', 'lrd', 'lsl', 'mad', 'mdl', 'mga', 'mkd', 'mmk', 'mnt', 'mop', 'mro', 'mur', 'mvr', 'mwk', 'mxn', 'myr', 'mzn', 'nad', 'ngn', 'nio', 'nok', 'npr', 'nzd', 'omr', 'pab', 'pen', 'pgk', 'php', 'pkr', 'pln', 'pyg', 'qar', 'ron', 'rsd', 'rub', 'rwf', 'sar', 'sbd', 'scr', 'sek', 'sgd', 'shp', 'sle', 'sos', 'srd', 'std', 'szl', 'thb', 'tjs', 'tnd', 'top', 'try', 'ttd', 'twd', 'tzs', 'uah', 'ugx', 'uyu', 'uzs', 'vnd', 'vuv', 'wst', 'xaf', 'xcd', 'xof', 'xpf', 'yer', 'zar', 'zmw', 'usdc', 'btn', 'ghs', 'eek', 'lvl', 'svc', 'vef', 'ltl', 'sll'];

	/*
	* MXCPFCMetaboxesclass constructor
	*/
	public function __construct()
	{
	}

	/*
	* Create Metaboxes
	*/
	public static function createMetaboxes()
	{

		add_action('add_meta_boxes', array('MXCPFCMetaboxesclass', 'mxcpfc_metaboxes_init'));

		// save metabox data of amount
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_of_amount_save'));

		// save metabox data of URL to client
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_url_to_client_save'));

		// save metabox data of customer email
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_customer_email_save'));

		// save metabox data of invoice number
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_invoice_number_save'));

		// save metabox data of invoice number
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_currency_save'));

		// save metabox data of offer
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_offer_save'));

		// save metabox data of offer
		add_action('save_post', array('MXCPFCMetaboxesclass', 'meta_data_url_hash_save'));
	}

	/*
		* Metabox initialization
		*/
	public static function mxcpfc_metaboxes_init()
	{

		// metabox of meta url
		add_meta_box(
			'meta_url_hash',
			'Url Hash',
			array('MXCPFCMetaboxesclass', 'metabox_of_meta_url_hash'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox currency
		add_meta_box(
			'meta_currency',
			'Currency',
			array('MXCPFCMetaboxesclass', 'metabox_currency'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox of amount
		add_meta_box(
			'meta_of_amount',
			'Price amount (how much it costs)',
			array('MXCPFCMetaboxesclass', 'metabox_of_amount'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox of URL to client
		add_meta_box(
			'meta_url_to_client',
			'URL to client (autogenerated)',
			array('MXCPFCMetaboxesclass', 'metabox_of_url_to_client'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox of customer email
		add_meta_box(
			'meta_customer_email',
			'Customer email',
			array('MXCPFCMetaboxesclass', 'metabox_of_customer_email'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox of invoice number
		add_meta_box(
			'meta_invoice_number',
			'Invoice number',
			array('MXCPFCMetaboxesclass', 'metabox_of_invoice_number'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox of offer
		add_meta_box(
			'meta_offer',
			'Offer',
			array('MXCPFCMetaboxesclass', 'metabox_offer'),
			'mxcpfc_payment',
			'normal',
			'default'
		);

		// metabox has sent
		add_meta_box(
			'meta_sent_to_client',
			'Sending payment request to client',
			array('MXCPFCMetaboxesclass', 'metabox_sent_to_client'),
			'mxcpfc_payment',
			'normal',
			'default'
		);
	}

	/*
		* Metabox of sent to client
		*/
	public static function metabox_sent_to_client($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_sent_to_client_data', true);

		wp_nonce_field('meta_sent_to_client_action', 'meta_sent_to_client_nonce');

		// payment confirm
		$data_payment_confirm = get_post_meta($post->ID, '_meta_bill_confirm', true);

		if ($data_payment_confirm == 'confirm') { ?>

			<h4><u>Client already has paid for this bill.</u></h4>
			<h5>If you want to add new payment request, please, create new payment item.</h5>

		<?php } else {

			$payment_options = get_option('_mx_create_paymetn_options');

			if ($payment_options) {

				if (esc_attr($data) == '') {

					echo '<p id="mx_send_payment_to_client_text">Do you want to send this payment to the client?</p>';

					echo '<p><button class="button button-primary button-large" id="mx_send_payment_to_client" data-post-id="' . $post->ID . '">Send Payment</button></p>';
				} else {

					echo '<p id="mx_send_payment_to_client_text">You have sent payment to the client. Do you want do it one more time?</p>';

					echo '<p><button class="button button-primary button-large" id="mx_send_payment_to_client" data-post-id="' . $post->ID . '">Send Payment Again</button></p>';
				}
			} else {

				echo 'Please, <a href="' . get_admin_url() . 'options-general.php?page=mxcpfc_payment_settings">set up the payment options</a>.';
			}
		}
	}
	/*____________________________________________________________________*/

	/*
		* Metabox of offer
		*/
	public static function metabox_offer($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_offer_data', true);

		wp_nonce_field('meta_offer_action', 'meta_offer_nonce');

		echo '<p>Offer: <input type="text" data-invoice-number="' . $post->ID . '" name="meta_of_offer_field" id="meta_of_offer_field" value="'
			. esc_attr($data) . '" required /></p>';
	}

	// save meta of invoice number
	public static function meta_data_offer_save($postID)
	{

		if (!isset($_POST['meta_of_offer_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = sanitize_text_field($_POST['meta_of_offer_field']);

		update_post_meta($postID, '_meta_offer_data', $data);
	}
	/*____________________________________________________________________*/

	/*
		* Metabox of invoice number
		*/
	public static function metabox_of_invoice_number($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_invoice_number_data', true);

		wp_nonce_field('meta_invoice_number_action', 'meta_invoice_number_nonce');

		echo '<p>Invoice number: <input type="text" data-invoice-number="' . $post->ID . '" name="meta_of_invoice_number_field" id="meta_of_invoice_number_field" value="'
			. esc_attr($data) . '" readonly /></p>';
	}

	// save meta of invoice number
	public static function meta_data_invoice_number_save($postID)
	{

		if (!isset($_POST['meta_of_invoice_number_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = sanitize_text_field($_POST['meta_of_invoice_number_field']);

		update_post_meta($postID, '_meta_invoice_number_data', $data);
	}
	/*____________________________________________________________________*/

	/*
		* Metabox of customer email
		*/
	public static function metabox_of_customer_email($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_customer_email_data', true);

		wp_nonce_field('meta_customer_email_action', 'meta_customer_email_nonce');

		echo '<p>Customer email: <input type="email" name="meta_of_customer_email_field" id="meta_of_customer_email_field" value="'
			. esc_attr($data) . '" required /></p>';
	}

	// save meta of customer email
	public static function meta_data_customer_email_save($postID)
	{

		if (!isset($_POST['meta_of_customer_email_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = sanitize_email($_POST['meta_of_customer_email_field']);

		update_post_meta($postID, '_meta_customer_email_data', $data);
	}
	/*____________________________________________________________________*/

	/*
		* Metabox of URL to client
		*/
	public static function metabox_of_meta_url_hash($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_url_hash_data', true);

		wp_nonce_field('meta_url_hash_action', 'meta_url_hash_nonce');

		echo '<p>Url Hash: <input type="text" data-url-path="' . get_home_url() . '/wordpress/' . self::get_payment_options()['process_page_url'] . '/" name="meta_of_url_hash_field" id="meta_of_url_hash_field" value="'
			. esc_attr($data) . '" readonly required /></p>';
	}

	// save meta of URL to client
	public static function meta_data_url_hash_save($postID)
	{

		if (!isset($_POST['meta_of_url_hash_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = sanitize_text_field($_POST['meta_of_url_hash_field']);

		update_post_meta($postID, '_meta_url_hash_data', $data);
	}
	/*____________________________________________________________________*/


	/*
		* Metabox of URL to client
		*/
	public static function metabox_of_url_to_client($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_url_to_client_data', true);

		wp_nonce_field('meta_url_to_client_action', 'meta_url_to_client_nonce');

		echo '<p>URL to client: <input type="text" data-url-path="' . get_home_url() . '/wordpress/' . self::get_payment_options()['process_page_url'] . '/" name="meta_of_url_to_client_field" id="meta_of_url_to_client_field" value="'
			. esc_attr($data) . '" readonly required /></p>';
	}

	// save meta of URL to client
	public static function meta_data_url_to_client_save($postID)
	{

		if (!isset($_POST['meta_of_url_to_client_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = esc_url_raw($_POST['meta_of_url_to_client_field']);

		update_post_meta($postID, '_meta_url_to_client_data', $data);
	}
	/*____________________________________________________________________*/

	/*
		* Metabox of amount
		*/
	public static function metabox_of_amount($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_of_amount_data', true);

		wp_nonce_field('meta_of_amount_action', 'meta_of_amount_nonce');

		echo '<p>Price amount: <input type="text" name="meta_of_amount_field" id="meta_of_amount_field" value="'
			. esc_attr($data) . '" required /></p>';
	}

	// save meta of amount
	public static function meta_data_of_amount_save($postID)
	{

		if (!isset($_POST['meta_of_amount_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = sanitize_text_field($_POST['meta_of_amount_field']);

		update_post_meta($postID, '_meta_of_amount_data', $data);
	}
	/*____________________________________________________________________*/

	/*
		* Metabox of currency
		*/
	public static function metabox_currency($post, $box)
	{

		$data = get_post_meta($post->ID, '_meta_currency_data', true);

		wp_nonce_field('meta_currency_action', 'meta_currency_nonce'); ?>

		<select name="meta_currency_field" id="meta_currency_field" required>
			<?php foreach (self::$currencies as $currency) : ?>
				<option value="<?php echo $currency; ?>" <?php echo $currency == $data ? 'selected' : ''; ?>><?php echo $currency; ?></option>
			<?php endforeach; ?>
		</select>

<?php

	}

	// save meta currency
	public static function meta_data_currency_save($postID)
	{

		if (!isset($_POST['meta_currency_field']))
			return;

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
			return;

		if (wp_is_post_revision($postID))
			return;

		if (!current_user_can('edit_post', $postID))
			return;

		$data = sanitize_text_field($_POST['meta_currency_field']);

		update_post_meta($postID, '_meta_currency_data', $data);
	}
	/*____________________________________________________________________*/


	public static function get_payment_options()
	{
		$payment_options = get_option('_mx_create_paymetn_options');

		if ($payment_options) {

			$unserialize_options = maybe_unserialize($payment_options);

			return $unserialize_options;
		}

		return array(
			'publishable_key' => '',
			'secret_key' => '',
			'process_page_url' => '',
			'company_email' => '',
			'department_company' => '',
			'company_name' => '',
			'message_for_client' => ''
		);
	}
}
