<?php

/**
 * Sibs Plugin Additional.
 *
 * This file is used for add payment status, extra customer info.
 * Copyright (c) SIBS
 *
 * @located at  /
 */
require_once __DIR__.'/vendor/autoload.php';

use Sibs\Stargate\Constants\WoocommerceOrderStatus;
use Sibs\Stargate\Mails\SibsPaymentProcessedEmail;

defined('ABSPATH') || exit;

add_action('wp_print_scripts', 'sibs_add_custom_order_status_icon');
add_action('woocommerce_checkout_process', 'verify_billing_shipping_address');

add_filter('init', 'sibs_register_payment_status');
add_filter('wc_order_statuses', 'sibs_add_order_status');
add_filter('woocommerce_email_classes', 'payment_processed_woocommerce_email');

/**
 * Register new order statuses
 *  (Payment Authorized).
 */
function sibs_register_payment_status()
{
    register_post_status(WoocommerceOrderStatus::AUTHORIZED, [
        'label' => _x('Payment Authorized', 'WooCommerce Order Status', 'sibs-woo-stg'),
        'label_count'
        // translators: payment status count
        => _nx_noop(
            'Payment Authorized <span class="count">(%s)</span>',
            'Payment Authorized <span class="count">(%s)</span>',
            'Order status',
            'sibs-woo-stg'
        ),
        'exclude_from_search' => false,
        'public' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
    ]);

    register_post_status(WoocommerceOrderStatus::PARTIALLY_CAPTURED, [
        'label' => _x('Payment Partially Captured', 'WooCommerce Order Status', 'sibs-woo-stg'),
        'label_count'
        // translators: payment status count
        => _nx_noop(
            'Payment Partially Captured <span class="count">(%s)</span>',
            'Payment Partially Captured <span class="count">(%s)</span>',
            'Order status',
            'sibs-woo-stg'
        ),
        'exclude_from_search' => false,
        'public' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
    ]);

    register_post_status(WoocommerceOrderStatus::PARTIALLY_REFUNDED, [
        'label' => _x('Payment Partially Refunded', 'WooCommerce Order Status', 'sibs-woo-stg'),
        'label_count'
        // translators: payment status count
        => _nx_noop(
            'Payment Partially Refunded <span class="count">(%s)</span>',
            'Payment Partially Refunded <span class="count">(%s)</span>',
            'Order status',
            'sibs-woo-stg'
        ),
        'exclude_from_search' => false,
        'public' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
    ]);

    register_post_status(WoocommerceOrderStatus::PENDING_REFUND, [
        'label' => _x('Payment Pending Refund', 'WooCommerce Order Status', 'sibs-woo-stg'),
        'label_count'
        // translators: payment status count
        => _nx_noop(
            '<span class="count">Payment Pending Refund (%s)</span>',
            '<span class="count">Payment Pending Refund (%s)</span>',
            'Order status',
            'sibs-woo-stg'
        ),
        'exclude_from_search' => false,
        'public' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
    ]);
}

/**
 * Add new order statuses to woocommerce
 *  ( Payment Authorized ).
 *
 * @param array $order_status order status
 */
function sibs_add_order_status($order_status)
{
    $order_status[WoocommerceOrderStatus::AUTHORIZED] = _x('Payment Authorized', 'Payment Authorized Order Status', 'sibs-woo-stg');
    $order_status[WoocommerceOrderStatus::PARTIALLY_CAPTURED] = _x('Payment Partially Captured', 'Payment Partially Captured Order Status', 'sibs-woo-stg');
    $order_status[WoocommerceOrderStatus::PARTIALLY_REFUNDED] = _x('Payment Partially Refunded', 'Payment Partially Refunded Order Status', 'sibs-woo-stg');
    $order_status[WoocommerceOrderStatus::PENDING_REFUND] = _x('Payment Pending Refund', 'Payment Pending Refund Order Status', 'sibs-woo-stg');

    return $order_status;
}

/**
 * Add sibs custom order status icon.
 */
function sibs_add_custom_order_status_icon()
{
    if (!is_admin()) {
        return;
    }
    ?>
	<style>
		.column-order_status mark.status-authorized {
			color: #94660c;
			background-color: #f8dda7;
		}

		.column-order_status mark.status-part-captured {
			color: #5b841b;
			background-color: #c6e1c6;
		}

		.column-order_status mark.status-part-refunded {
			color: #777777;
			background-color: #e5e5e5;
		}
	</style>
<?php
}

/**
 *  Add a custom email to the list of emails WooCommerce should load.
 *
 * @since 0.1
 *
 * @param array $email_classes available email classes
 *
 * @return array filtered available email classes
 */
function payment_processed_woocommerce_email($email_classes)
{
    // add the email class to the list of email classes that WooCommerce loads
    $email_classes['WCPaymentProcessedEmail'] = new SibsPaymentProcessedEmail();

    return $email_classes;
}

function verify_billing_shipping_address()
{
    $char_billing_address_1 = !empty($_POST['billing_address_1']) ? strlen($_POST['billing_address_1']) : 0;
    $char_billing_address_2 = !empty($_POST['billing_address_2']) ? strlen($_POST['billing_address_2']) : 0;

    $total_billing = $char_billing_address_1 + $char_billing_address_2;

    if ($total_billing > 70) {
        wc_add_notice(__('<strong>Billing Street</strong> address exceeds 70 characters limit', 'sibs-woo-stg'), 'error');
    }

    $char_shipping_address_1 = !empty($_POST['shipping_address_1']) ? strlen($_POST['shipping_address_1']) : 0;
    $char_shipping_address_2 = !empty($_POST['shipping_address_2']) ? strlen($_POST['shipping_address_2']) : 0;

    $total_shipping = $char_shipping_address_1 + $char_shipping_address_2;

    if ($total_shipping > 70) {
        wc_add_notice(__('<strong>Shipping Street</strong> address exceeds 70 characters limit', 'sibs-woo-stg'), 'error');
    }
}
