<?php
/*####################################################################
 # Copyright ©2020 UseePay Ltd. All Rights Reserved.                 #
 # This file may not be redistributed in whole or significant part.  #
 # This file is part of the UseePay package and should not be used   #
 # and distributed for any other purpose that is not approved by     #
 # UseePay Ltd.                                                      #
 # https://www.useepay.com                                           #
 ####################################################################*/

/**
 * Plugin Name: WooCommerce Useepay Gateway
 * Plugin URI: https://www.useepay.com
 * Description: Take credit card payments on your store using Useepay.
 * Version: 1.0.4
 * Author: UseePay
 * Author URI: https://www.useepay.com
 */

if (!defined('ABSPATH')) {
    exit;
} // Exit if accessed directly

define('WC_USEEPAY_INNER_VERSION', '1.0.4');
define( 'WC_USEEPAY_INNER_MAIN_FILE', __FILE__ );


class WC_UseePay_Inner
{
    private static $instance = null;

    static function init()
    {
        if (is_null(self::$instance)) {
            self::$instance = new WC_UseePay_Inner();
        }
        return self::$instance;
    }

    private function __construct()
    {
        add_action('plugins_loaded', array($this, 'woocommerce_gateway_useepay_init'));
    }

    function woocommerce_gateway_useepay_init()
    {
        if (!class_exists('WooCommerce')) {
            // woocommerce not installed
            add_action('admin_notices', array($this, 'woocommerce_not_installed_notice'));
            return;
        }

        require_once dirname(__FILE__) . '/includes/class-wc-useepay-inner-exception.php';
        require_once dirname(__FILE__) . '/includes/class-wc-useepay-inner-util.php';
        require_once dirname(__FILE__) . '/includes/class-wc-useepay-inner-logger.php';
        require_once dirname(__FILE__) . '/includes/class-wc-useepay-inner-api.php';
        require_once dirname(__FILE__) . '/includes/class-wc-useepay-inner-helper.php';
        require_once dirname(__FILE__) . '/includes/abstract-wc-useepay-inner-payment-gateway.php';
        require_once dirname(__FILE__) . '/includes/class-wc-gateway-useepay-creditcard.php';

        add_filter('woocommerce_payment_gateways', array($this, 'add_gateways'));
        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'wc_useepay_plugin_links'));
        add_filter('woocommerce_get_order_item_totals', array($this, 'wc_useepay_display_order_meta_for_customer'), 10, 2);
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles'));
    }

    function woocommerce_not_installed_notice()
    {
        echo '<p>WooCommerce is not installed or activated. Please install and activate <a href="https://woocommerce.com/" target="_blank">WooCommerce</a> before using UseePay.</p>';
    }

    /**
     * Add the gateway to WooCommerce
     *
     */
    function add_gateways($methods)
    {
        $methods[] = 'WC_Gateway_UseePay_CreditCard';
        return $methods;
    }

    /**
     * Generate a href tag(named Settings) in plugins installed page for UseePay.
     *
     * @param $links
     * @return array
     */
    function wc_useepay_plugin_links($links)
    {
        return array_merge(
            array(
                '<a href="admin.php?page=wc-settings&tab=checkout&section=useepay_creditcard">设置</a>',
            ),
            $links
        );
    }

    /**
     * Display useepay Trade No. for customer
     *
     *
     * The function is put here because the useepay class
     * is not called on order-received page
     *
     * @param array $total_rows
     * @param mixed $order
     * @return array
     */
    function wc_useepay_display_order_meta_for_customer($total_rows, $order)
    {
        $trade_no = get_post_meta($order->get_id(), 'useepay Trade No.', true);

        if (!empty($trade_no)) {
            $new_row['useepay_trade_no'] = array(
                'label' => 'UseePay Transaction ID:',
                'value' => $trade_no
            );
            // Insert $new_row after shipping field
            $total_rows = array_merge(array_splice($total_rows, 0, 2), $new_row, $total_rows);
        }
        return $total_rows;
    }

    /**
     * Enqueue admin styles for UseePay settings
     */
    function enqueue_admin_styles($hook) {
        // Only load on UseePay settings page
        if (strpos($hook, 'wc-settings') !== false && isset($_GET['section']) && $_GET['section'] === 'useepay_creditcard') {
            wp_enqueue_style(
                'useepay-admin-styles', 
                plugin_dir_url(__FILE__) . 'assets/css/useepay-styles.css', 
                array(), 
                '1.0.4'
            );
        }
        
        // Also load on WooCommerce settings page in case of other UseePay sections
        if (strpos($hook, 'wc-settings') !== false) {
            wp_enqueue_style(
                'useepay-admin-styles', 
                plugin_dir_url(__FILE__) . 'assets/css/useepay-styles.css', 
                array(), 
                '1.0.4'
            );
        }
    }

}

WC_UseePay_Inner::init();
