<?php // phpcs:ignore WordPress.Files.FileName
/**
 * The class that manage the Payout shortcode
 *
 * @package YITH\PayPalPayouts\Classes
 */

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

if ( ! class_exists( 'YITH_Payouts_Shortcodes' ) ) {
	/**
	 * Class YITH_Payouts_Shortcodes
	 */
	class YITH_Payouts_Shortcodes {
		/**
		 * YITH_Payouts_Shortcodes constructor.
		 */
		public function __construct() {
			add_shortcode( 'yith_payout_transactions', array( $this, 'payout_transaction' ) );
		}

		/**
		 * Show the payout transaction history
		 *
		 * @param array $atts The shortcode args.
		 *
		 * @return string
		 * @author YITH <plugins@yithemes.com>
		 * @since 1.0.0
		 */
		public function payout_transaction( $atts ) {

			$atts = shortcode_atts(
				array(
					'per_page'     => 10,
					'pagination'   => 'no',
					'current_page' => 1,
				),
				$atts
			);

			extract( $atts ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract

			$current_page = empty( $current_page ) ? 1 : $current_page;

			if ( is_user_logged_in() ) {

				$query_args     = array();
				$user_id        = get_current_user_id();
				$receiver_email = apply_filters( 'yith_payout_receiver_email', $this->get_receiver_email( $user_id ), $user_id );

				if ( ! empty( $receiver_email ) ) {

					$query_args['receiver'] = $receiver_email;
					$query_args['fields']   = 'count';

					$tot_items = YITH_Payout_Items()->get_payout_items( $query_args );

					$query_args['fields']  = array(
						'payout_item_id',
						'transaction_id',
						'amount',
						'currency',
						'transaction_status',
					);
					$query_args['orderby'] = 'ID';
					$query_args['order']   = 'DESC';
					$pages                 = 1;
					if ( 'yes' === $pagination && $tot_items > $per_page ) {

						if ( is_account_page() ) {

							$base_url = wc_get_endpoint_url( 'payouts', '%#%' );
							$format   = '%#%';
						} else {
							$base_url = esc_url( add_query_arg( array( 'paged' => '%#%' ), get_the_permalink( $post->ID ) ) );
							$format   = '?paged=%#%';
						}
						$pages = ceil( (int) $tot_items / $per_page );

						if ( $current_page > $pages ) {
							$current_page = $pages;
						}

						$offset = ( $current_page - 1 ) * $per_page;
						if ( $pages > 1 ) {
							$page_links = paginate_links(
								array(
									'base'     => $base_url,
									'format'   => $format,
									'current'  => $current_page,
									'total'    => $pages,
									'show_all' => true,
								)
							);
						}

						$query_args['number'] = $per_page;
						$query_args['offset'] = $offset;
					}

					$items             = YITH_Payout_Items()->get_payout_items( $query_args );
					$additional_params = array(
						'count'          => $tot_items,
						'current_page'   => $current_page,
						'max_num_pages'  => $pages,
						'user_log_items' => $items,
						'page_links'     => isset( $page_links ) ? $page_links : false,
					);

					$atts = array_merge( $atts, $additional_params );

					$atts['atts'] = $atts;

					ob_start();
					wc_get_template( 'view-payouts-list.php', $atts, '', YITH_PAYOUTS_TEMPLATE_PATH );
					$template = ob_get_contents();
					ob_end_clean();

					return $template;

				} else {
					$additional_params = array(
						'count'          => 0,
						'current_page'   => 1,
						'max_num_pages'  => 0,
						'user_log_items' => array(),

					);

					$atts         = array_merge( $atts, $additional_params );
					$atts['atts'] = apply_filters( 'yith_payouts_view_payouts_list_shortcode_args', $atts );

					ob_start();
					wc_get_template( 'view-payouts-list.php', $atts, '', YITH_PAYOUTS_TEMPLATE_PATH );
					$template = ob_get_contents();
					ob_end_clean();

					return $template;
				}
			}
		}

		/**
		 * Get the paypal email
		 *
		 * @param int $user_id The user id.
		 *
		 * @return string
		 * @since 1.0.0
		 */
		public function get_receiver_email( $user_id ) {

			$receivers_opt = get_option( 'yith_payouts_receiver_list', array() );

			if ( $receivers_opt ) {
				foreach ( $receivers_opt as $receiver ) {

					if ( intval( $user_id ) === intval( $receiver['user_id'] ) ) {
						return $receiver['paypal_email'];
					}
				}
			}

			return '';
		}

	}
}

if ( ! function_exists( 'YITH_Payouts_Shortcodes' ) ) {
	/**
	 * Return the instance of the class
	 *
	 * @return YITH_Payouts_Shortcodes
	 */
	function YITH_Payouts_Shortcodes() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName
		return new YITH_Payouts_Shortcodes();
	}
}

YITH_Payouts_Shortcodes();
