<?php
/**
 * Class for Generate Buy Now Link
 *
 * @package  smart-offers/modules/buy-now/php/admin/
 * @since    4.0.0
 * @version  1.1.2
 */

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

if ( ! class_exists( 'SA_WC_Generate_Buy_Now_Link' ) ) {

	/**
	 * Class for Generate Buy Now Link
	 */
	class SA_WC_Generate_Buy_Now_Link {

		/**
		 * Variable to hold instance of Generate Buy Now Link
		 *
		 * @var $instance
		 */
		private static $instance = null;

		/**
		 * Constructor
		 */
		public function __construct() {
			add_action( 'woocommerce_admin_field_bn_click_to_copy_button', array( $this, 'bn_click_to_copy_button' ) );
		}

		/**
		 * Get single instance of Generate Buy Now Link.
		 *
		 * @return SA_WC_Generate_Buy_Now_Link Singleton object of SA_WC_Generate_Buy_Now_Link
		 */
		public static function get_instance() {

			// Check if instance is already exists.
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Function to display create buy now link settings
		 */
		public function render_bn_create_link_fields() {
			?>
			<div class="wrap woocommerce">
				<h1 class="wp-heading-inline"><?php echo esc_html__( 'Create a Buy Now Link', 'smart-offers' ); ?></h1>
				<div id="wc_buy_now_product_data">
					<?php woocommerce_admin_fields( $this->get_bn_create_link_settings() ); ?>
				</div>
			</div>
			<?php
		}

		/**
		 * Function to get settings for create buy now link tab.
		 */
		public function get_bn_create_link_settings() {

			$shipping_methods = (array) WC()->shipping->load_shipping_methods();
			$shipping_methods = wp_list_pluck( $shipping_methods, 'method_title', 'id' );
			$shipping_methods = array( '' => '' ) + $shipping_methods;

			$pages = (array) get_pages();
			$pages = wp_list_pluck( $pages, 'post_title', 'ID' );
			$pages = array( '' => '' ) + $pages;

			$settings = array(
				array(
					'type' => 'title',
					'id'   => 'wc_buy_now_create_link',
					/* translators: 1. a tag opening with BN link, 2. View docs, 3. a tag closing */
					'desc' => sprintf( __( 'Generate a special Buy Now link to share in blog posts, pages, emails, social media promotions or elsewhere. %1$1s%2$2s%3$3s', 'smart-offers' ), '<a href="https://www.storeapps.org/knowledgebase_category/woocommerce-buy-now/?utm_source=so&utm_medium=in_app&utm_campaign=view_docs" target="_blank">', __( 'View docs', 'smart-offers' ), '</a>' ),
				),
				array(
					'name'              => __( 'Select products to add to cart', 'smart-offers' ),
					'desc'              => __( 'Select one or more products that you would like ordered with this link.', 'smart-offers' ),
					'id'                => 'buy_now_generate_link_product',
					'type'              => 'multiselect',
					'options'           => array(),
					'class'             => 'select2_search_products_coupons',
					'custom_attributes' => array(
						'data-placeholder' => esc_attr__( 'Search for a product&hellip;', 'smart-offers' ),
						'data-action'      => 'wc_buy_now_json_search_products_and_variations',
						'data-security'    => esc_attr( wp_create_nonce( 'search-products' ) ),
					),
					'css'               => 'min-width:300px;',
					'desc_tip'          => true,
				),
				array(
					'name'              => __( 'Select coupons', 'smart-offers' ),
					'desc'              => __( 'Optionally, select one or more coupons that you want to apply with this link.', 'smart-offers' ),
					'id'                => 'buy_now_generate_link_coupon',
					'type'              => 'multiselect',
					'options'           => array(),
					'class'             => 'select2_search_products_coupons',
					'custom_attributes' => array(
						'data-placeholder' => esc_attr__( 'Search for a coupon&hellip;', 'smart-offers' ),
						'data-action'      => 'woocommerce_json_search_coupons',
						'data-security'    => esc_attr( wp_create_nonce( 'search-coupons' ) ),
					),
					'css'               => 'min-width:300px;',
					'desc_tip'          => true,
				),
				array(
					'name'              => __( 'Shipping method', 'smart-offers' ),
					'desc'              => __( 'Optionally, select shipping method for the order.', 'smart-offers' ),
					'id'                => 'buy_now_shipping_method',
					'type'              => 'select',
					'class'             => 'wc-enhanced-select-nostd',
					'options'           => $shipping_methods,
					'custom_attributes' => array(
						'data-placeholder' => esc_attr__( 'Select a shipping method&hellip;', 'smart-offers' ),
					),
					'css'               => 'min-width:300px;',
					'desc_tip'          => true,
				),
				array(
					'name'              => __( 'Redirect to a page', 'smart-offers' ),
					'desc'              => __( 'Optionally, select page you want to redirect users to when "Buy Now" button is clicked. Leaving it blank, will complete checkout for logged in users whose previous payment details are found and for guest users, they will be taken to the checkout page.', 'smart-offers' ),
					'id'                => 'buy_now_redirect_link',
					'type'              => 'single_select_page',
					'class'             => 'wc-enhanced-select-nostd',
					'options'           => $pages,
					'custom_attributes' => array(
						'data-placeholder' => __( 'Select a page&hellip', 'smart-offers' ),
					),
					'css'               => 'min-width:300px;',
					'desc_tip'          => true,
				),
				array(
					'name'              => __( 'Here\'s your Buy Now link', 'smart-offers' ),
					'desc'              => __( 'Copy this link and use wherever you want.', 'smart-offers' ),
					'id'                => 'buy_now_generated_link',
					'type'              => 'textarea',
					'custom_attributes' => array(
						'readonly' => 'readonly',
					),
					'css'               => 'min-width:600px;',
					'desc_tip'          => true,
				),
				array(
					'type' => 'bn_click_to_copy_button',
				),
				array(
					'type' => 'sectionend',
					'id'   => 'wc_buy_now_create_link',
				),
			);

			return $settings;
		}

		/**
		 * Function to display click to copy button.
		 */
		public function bn_click_to_copy_button() {
			?>
			<tr valign="top">
				<th scope="row" class="titledesc"></th>
				<td style="padding-top:0">
					<button class="button button-primary button-hero bn-click-to-copy-btn" id="bn-click-to-copy-btn" data-clipboard-action="copy" data-clipboard-target="#buy_now_generated_link" disabled="disabled"><?php echo esc_html__( 'Click to copy', 'smart-offers' ); ?></button>
				</td>
			</tr>
			<?php
		}
	}
}
