<?php
namespace BooklyPaypalPaymentsStandard\Frontend\Modules\Booking\ProxyProviders;

use Bookly\Lib as BooklyLib;
use Bookly\Lib\Entities\Payment;
use Bookly\Lib\UserBookingData;
use Bookly\Lib\Utils\Common;
use Bookly\Frontend\Modules\Booking\Proxy;

/**
 * Class Local
 * @package BooklyPaypalPaymentsStandard\Frontend\Modules\Booking\ProxyProviders
 */
class Local extends Proxy\PaypalPaymentsStandard
{
    /**
     * @inheritdoc
     */
    public static function renderPaymentForm( $form_id, $page_url )
    {
        $userData = new UserBookingData( $form_id );
        $userData->load();

        $cart_info = $userData->cart->getInfo( Payment::TYPE_PAYPAL );
        $cart_info->setGatewayTaxCalculationRule( 'tax_increases_the_cost' );
        $first_name = $userData->getFirstName();
        $last_name  = $userData->getLastName();
        // Check if defined First name
        if ( ! $first_name ) {
            $full_name  = $userData->getFullName();
            $first_name = strtok( $full_name, ' ' );
            $last_name  = strtok( '' );
        }
        $replacement = array(
            '%action%'        => 'https://www' . ( get_option( 'bookly_paypal_sandbox' ) ? '.sandbox' : '' ) . '.paypal.com/cgi-bin/webscr',
            '%business%'      => get_option( 'bookly_paypal_id' ),
            '%amount%'        => $cart_info->getGatewayAmount(),
            '%tax%'           => $cart_info->getGatewayTax(),
            '%currency%'      => get_option( 'bookly_pmt_currency' ),
            '%item_name%'     => esc_attr( $userData->cart->getItemsTitle( 127 ) ),
            '%notify_url%'    => esc_attr( add_query_arg( array( 'bookly_action' => 'paypal-ps-ipn' ), $page_url ) ),
            '%return%'        => esc_attr( add_query_arg( array( 'bookly_action' => 'paypal-ps-return', 'bookly_fid' => $form_id ), $page_url ) ),
            '%cancel_return%' => esc_attr( add_query_arg( array( 'bookly_action' => 'paypal-ps-cancel', 'bookly_fid' => $form_id ), $page_url ) ),
            '%email%'         => $userData->getEmail(),
            '%first_name%'    => esc_attr( $first_name ),
            '%last_name%'     => esc_attr( $last_name ),
            '%back%'          => Common::getTranslatedOption( 'bookly_l10n_button_back' ),
            '%next%'          => Common::getTranslatedOption( 'bookly_l10n_step_payment_button_next' ),
            '%gateway%'       => Payment::TYPE_PAYPAL,
            '%align_class%'   => get_option( 'bookly_app_align_buttons_left' ) ? 'bookly-left' : 'bookly-right',
        );
        $form = '<form action="%action%" method="post" data-gateway="%gateway%">
            <input type="hidden" name="cmd" value="_xclick" />
            <input type="hidden" name="business" value="%business%" />
            <input type="hidden" name="amount" value="%amount%" class="bookly-js-payment-amount" />
            <input type="hidden" name="tax" value="%tax%" class="bookly-js-payment-tax" />
            <input type="hidden" name="custom" value="" class="bookly-payment-id" />
            <input type="hidden" name="currency_code" value="%currency%" />
            <input type="hidden" name="item_name" value="%item_name%" />
            <input type="hidden" name="notify_url" value="%notify_url%" />
            <input type="hidden" name="return" value="%return%" />
            <input type="hidden" name="cancel_return" value="%cancel_return%" />
            <input type="hidden" name="email" value="%email%" />
            <input type="hidden" name="first_name" value="%first_name%" />
            <input type="hidden" name="last_name" value="%last_name%" />
            <input type="hidden" name="charset" value="utf-8" />
            <button class="bookly-back-step bookly-js-back-step bookly-btn ladda-button" data-style="zoom-in" style="margin-right: 10px;" data-spinner-size="40"><span class="ladda-label">%back%</span></button>
            <div class="%align_class%">
                <button class="bookly-next-step bookly-js-next-step bookly-btn ladda-button" data-style="zoom-in" data-spinner-size="40"><span class="ladda-label">%next%</span></button>
            </div>
        </form>';

        echo strtr( $form, $replacement );
    }
}