<?php
namespace orderflow;

class OrderAmountModifier {
    /**
     * কনস্ট্রাক্টর
     */
    public function __construct() {
        add_filter('woocommerce_get_order_item_totals', array($this, 'modify_order_total_display'), 10, 3);


        add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'add_payment_details_column'));
        add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'display_payment_details_column'), 10, 2);

        add_action('admin_head', array($this, 'add_admin_styles'));


        add_action('woocommerce_admin_order_totals_after_total', array($this, 'add_partial_payment_details_admin'), 20, 1);

        add_action('woocommerce_email_after_order_table', array($this, 'add_partial_payment_to_email'), 10, 4);

        // মাই অ্যাকাউন্ট পেজে অর্ডার ভিউতে পার্শিয়াল পেমেন্ট ডিসপ্লে করার জন্য
        add_action('woocommerce_order_details_after_order_table', array($this, 'add_partial_payment_to_order_view'), 10, 1);

        // অর্ডার ট্র্যাকিং পেজে পার্শিয়াল পেমেন্ট ডিসপ্লে করার জন্য
        add_action('woocommerce_order_details_before_order_table', array($this, 'add_partial_payment_to_order_tracking'), 10, 1);
    }

    /**
     * অ্যাডমিন পেজের জন্য স্টাইল যোগ করে
     */
    public function add_admin_styles() {
        ?>
        <style>
            /*
            th#order_total {
                display: none;
            }

            td.order_total.column-order_total {
                display: none;
            }


            th.manage-column.column-order_total.sortable.desc {
                display: none;
            }*/
        </style>
        <?php
    }

    public function add_payment_details_column($columns) {
        // অর্ডার স্ট্যাটাসের পরে আমাদের কলাম যোগ করব
        $new_columns = array();

        foreach ($columns as $key => $value) {
            $new_columns[$key] = $value;

            // অর্ডার স্ট্যাটাস কলামের পরে আমাদের কলাম যোগ করব
            if ($key === 'order_status') {
                $new_columns['payment_details'] = __('Amount', 'your-text-domain');
            }
        }

        return $new_columns;
    }

    public function display_payment_details_column($column, $order_id) {
        if ($column !== 'payment_details') {
            return;
        }

        $order = wc_get_order($order_id);
        $has_partial = $order->get_meta('_has_partial_payment');

        if ($has_partial === 'yes') {
            $partial_amount = $order->get_meta('_partial_payment_amount');
            $remaining_amount = $order->get_meta('_remaining_payment_amount');
            $total_amount = $partial_amount + $remaining_amount;

            echo '<div class="payment-details-column">';
            echo '<div class="payment-row">';
            echo '<span class="payment-label">' . __('পেইড:', 'your-text-domain') . '</span>';
            echo '<span class="payment-amount paid">' . wc_price($partial_amount) . '</span>';
            echo '</div>';

            echo '<div class="payment-row">';
            echo '<span class="payment-label">' . __('বাকি:', 'your-text-domain') . '</span>';
            echo '<span class="payment-amount remaining">' . wc_price($remaining_amount) . '</span>';
            echo '</div>';

            echo '<div class="payment-row total">';
            echo '<span class="payment-label">' . __('Total:', 'your-text-domain') . '</span>';
            echo '<span class="payment-amount">' . wc_price($total_amount) . '</span>';
            echo '</div>';
            echo '</div>';
        } else {
            // সাধারণ অর্ডারের জন্য
            echo '<div class="payment-details-column">';
            echo '<div class="payment-row total">';
            echo '<span class="payment-label">' . __('Total:', 'your-text-domain') . '</span>';
            echo '<span class="payment-amount">' . $order->get_formatted_order_total() . '</span>';
            echo '</div>';
            echo '</div>';
        }
    }

    /**
     * অর্ডার Total ডিসপ্লে মডিফাই করে
     *
     * @param array    $total_rows  অর্ডার Total রোস
     * @param \WC_Order $order      অর্ডার অবজেক্ট
     * @param string   $tax_display ট্যাক্স ডিসপ্লে সেটিং
     * @return array মডিফাইড Total রোস
     */
    public function modify_order_total_display($total_rows, $order, $tax_display) {
        // শুধুমাত্র ধন্যবাদ পেজে কাজ করবে
        if (!is_order_received_page()) {
            return $total_rows;
        }

        // পার্শিয়াল পেমেন্ট আছে কিনা চেক করুন
        $has_partial = $order->get_meta('_has_partial_payment');
        if ($has_partial !== 'yes') {
            return $total_rows;
        }

        // পার্শিয়াল অ্যামাউন্ট পান
        $partial_amount = $order->get_meta('_partial_payment_amount');

        // Total রো আপডেট করুন
        if (isset($total_rows['order_total'])) {
            $total_rows['order_total']['value'] = wc_price($partial_amount);

            // লেবেল পরিবর্তন করতে চাইলে (ঐচ্ছিক)
            $total_rows['order_total']['label'] = __('পেইড অ্যামাউন্ট', 'your-text-domain') . ':';
        }

        return $total_rows;
    }

    /**
     * অ্যাডমিন পেজে অর্ডার Total মডিফাই করে
     *
     * @param \WC_Order $order অর্ডার অবজেক্ট
     */
    public function modify_admin_order_total($order) {
        $has_partial = $order->get_meta('_has_partial_payment');
        if ($has_partial !== 'yes') {
            return;
        }

        $partial_amount = $order->get_meta('_partial_payment_amount');
        $remaining_amount = $order->get_meta('_remaining_payment_amount');

        // অ্যাডমিন পেজে Total মডিফাই করার লজিক
        echo '<div class="partial-payment-notice">';
        echo '<p><strong>পেইড অ্যামাউন্ট:</strong> ' . wc_price($partial_amount) . '</p>';
        echo '<p><strong>রিমেইনিং অ্যামাউন্ট:</strong> ' . wc_price($remaining_amount) . '</p>';
        echo '</div>';
    }

    /**
     * ইমেলে অর্ডার Total মডিফাই করে
     *
     * @param \WC_Order $order অর্ডার অবজেক্ট
     * @param bool $sent_to_admin অ্যাডমিনে পাঠানো হচ্ছে কিনা
     * @param bool $plain_text প্লেইন টেক্সট ইমেল কিনা
     */
    public function modify_email_order_total($order, $sent_to_admin, $plain_text) {
        $has_partial = $order->get_meta('_has_partial_payment');
        if ($has_partial !== 'yes') {
            return;
        }

        $partial_amount = $order->get_meta('_partial_payment_amount');
        $remaining_amount = $order->get_meta('_remaining_payment_amount');

        // ইমেলে Total মডিফাই করার লজিক
        if ($plain_text) {
            echo "\n" . 'পেইড অ্যামাউন্ট: ' . wc_price($partial_amount) . "\n";
            echo 'রিমেইনিং অ্যামাউন্ট: ' . wc_price($remaining_amount) . "\n";
        } else {
            echo '<h2>' . __('পেমেন্ট ডিটেইলস', 'your-text-domain') . '</h2>';
            echo '<table>';
            echo '<tr><td>' . __('পেইড অ্যামাউন্ট:', 'your-text-domain') . '</td><td>' . wc_price($partial_amount) . '</td></tr>';
            echo '<tr><td>' . __('রিমেইনিং অ্যামাউন্ট:', 'your-text-domain') . '</td><td>' . wc_price($remaining_amount) . '</td></tr>';
            echo '</table>';
        }
    }

    public function add_partial_payment_details_admin($order_id) {
        $order = wc_get_order($order_id);
        $has_partial = $order->get_meta('_has_partial_payment');

        if ($has_partial !== 'yes') {
            return;
        }

        $partial_amount = $order->get_meta('_partial_payment_amount');
        $remaining_amount = $order->get_meta('_remaining_payment_amount');
        $total_amount = $partial_amount + $remaining_amount;

        // পেমেন্ট মেথড এবং তারিখ পাওয়া
        $payment_method_title = $order->get_payment_method_title();
        $date_paid = $order->get_date_paid();
        $payment_date = $date_paid ? $date_paid->date_i18n(get_option('date_format')) : '';

        ?>
        <div class="partial-payment-details">
            <table class="wc-order-totals" style="display: block; border-top: 1px solid #999; margin-top:12px; padding-top:12px">
                <tbody>
                <tr>
                    <td class="label label-highlight">Paid:</td>
                    <td width="1%"></td>
                    <td class="total">
                        <?php echo wc_price($partial_amount); ?>
                    </td>
                </tr>
                <tr>
                    <td class="label label-highlight">Remaining:</td>
                    <td width="1%"></td>
                    <td class="total">
                        <?php echo wc_price($remaining_amount); ?>
                    </td>
                </tr>
                <tr>
                    <td class="label label-highlight">Total:</td>
                    <td width="1%"></td>
                    <td class="total">
                        <?php echo wc_price($total_amount); ?>
                    </td>
                </tr>
                <tr>
                    <td>
                            <span class="description">
                                <?php
                                if (!empty($payment_date)) {
                                    echo esc_html($payment_date) . ' via ' . esc_html($payment_method_title);
                                } else {
                                    echo esc_html($payment_method_title);
                                }
                                ?>
                            </span>
                    </td>
                    <td colspan="2"></td>
                </tr>
                </tbody>
            </table>
            <div class="clear"></div>
        </div>

        <style>
            .partial-payment-details .label-highlight {
                font-weight: 600;
            }
            .partial-payment-details .total {
                font-weight: 600;
            }
        </style>
        <?php
    }


    public function add_partial_payment_to_email($order, $sent_to_admin, $plain_text, $email) {
        $has_partial = $order->get_meta('_has_partial_payment');
        if ($has_partial !== 'yes') {
            return;
        }

        $partial_amount = $order->get_meta('_partial_payment_amount');
        $remaining_amount = $order->get_meta('_remaining_payment_amount');
        $total_amount = $partial_amount + $remaining_amount;

        // পেমেন্ট মেথড এবং তারিখ পাওয়া
        $payment_method_title = $order->get_payment_method_title();
        $date_paid = $order->get_date_paid();
        $payment_date = $date_paid ? $date_paid->date_i18n(get_option('date_format')) : '';

        if ($plain_text) {
            echo "\n" . __('Payment Details', 'your-text-domain') . "\n";
            echo "--------------------------------\n";
            echo __('Paid Amount:', 'your-text-domain') . ' ' . wc_price($partial_amount) . "\n";
            echo __('Remaining Amount:', 'your-text-domain') . ' ' . wc_price($remaining_amount) . "\n";
            echo __('Total Amount:', 'your-text-domain') . ' ' . wc_price($total_amount) . "\n";
            if (!empty($payment_date)) {
                echo __('Payment Date:', 'your-text-domain') . ' ' . $payment_date . "\n";
                echo __('Payment Method:', 'your-text-domain') . ' ' . $payment_method_title . "\n";
            } else {
                echo __('Payment Method:', 'your-text-domain') . ' ' . $payment_method_title . "\n";
            }
            echo "\n";
        } else {
            ?>
                <style>
                    table.wc-order-totals {
                        display: none;
                    }
                </style>
            <h2><?php _e('Payment Details', 'your-text-domain'); ?></h2>
            <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
                <tr>
                    <th class="td" scope="row" style="text-align:left;"><?php _e('Paid Amount:', 'your-text-domain'); ?></th>
                    <td class="td" style="text-align:left;"><?php echo wc_price($partial_amount); ?></td>
                </tr>
                <tr>
                    <th class="td" scope="row" style="text-align:left;"><?php _e('Remaining Amount:', 'your-text-domain'); ?></th>
                    <td class="td" style="text-align:left;"><?php echo wc_price($remaining_amount); ?></td>
                </tr>
                <tr>
                    <th class="td" scope="row" style="text-align:left;"><?php _e('Total Amount:', 'your-text-domain'); ?></th>
                    <td class="td" style="text-align:left;"><?php echo wc_price($total_amount); ?></td>
                </tr>
                <tr>
                    <th class="td" scope="row" style="text-align:left;"><?php _e('Payment Method:', 'your-text-domain'); ?></th>
                    <td class="td" style="text-align:left;"><?php echo esc_html($payment_method_title); ?></td>
                </tr>
                <?php if (!empty($payment_date)) : ?>
                    <tr>
                        <th class="td" scope="row" style="text-align:left;"><?php _e('Payment Date:', 'your-text-domain'); ?></th>
                        <td class="td" style="text-align:left;"><?php echo esc_html($payment_date); ?></td>
                    </tr>
                <?php endif; ?>
            </table>
            <?php
        }
    }

    /**
     * মাই অ্যাকাউন্ট পেজে অর্ডার ভিউতে পার্শিয়াল পেমেন্ট ডিসপ্লে করে
     *
     * @param \WC_Order $order অর্ডার অবজেক্ট
     */
    public function add_partial_payment_to_order_view($order) {
        $has_partial = $order->get_meta('_has_partial_payment');
        if ($has_partial !== 'yes') {
            return;
        }

        $partial_amount = $order->get_meta('_partial_payment_amount');
        $remaining_amount = $order->get_meta('_remaining_payment_amount');
        $total_amount = $partial_amount + $remaining_amount;

        // পেমেন্ট মেথড এবং তারিখ পাওয়া
        $payment_method_title = $order->get_payment_method_title();
        $date_paid = $order->get_date_paid();
        $payment_date = $date_paid ? $date_paid->date_i18n(get_option('date_format')) : '';
        ?>
        <section class="woocommerce-order-details">
            <h2 class="woocommerce-order-details__title"><?php _e('Payment Details', 'your-text-domain'); ?></h2>
            <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                <tbody>
                <tr>
                    <th scope="row"><?php _e('Paid Amount:', 'your-text-domain'); ?></th>
                    <td><?php echo wc_price($partial_amount); ?></td>
                </tr>
                <tr>
                    <th scope="row"><?php _e('Remaining Amount:', 'your-text-domain'); ?></th>
                    <td><?php echo wc_price($remaining_amount); ?></td>
                </tr>
                <tr>
                    <th scope="row"><?php _e('Total Amount:', 'your-text-domain'); ?></th>
                    <td><?php echo wc_price($total_amount); ?></td>
                </tr>
                <tr>
                    <th scope="row"><?php _e('Payment Method:', 'your-text-domain'); ?></th>
                    <td><?php echo esc_html($payment_method_title); ?></td>
                </tr>
                <?php if (!empty($payment_date)) : ?>
                    <tr>
                        <th scope="row"><?php _e('Payment Date:', 'your-text-domain'); ?></th>
                        <td><?php echo esc_html($payment_date); ?></td>
                    </tr>
                <?php endif; ?>
                </tbody>
            </table>
        </section>
        <?php
    }

    /**
     * অর্ডার ট্র্যাকিং পেজে পার্শিয়াল পেমেন্ট ডিসপ্লে করে
     *
     * @param \WC_Order $order অর্ডার অবজেক্ট
     */
    public function add_partial_payment_to_order_tracking($order) {
        $has_partial = $order->get_meta('_has_partial_payment');
        if ($has_partial !== 'yes') {
            return;
        }

        $partial_amount = $order->get_meta('_partial_payment_amount');
        $remaining_amount = $order->get_meta('_remaining_payment_amount');
        $total_amount = $partial_amount + $remaining_amount;

        // পেমেন্ট মেথড এবং তারিখ পাওয়া
        $payment_method_title = $order->get_payment_method_title();
        $date_paid = $order->get_date_paid();
        $payment_date = $date_paid ? $date_paid->date_i18n(get_option('date_format')) : '';
        ?>
        <div class="woocommerce-order-payment-details">
            <h2><?php _e('Payment Details', 'your-text-domain'); ?></h2>
            <ul class="order_details">
                <li>
                    <?php _e('Paid Amount:', 'your-text-domain'); ?>
                    <strong><?php echo wc_price($partial_amount); ?></strong>
                </li>
                <li>
                    <?php _e('Remaining Amount:', 'your-text-domain'); ?>
                    <strong><?php echo wc_price($remaining_amount); ?></strong>
                </li>
                <li>
                    <?php _e('Total Amount:', 'your-text-domain'); ?>
                    <strong><?php echo wc_price($total_amount); ?></strong>
                </li>
                <li>
                    <?php _e('Payment Method:', 'your-text-domain'); ?>
                    <strong><?php echo esc_html($payment_method_title); ?></strong>
                </li>
                <?php if (!empty($payment_date)) : ?>
                    <li>
                        <?php _e('Payment Date:', 'your-text-domain'); ?>
                        <strong><?php echo esc_html($payment_date); ?></strong>
                    </li>
                <?php endif; ?>
            </ul>
        </div>
        <style>
            .woocommerce-order-payment-details {
                margin-bottom: 2em;
            }
            .woocommerce-order-payment-details ul.order_details {
                margin-right: 0;
                margin-left: 0;
            }
        </style>
        <?php
    }
}