<?php

namespace CF7PA_Pay_Addons\Shared;

class Utils {

	public static function format_stripe_amount( $amount ) {
		return number_format($amount / 100, 2, '.', '');
	}

	public static function format_stripe_date( $seconds ) {
    // Convert seconds to GMT datetime string
    $dt_gmt = gmdate('Y-m-d H:i:s', $seconds);

    // Convert from GMT to local time zone based on WP settings
    $dt = get_date_from_gmt($dt_gmt, 'Y-m-d H:i:s');

    // Format according to WordPress date settings
    return date_i18n(get_option('date_format'), strtotime($dt));
	}

	public static function format_stripe_time( $seconds ) {
    // Convert seconds to GMT datetime string
    $dt_gmt = gmdate('Y-m-d H:i:s', $seconds);
    
    // Convert from GMT to local time zone based on WP settings
    $dt = get_date_from_gmt($dt_gmt, 'Y-m-d H:i:s');
    
    // Format according to WordPress date and time settings
    $format = get_option('date_format') . ' ' . get_option('time_format');
    return date_i18n($format, strtotime($dt));
	}

	public static function format_amount_with_symbol( $amount, $currency ) {
		if ( class_exists( 'NumberFormatter' ) ) {
			$amount_in_dollars = $amount / 100;
			$formatter = new \NumberFormatter( get_locale(), \NumberFormatter::CURRENCY );
			return $formatter->formatCurrency( $amount_in_dollars, $currency );
		}
		$symbol = self::get_currency_symbol( $currency );
		return $symbol . self::format_stripe_amount($amount);
	}

	/**
	 * Checks Stripe minimum order value authorized per currency
	 */
	public static function get_minimum_amount( $currency ) {
		// Check order amount
		switch ( $currency ) {
			case 'USD':
			case 'CAD':
			case 'EUR':
			case 'CHF':
			case 'AUD':
			case 'SGD':
				$minimum_amount = 50;
				break;
			case 'GBP':
				$minimum_amount = 30;
				break;
			case 'DKK':
				$minimum_amount = 250;
				break;
			case 'NOK':
			case 'SEK':
				$minimum_amount = 300;
				break;
			case 'JPY':
				$minimum_amount = 5000;
				break;
			case 'MXN':
				$minimum_amount = 1000;
				break;
			case 'HKD':
				$minimum_amount = 400;
				break;
			default:
				$minimum_amount = 50;
				break;
		}

		return $minimum_amount;
	}

	public static function get_formatted_address( $args = array(), $separator = '<br/>' ) {
		$address_string = $args['line1'] . $separator;

		if ( ! empty( $args['line2'] ) ) {
			$address_string .= $args['line2'] . $separator;
		}

		$address_string .= $args['city'] . ', ' . $args['state'] . ' ' . $args['postal_code'] . ' ';
		$address_string .= $args['country'];

		// We're done!
		return $address_string;
	}

	public static function get_currencies() {
		$currencies = array(
			'USD' => array( __( 'US Dollars (USD)', 'accept-stripe-for-contact-form-7' ), '$' ),
			'EUR' => array( __( 'Euros (EUR)', 'accept-stripe-for-contact-form-7' ), '€' ),
			'GBP' => array( __( 'Pounds Sterling (GBP)', 'accept-stripe-for-contact-form-7' ), '£' ),
			'AUD' => array( __( 'Australian Dollars (AUD)', 'accept-stripe-for-contact-form-7' ), 'AU$' ),
			'AED' => array( __( 'United Arab Emirates Dirham (AED)', 'accept-stripe-for-contact-form-7' ), 'د.إ' ),
			'ARS' => array( __( 'Argentine Peso (ARS)', 'accept-stripe-for-contact-form-7' ), 'ARS' ),
			'BAM' => array( __( 'Bosnia and Herzegovina Convertible Mark (BAM)', 'accept-stripe-for-contact-form-7' ), 'KM' ),
			'BGN' => array( __( 'Bulgarian Lev (BGN)', 'accept-stripe-for-contact-form-7' ), 'Лв.' ),
			'BRL' => array( __( 'Brazilian Real (BRL)', 'accept-stripe-for-contact-form-7' ), 'R$' ),
			'CAD' => array( __( 'Canadian Dollars (CAD)', 'accept-stripe-for-contact-form-7' ), 'CA$' ),
			'CLP' => array( __( 'Chilean Peso (CLP)', 'accept-stripe-for-contact-form-7' ), 'CLP' ),
			'CNY' => array( __( 'Chinese Yuan (CNY)', 'accept-stripe-for-contact-form-7' ), 'CN￥' ),
			'COP' => array( __( 'Colombian Peso (COP)', 'accept-stripe-for-contact-form-7' ), 'COL$' ),
			'CZK' => array( __( 'Czech Koruna (CZK)', 'accept-stripe-for-contact-form-7' ), 'Kč' ),
			'DKK' => array( __( 'Danish Krone (DKK)', 'accept-stripe-for-contact-form-7' ), 'kr' ),
			'DOP' => array( __( 'Dominican Peso (DOP)', 'accept-stripe-for-contact-form-7' ), 'RD$' ),
			'EGP' => array( __( 'Egyptian Pound (EGP)', 'accept-stripe-for-contact-form-7' ), 'E£' ),
			'HKD' => array( __( 'Hong Kong Dollar (HKD)', 'accept-stripe-for-contact-form-7' ), 'HK$' ),
			'HUF' => array( __( 'Hungarian Forint (HUF)', 'accept-stripe-for-contact-form-7' ), 'Ft' ),
			'INR' => array( __( 'Indian Rupee (INR)', 'accept-stripe-for-contact-form-7' ), '₹' ),
			'IDR' => array( __( 'Indonesia Rupiah (IDR)', 'accept-stripe-for-contact-form-7' ), 'Rp' ),
			'ILS' => array( __( 'Israeli Shekel (ILS)', 'accept-stripe-for-contact-form-7' ), '₪' ),
			'JPY' => array( __( 'Japanese Yen (JPY)', 'accept-stripe-for-contact-form-7' ), '¥' ),
			'LBP' => array( __( 'Lebanese Pound (LBP)', 'accept-stripe-for-contact-form-7' ), 'ل.ل' ),
			'MYR' => array( __( 'Malaysian Ringgits (MYR)', 'accept-stripe-for-contact-form-7' ), 'RM' ),
			'MXN' => array( __( 'Mexican Peso (MXN)', 'accept-stripe-for-contact-form-7' ), 'MX$' ),
			'NZD' => array( __( 'New Zealand Dollar (NZD)', 'accept-stripe-for-contact-form-7' ), 'NZ$' ),
			'NOK' => array( __( 'Norwegian Krone (NOK)', 'accept-stripe-for-contact-form-7' ), 'kr' ),
			'PEN' => array( __( 'Peruvian Nuevo Sol (PEN)', 'accept-stripe-for-contact-form-7' ), 'S/' ),
			'PHP' => array( __( 'Philippine Pesos (PHP)', 'accept-stripe-for-contact-form-7' ), '₱' ),
			'PLN' => array( __( 'Polish Zloty (PLN)', 'accept-stripe-for-contact-form-7' ), 'zł' ),
			'RON' => array( __( 'Romanian Leu (RON)', 'accept-stripe-for-contact-form-7' ), 'lei' ),
			'RUB' => array( __( 'Russian Ruble (RUB)', 'accept-stripe-for-contact-form-7' ), '₽' ),
			'SAR' => array( __( 'Saudi Riyal (SAR)', 'accept-stripe-for-contact-form-7' ), 'ر.س' ),
			'SGD' => array( __( 'Singapore Dollar (SGD)', 'accept-stripe-for-contact-form-7' ), 'SG$' ),
			'ZAR' => array( __( 'South African Rand (ZAR)', 'accept-stripe-for-contact-form-7' ), 'R' ),
			'KRW' => array( __( 'South Korean Won (KRW)', 'accept-stripe-for-contact-form-7' ), '₩' ),
			'SEK' => array( __( 'Swedish Krona (SEK)', 'accept-stripe-for-contact-form-7' ), 'kr' ),
			'CHF' => array( __( 'Swiss Franc (CHF)', 'accept-stripe-for-contact-form-7' ), 'CHF' ),
			'TWD' => array( __( 'Taiwan New Dollars (TWD)', 'accept-stripe-for-contact-form-7' ), 'NT$' ),
			'THB' => array( __( 'Thai Baht (THB)', 'accept-stripe-for-contact-form-7' ), '฿' ),
			'TRY' => array( __( 'Turkish Lira (TRY)', 'accept-stripe-for-contact-form-7' ), '₺' ),
			'UYU' => array( __( 'Uruguayan Peso (UYU)', 'accept-stripe-for-contact-form-7' ), '$U' ),
			'VND' => array( __( 'Vietnamese Dong (VND)', 'accept-stripe-for-contact-form-7' ), '₫' ),
		);

		return $currencies;
	}

	public static function get_currencies_options() {
		$currenciesOptions = array();
		$all_currencies    = self::get_currencies();
		foreach ( $all_currencies as $cur => $val ) {
			$currenciesOptions[ $cur ] = $val[1] . ' ' . $val[0];
		}
		return $currenciesOptions;
	}

	public static function get_all_payment_methods() {
		return [
			'automatic' => __('Automatic collect', 'accept-stripe-for-contact-form-7'),
			'card' => __('Card', 'accept-stripe-for-contact-form-7'),
			'us_bank_account'=> __('ACH Direct Debit', 'accept-stripe-for-contact-form-7'),
			'affirm' => __('Affirm', 'accept-stripe-for-contact-form-7'),
			'afterpay_clearpay' => __('Afterpay (Clearpay)', 'accept-stripe-for-contact-form-7'),
			'alipay' => __('Alipay', 'accept-stripe-for-contact-form-7'),
			'apple_pay' => __('Apple Pay', 'accept-stripe-for-contact-form-7'),
			'au_becs_debit' => __('BECS direct debit', 'accept-stripe-for-contact-form-7'),
			'bacs_debit' => __('Bacs Direct Debit', 'accept-stripe-for-contact-form-7'),
			'bancontact' => __('Bancontact', 'accept-stripe-for-contact-form-7'),
			'customer_balance' => __('Bank transfers ', 'accept-stripe-for-contact-form-7'),
			'eps' => __('EPS', 'accept-stripe-for-contact-form-7'),
			'fpx' => __('FPX', 'accept-stripe-for-contact-form-7'),
			'giropay' => __('giropay', 'accept-stripe-for-contact-form-7'),
			'google_pay' => __('Google Pay', 'accept-stripe-for-contact-form-7'),
			'grabpay' => __('GrabPay', 'accept-stripe-for-contact-form-7'),
			'ideal' => __('iDEAL', 'accept-stripe-for-contact-form-7'),
			'klarna' => __('Klarna', 'accept-stripe-for-contact-form-7'),
			'konbini' => __('Konbini', 'accept-stripe-for-contact-form-7'),
			'p24' => __('P24', 'accept-stripe-for-contact-form-7'),
			'paynow' => __('PayNow', 'accept-stripe-for-contact-form-7'),
			'paypal' => __('PayPal', 'accept-stripe-for-contact-form-7'),
			'sepa_debit' => __('SEPA Direct Debit', 'accept-stripe-for-contact-form-7'),
			'sofort' => __('SOFORT', 'accept-stripe-for-contact-form-7'),
			'wechat_pay' => __('WeChat Pay', 'accept-stripe-for-contact-form-7'),
		];
	}

	public static function get_supported_payment_methods( $currency ) {
		$default_methods = array(
			'automatic' => __( 'Automatic collect', 'accept-stripe-for-contact-form-7' ),
			'card'      => __( 'Card', 'accept-stripe-for-contact-form-7' ),
		);
		$china_methods   = array(
			'automatic'  => __( 'Automatic collect', 'accept-stripe-for-contact-form-7' ),
			'card'       => __( 'Card', 'accept-stripe-for-contact-form-7' ),
			'alipay'     => __( 'Alipay', 'accept-stripe-for-contact-form-7' ),
			'wechat_pay' => __( 'WeChat', 'accept-stripe-for-contact-form-7' ),
		);
		$usd_methods     = array(
			'automatic'         => __( 'Automatic collect', 'accept-stripe-for-contact-form-7' ),
			'card'              => __( 'Card', 'accept-stripe-for-contact-form-7' ),
			'affirm'            => __( 'Affirm', 'accept-stripe-for-contact-form-7' ),
			'afterpay_clearpay' => __( 'Afterpay (Clearpay)', 'accept-stripe-for-contact-form-7' ),
			'alipay'            => __( 'Alipay', 'accept-stripe-for-contact-form-7' ),
			'us_bank_account'   => __( 'ACH Direct Debit', 'accept-stripe-for-contact-form-7' ),
			'wechat_pay'        => __( 'WeChat', 'accept-stripe-for-contact-form-7' ),
		);
		$payment_methods = array(
			'USD' => $usd_methods,
			'CAD' => $usd_methods,
			'CNY' => $china_methods,
			'HKD' => $china_methods,
			'SGD' => $china_methods,
			'JPY' => $china_methods,
			'EUR' => array(
				'automatic'  => __( 'Automatic collect', 'accept-stripe-for-contact-form-7' ),
				'bancontact' => __( 'Bancontact', 'accept-stripe-for-contact-form-7' ),
				'card'       => __( 'Card', 'accept-stripe-for-contact-form-7' ),
				'eps'        => __( 'EPS', 'accept-stripe-for-contact-form-7' ),
				'ideal'      => __( 'iDEAL', 'accept-stripe-for-contact-form-7' ),
				'giropay'    => __( 'giropay', 'accept-stripe-for-contact-form-7' ),
				'sepa_debit' => __( 'SEPA Direct Debit', 'accept-stripe-for-contact-form-7' ),
				'sofort'     => __( 'SOFORT', 'accept-stripe-for-contact-form-7' ),
			),
			'MYR' => array(
				'fpx' => __( 'FPX', 'accept-stripe-for-contact-form-7' ),
			),
		);

		return $payment_methods[ $currency ] ?? $default_methods;
	}

	public static function get_currency_symbol( $currency ) {
		$currency       = strtoupper( $currency );
		$all_currencies = self::get_currencies();
		if ( array_key_exists( $currency, $all_currencies ) ) {
			return $all_currencies[ $currency ][1];
		}
		return '$';
	}

	public static function get_pages_options() {
		$pages   = get_pages(
			array(
				'sort_column'  => 'menu_order',
				'sort_order'   => 'ASC',
				'hierarchical' => 0,
			)
		);
		$options = array();
		foreach ( $pages as $page ) {
			$options[ get_page_link( $page->ID ) ] = ! empty( $page->post_title ) ? $page->post_title : '#' . $page->ID;
		}

		return $options;
	}

	public static function make_classname( $dirname ) {
		$dirname    = pathinfo( $dirname, PATHINFO_FILENAME );
		$class_name = explode( '-', $dirname );
		$class_name = array_map( 'ucfirst', $class_name );
		$class_name = implode( '_', $class_name );

		return $class_name;
	}

	public static function format_stripe_desc( $name, $desc ) {
		return $name . ( $desc ? ' - ' . $desc : '' );
	}
}
