<?php

namespace Devurai\AcceptbluePaymentPro\Traits;

use Devurai\AcceptbluePaymentPro\DTOs\ABChargeResponse;
use Devurai\AcceptbluePaymentPro\Settings\ACHSettings;
use Devurai\AcceptbluePaymentPro\Settings\CCSettings;
use Devurai\AcceptbluePaymentPro\Settings\GooglePaySettings;

trait PaymentFunctions
{
    public function is_capture_transaction($settings): bool {
        /**
         * @var $settings GooglePaySettings|CCSettings|ACHSettings;
         */
        if($settings->is_charge_transaction()){
            return true;
        }else{
            if(!$settings->is_charge_transaction() && $this->cart_contains_virtual_product()){
                return true;
            }else{
                return false;
            }
        }
    }

    public function cart_contains_virtual_product(): bool {
        if(function_exists('WC')){
            $cart = WC()->cart->get_cart();
            $contains = false;

            if(empty($cart) || is_array($cart) && count($cart) === 0){
                return false;
            }

            foreach ($cart as $item){
                $product = $item['data'];

                if($product->is_virtual('yes') || $product->is_downloadable('yes')){
                    $contains = true;
                    break;
                }
            }

            return $contains;
        }else{
            return false;
        }
    }

    public function reduce_order_stock($order_id, $order): void
    {
        version_compare(WC_VERSION, '3.0.0', '<') ?
            $order->reduce_order_stock() :
            wc_reduce_stock_levels($order_id);
    }

    public function get_payment_method_from_order($order){
        if( version_compare( WC_VERSION, '3.0.0', '<' )) {
            return $order->payment_method;
        }else{
            return $order->get_payment_method();
        }
    }

    public function is_rest_request(): bool
    {
        return defined('REST_REQUEST') && REST_REQUEST;
    }

    public function save_payment_method(string $gateway_id, $response, &$_token): void
    {
        /**
         * @var $response ABChargeResponse
         */
        if (is_user_logged_in()) {
            $token = new \WC_Payment_Token_CC();
            $token->set_token($response->getCardRef());
            $token->set_gateway_id($gateway_id);
            $token->set_last4($response->getTransaction()->getCardDetails()->getLast4());
            $token->set_expiry_year($response->getTransaction()->getCardDetails()->getExpiryYear());
            $token->set_expiry_month($response->getTransaction()->getCardDetails()->getExpiryMonth());
            $token->set_card_type($response->getTransaction()->getCardDetails()->getCardType());
            $token->set_user_id(get_current_user_id());
            $token->save();

            $_token = $token;

            \WC_Payment_Tokens::set_users_default(get_current_user_id(), $token->get_id());
        }
    }

    public function order_contains_subscription($order): bool
    {
        if(function_exists('wcs_order_contains_subscription')){
            return wcs_order_contains_subscription($order);
        }else{
            return false;
        }
    }
}