<?php

namespace Devnet\PPH\Admin;

use Devnet\PPH\Includes\Helper;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class Edit_Product {
    public function __construct() {
        add_filter(
            'woocommerce_product_data_tabs',
            [$this, 'product_data_tab'],
            99,
            1
        );
        add_action( 'woocommerce_product_data_panels', [$this, 'product_data_panels'] );
        add_action(
            'woocommerce_product_after_variable_attributes',
            [$this, 'variation_panel'],
            10,
            3
        );
        add_action(
            'woocommerce_before_product_object_save',
            [$this, 'update_product'],
            10,
            2
        );
        add_action(
            'after_delete_post',
            [$this, 'remove_deleted_products_from_pph_table'],
            99,
            2
        );
        add_action( 'wp_ajax_update_pph_db_row', [$this, 'update_pph_db_row'] );
    }

    /**
     * Add custom tab to product tabs.
     *  
     * @since     1.0.0
     */
    public function product_data_tab( $tabs ) {
        $tabs['pph_price_history'] = [
            'label'    => esc_html__( 'Product Price History', 'product-price-history' ),
            'target'   => 'pph_price_history',
            'class'    => [],
            'priority' => 100,
        ];
        return $tabs;
    }

    /**
     * Add options to pph tab/panel.
     *  
     * @since     1.0.0
     */
    public function product_data_panels() {
        $id = get_the_ID();
        ?>

        <style>
            li.pph_price_history_tab a:before {
                content: "\f238" !important;
            }
        </style>

        <div id="pph_price_history" class="panel woocommerce_options_panel hidden">

            <?php 
        do_action( 'pph_before_editable_table' );
        ?>

            <?php 
        $this->editable_product_price_history( $id );
        ?>

        </div>

<?php 
    }

    /**
     * Build editable table.
     *  
     * @since     1.0.0
     */
    public function editable_product_price_history( $product_id, $no_entries_notice = true ) {
        $entries = Helper::get_price_history( $product_id );
        if ( !empty( $entries ) ) {
            // Get the date and time formats from the General Settings
            $date_format = get_option( 'date_format' );
            $count = count( $entries );
            do_action( 'pph_before_table_wrapper', $count );
            $limit = 1000;
            echo '<div class="pph-table-wrapper ' . esc_attr( Helper::plan( 'class' ) ) . '">';
            if ( $count > $limit ) {
                $entries = array_slice( $entries, -$limit, $limit );
                echo '<p><small>' . esc_html__( 'To ensure optimal performance, only a limited number of results are shown in the table.', 'product-price-history' ) . '</small></p>';
                printf( '<p>%s</p>', sprintf( 
                    /* translators: 1: number of displayed results, 2: total number of results */
                    esc_html__( 'Displaying %1$d out of %2$d results.', 'product-price-history' ),
                    esc_attr( $limit ),
                    esc_attr( $count )
                 ) );
            }
            echo '<table class="pph-table">';
            echo '<tr>';
            echo '<th class="short">' . esc_html__( 'Price', 'product-price-history' ) . '</th>';
            echo '<th class="short">' . esc_html__( 'Currency', 'product-price-history' ) . '</th>';
            echo '<th>' . esc_html__( 'Date', 'product-price-history' ) . '</th>';
            echo '<th class="short">' . esc_html__( 'Hide', 'product-price-history' ) . '</th>';
            echo '<th class="short pph-col-actions">' . esc_html__( 'Action', 'product-price-history' ) . '</th>';
            echo '</tr>';
            foreach ( $entries as $entry ) {
                $date_created = $entry['date_created'];
                if ( $date_format ) {
                    // Convert the database date to a timestamp
                    $timestamp = strtotime( $date_created );
                    // Format the date and time according to the settings
                    $formatted_date = date_i18n( $date_format, $timestamp );
                    $formatted_time = date_i18n( 'H:i:s', $timestamp );
                    $date_created = $formatted_date . ' ' . $formatted_time;
                }
                echo '<tr data-id="' . esc_attr( $entry['id'] ) . '">';
                echo '<td><input type="number" name="pph_sale_price" value="' . esc_attr( $entry['price'] ) . '" step="0.01" min="0" disabled /></td>';
                echo '<td><input type="text" name="pph_currency" value="' . esc_attr( $entry['currency'] ) . '" maxlength="3" disabled /></td>';
                echo '<td><input type="text" name="pph_date_created" value="' . esc_attr( $date_created ) . '" data-ymd-date="' . esc_attr( $entry['date_created'] ) . '" data-date-format="' . esc_attr( $date_format ) . '" placeholder="YYYY-MM-DD HH:MM:SS" disabled /></td>';
                echo '<td class="center"><input type="checkbox" name="pph_hidden" value="1" ' . checked( esc_attr( $entry['hidden'] ), true, false ) . ' /></td>';
                echo '<td class="center"><div class="pph-actions"><span class="pph-action-icon pph-edit-entry" title="Edit"></span><span class="pph-action-icon pph-delete-entry" title="Delete"></span></div></td>';
                echo '</tr>';
            }
            echo '</table>';
            echo '<button class="pph-delete-all button" data-id="' . esc_attr( $product_id ) . '">' . esc_html__( 'Delete all entries', 'product-price-history' ) . '</button>';
            echo '</div>';
        } else {
            if ( $no_entries_notice ) {
                echo '<div style="padding: 1rem;">';
                echo esc_html__( 'No pricing data recorded since plugin activation - This message indicates that no pricing data has been saved in the database since the activation of the plugin. This can occur if the plugin was recently installed or if the product price has not changed since the activation of the plugin.', 'product-price-history' );
                echo '</div>';
            }
        }
    }

    /**
     * Output editable table to variation panel.
     *  
     * @since     1.0.0
     */
    public function variation_panel( $loop, $variation_data, $variation ) {
        $this->editable_product_price_history( $variation->ID, false );
    }

    /**
     * Create entry on price change.
     *  
     */
    public function update_product( $product, $data_store ) {
        $interval = null;
        static $processed = [];
        $product_id = $product->get_id();
        if ( in_array( $product_id, $processed, true ) ) {
            return;
        }
        $processed[] = $product_id;
        if ( $product->is_type( 'variable' ) ) {
            $values = $this->build_variable_parent_values( $product );
        } else {
            // Simple, Variations, etc.
            $values = $this->build_regular_product_values( $product );
        }
        // Nothing to save?
        if ( empty( $values ) ) {
            return;
        }
        Helper::create_entry( $product, $interval, $values );
        do_action( 'pph_after_record_saved', $product, $interval );
    }

    private function build_variable_parent_values( $product ) {
        $parent_id = $product->get_id();
        $latest_entries = Helper::get_variable_latest_entries( $parent_id );
        if ( empty( $latest_entries ) ) {
            return null;
        }
        // Find lowest current variation price
        $min_entry = null;
        $min_price = null;
        foreach ( $latest_entries as $entry ) {
            $p = (float) $entry['price'];
            if ( $min_price === null || $p < $min_price ) {
                $min_price = $p;
                $min_entry = $entry;
            }
        }
        if ( !$min_entry ) {
            return null;
        }
        return [
            'regular_price'     => $min_entry['regular_price'],
            'sale_price'        => $min_entry['sale_price'],
            'price'             => $min_entry['price'],
            'currency'          => $min_entry['currency'],
            'product_type'      => 'variable',
            'parent_product_id' => $parent_id,
        ];
    }

    private function build_regular_product_values( $product ) {
        $changes = $product->get_changes();
        // New values from classic admin (simple only)
        $req_regular = ( isset( $_REQUEST['_regular_price'] ) ? wc_format_decimal( wc_clean( $_REQUEST['_regular_price'] ) ) : null );
        $req_sale = ( isset( $_REQUEST['_sale_price'] ) ? wc_format_decimal( wc_clean( $_REQUEST['_sale_price'] ) ) : null );
        // New values from REST/block editor
        $rest_regular = ( isset( $changes['regular_price'] ) ? wc_format_decimal( $changes['regular_price'] ) : null );
        $rest_sale = ( isset( $changes['sale_price'] ) ? wc_format_decimal( $changes['sale_price'] ) : null );
        // Compute new values
        $new_regular = $req_regular ?? $rest_regular ?? $product->get_regular_price();
        $new_sale = $req_sale ?? $rest_sale ?? $product->get_sale_price();
        // New effective
        $new_effective = ( $new_sale > 0 ? $new_sale : $new_regular );
        // Old effective (BEFORE save)
        $old_effective = $product->get_price();
        if ( !Helper::did_price_change( $old_effective, $new_effective ) ) {
            return null;
        }
        return [
            'regular_price' => $new_regular,
            'sale_price'    => $new_sale,
            'price'         => $new_effective,
        ];
    }

    /**
     * Ajax action for updating editable table fields.
     *  
     * @since     1.0.0
     */
    public function update_pph_db_row() {
        check_ajax_referer( 'pph-nonce', 'security' );
        $id = ( isset( $_POST['args']['id'] ) ? intval( $_POST['args']['id'] ) : null );
        $data = [];
        if ( isset( $_POST['args']['hidden'] ) ) {
            $data['hidden'] = absint( $_POST['args']['hidden'] ) || 0;
        }
        $response = Helper::update_entry( $id, $data );
        wp_send_json( $response );
        wp_die();
    }

    /**
     * Ajax action for deleting all product entries.
     *  
     * @since     2.1.0
     */
    public function delete_pph_product_entries() {
        check_ajax_referer( 'pph-nonce', 'security' );
        $id = ( isset( $_POST['args']['id'] ) ? intval( $_POST['args']['id'] ) : null );
        $delete = Helper::delete_product_entries( $id );
        wp_send_json( $delete );
        wp_die();
    }

    /**
     * Ajax action for deleting single product entry.
     *  
     * @since     2.1.0
     */
    public function delete_pph_entry() {
        check_ajax_referer( 'pph-nonce', 'security' );
        $id = ( isset( $_POST['args']['id'] ) ? intval( $_POST['args']['id'] ) : null );
        $delete = Helper::delete_entry( $id );
        wp_send_json( $delete );
        wp_die();
    }

    /**
     * When deleting the product or variation, delete also entries from pph table.
     *  
     * @since     1.0.0
     */
    public function remove_deleted_products_from_pph_table( $post_id, $post ) {
        if ( $post->post_type === 'product' || $post->post_type === 'product_variation' ) {
            Helper::delete_product_entries( $post_id );
        }
    }

}
