<?php
/**
 * Plugin Name:         Deshi Pay
 * Plugin URI:          https://wordpress.org/plugins/deshi-pay
 * Description:         Modern bKash, Nagad & Rocket manual gateway with individual enable/disable and settings link.
 * Version:             1.0.0
 * Author:              Rakib Hussain
 * Author URI:          https://rakibhussain.com
 * License:             GPL v2 or later
 * Text Domain:         deshi-pay
 */

if ( ! defined( 'ABSPATH' ) ) exit;

// ১. সেটিংস লিঙ্ক ফাংশন ইউনিক করা হয়েছে (DSHPY_ prefix)
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'dshpy_settings_link' );
function dshpy_settings_link( $links ) {
    $settings_link = '<a href="admin.php?page=wc-settings&tab=checkout&section=dshpy_bd_gateway">' . esc_html__( 'Settings', 'deshi-pay' ) . '</a>';
    array_unshift( $links, $settings_link );
    return $links;
}

add_action( 'plugins_loaded', 'dshpy_init_advanced_gateway' );

function dshpy_init_advanced_gateway() {
    if ( ! class_exists( 'WC_Payment_Gateway' ) ) return;

    // ২. ক্লাসের নাম ইউনিক করা হয়েছে (DSHPY_Advanced_Gateway)
    class DSHPY_Advanced_Gateway extends WC_Payment_Gateway {

        public function __construct() {
            $this->id = 'dshpy_bd_gateway'; 
            $this->has_fields = true;
            $this->method_title = 'BD Wallet (Bkash/Nagad/Rocket)';
            $this->method_description = 'Manual Mobile Banking Payment Gateway for Bangladesh.';
            
            $this->init_form_fields();
            $this->init_settings();

            $this->title = $this->get_option( 'title' );
            $this->description = $this->get_option( 'description' );

            add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
        }

        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title'   => 'Enable/Disable',
                    'type'    => 'checkbox',
                    'label'   => 'Enable Deshi Pay',
                    'default' => 'yes'
                ),
                'title' => array(
                    'title'       => 'Title',
                    'type'        => 'text',
                    'description' => 'চেকআউট পেজে ইউজার যে নাম দেখবে।',
                    'default'     => 'Mobile Banking',
                    'desc_tip'    => true,
                ),
                'bkash_section' => array('title' => 'bKash Settings', 'type' => 'title'),
                'bkash_enabled' => array('title' => 'Enable bKash', 'type' => 'checkbox', 'default' => 'yes'),
                'bkash_no'      => array('title' => 'bKash Number', 'type' => 'text'),
                'nagad_section' => array('title' => 'Nagad Settings', 'type' => 'title'),
                'nagad_enabled' => array('title' => 'Enable Nagad', 'type' => 'checkbox', 'default' => 'yes'),
                'nagad_no'      => array('title' => 'Nagad Number', 'type' => 'text'),
                'rocket_section'=> array('title' => 'Rocket Settings', 'type' => 'title'),
                'rocket_enabled'=> array('title' => 'Enable Rocket', 'type' => 'checkbox', 'default' => 'yes'),
                'rocket_no'     => array('title' => 'Rocket Number', 'type' => 'text'),
            );
        }

        public function payment_scripts() {
            if ( ! is_checkout() ) return;
            wp_enqueue_style( 'dshpy-gateway-css', plugin_dir_url( __FILE__ ) . 'assets/css/style.css', array(), time() );
            wp_enqueue_script( 'dshpy-gateway-js', plugin_dir_url( __FILE__ ) . 'assets/js/script.js', array('jquery'), time(), true );
        }

        public function payment_fields() {
            $bkash_on  = $this->get_option('bkash_enabled') === 'yes';
            $nagad_on  = $this->get_option('nagad_enabled') === 'yes';
            $rocket_on = $this->get_option('rocket_enabled') === 'yes';

            $bkash_no  = $this->get_option('bkash_no');
            $nagad_no  = $this->get_option('nagad_no');
            $rocket_no = $this->get_option('rocket_no');
            
            $total = WC()->cart->get_total();
            
            $bkash_logo  = plugin_dir_url( __FILE__ ) . 'includes/images/bkash.png';
            $nagad_logo  = plugin_dir_url( __FILE__ ) . 'includes/images/nagad.png';
            $rocket_logo = plugin_dir_url( __FILE__ ) . 'includes/images/rocket.png';

            $default_num = '';
            $default_name = '';
            if($bkash_on) { $default_num = $bkash_no; $default_name = 'Bkash'; }
            elseif($nagad_on) { $default_num = $nagad_no; $default_name = 'Nagad'; }
            elseif($rocket_on) { $default_num = $rocket_no; $default_name = 'Rocket'; }
            ?>
            <div class="payment-gateway-card">
                <div class="amount-section">
                    <p>আপনাকে পেমেন্ট করতে হবে</p>
                    <h2 class="total-amount"><?php echo wp_kses_post($total); ?></h2>
                    <span class="secure-tag">🛡️ ১০০% সুরক্ষিত পেমেন্ট</span>
                </div>

                <div class="method-selector">
                    <p class="label">পেমেন্ট মেথড বেছে নিন</p>
                    <div class="method-grid">
                        <?php if($bkash_on && $bkash_no): ?>
                        <div class="method-box active" data-num="<?php echo esc_attr($bkash_no); ?>" data-name="Bkash" data-img="<?php echo esc_url($bkash_logo); ?>">
                            <img src="<?php echo esc_url($bkash_logo); ?>" alt="Bkash">
                            <span>Bkash</span>
                        </div>
                        <?php endif; ?>

                        <?php if($nagad_on && $nagad_no): ?>
                        <div class="method-box" data-num="<?php echo esc_attr($nagad_no); ?>" data-name="Nagad" data-img="<?php echo esc_url($nagad_logo); ?>">
                            <img src="<?php echo esc_url($nagad_logo); ?>" alt="Nagad">
                            <span>Nagad</span>
                        </div>
                        <?php endif; ?>

                        <?php if($rocket_on && $rocket_no): ?>
                        <div class="method-box" data-num="<?php echo esc_attr($rocket_no); ?>" data-name="Rocket" data-img="<?php echo esc_url($rocket_logo); ?>">
                            <img src="<?php echo esc_url($rocket_logo); ?>" alt="Rocket">
                            <span>Rocket</span>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="info-box">
                    <div style="display: flex; align-items: center; gap: 10px; margin-bottom: 12px;">
                         <img id="m-logo" src="<?php echo esc_url($bkash_logo); ?>" style="width:28px; height:28px; border-radius:6px; object-fit: contain;"> 
                         <span style="font-weight:bold; font-size: 15px;">
                            <span id="m-name" style="color:#2563eb;"><?php echo esc_html($default_name); ?></span> (Personal)
                         </span>
                    </div>
                    
                    <div class="num-copy-row" style="display: flex; align-items: center; justify-content: space-between; background: #fff; padding: 10px; border-radius: 8px; border: 1px solid #ddd;">
                        <span id="target-number" style="letter-spacing: 0.5px; font-weight: bold; font-size: 18px;"><?php echo esc_html($default_num); ?></span>
                        <button type="button" class="btn-copy" onclick="copyText()" style="background: #f0f7ff; border: none; padding: 8px; border-radius: 6px; cursor: pointer; display: flex; align-items: center; justify-content: center;">
                            <svg viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="20" height="20" style="display: block;">
                                <rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
                                <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
                            </svg>
                        </button>
                    </div>
                    
                    <div style="margin-top: 15px; display: flex; align-items: center; gap: 6px;">
                        <span class="info-icon" style="background: #2563eb; color: #fff; width: 16px; height: 16px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 10px;">i</span>
                        <p style="font-size: 12px; color: #666; margin: 0;">উল্লেখিত নাম্বারে  Send Money করে টাকা পাঠিয়ে নিচে Trx ID দিন।</p>
                    </div>
                </div>

                <div class="trx-field">
                    <label>Transaction ID</label>
                    <?php wp_nonce_field( 'dshpy_payment_action', 'dshpy_nonce' ); ?>
                    <input type="text" name="dshpy_trx_id" id="dshpy_trx_id" class="input-trx" placeholder="EX: 8N779STK" required>
                </div>
            </div>
            <?php
        }

        public function process_payment( $order_id ) {
            if ( ! isset( $_POST['dshpy_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['dshpy_nonce'] ), 'dshpy_payment_action' ) ) {
                return;
            }

            $order = wc_get_order( $order_id );
            
            if ( isset( $_POST['dshpy_trx_id'] ) ) {
                $trx_id = sanitize_text_field( wp_unslash( $_POST['dshpy_trx_id'] ) );
                update_post_meta( $order_id, '_transaction_id', $trx_id );
                $order->add_order_note( 'Transaction ID: ' . $trx_id );
            }
            $order->update_status( 'on-hold', 'Payment verification pending.' );
            WC()->cart->empty_cart();
            return array( 'result' => 'success', 'redirect' => $this->get_return_url( $order ) );
        }
    }
}

add_filter( 'woocommerce_payment_gateways', 'dshpy_add_advanced_gateway' );
function dshpy_add_advanced_gateway( $methods ) {
    $methods[] = 'DSHPY_Advanced_Gateway';
    return $methods;
}
