<?php
namespace PSSG_Sync_Sheet\App\Handle;

use PSSG_Sync_Sheet\App\Core\Admin_Base;
use PSSG_Sync_Sheet\App\Service\Standalone;
use PSSG_Sync_Sheet\App\Service\Products;
use PSSG_Sync_Sheet\App\Http\Sheet;

class Thumbs_Handle extends Admin_Base
{
    use Standalone;

    public function __construct()
    {
        parent::__construct();
        add_action( 'pssg_admin_section_bottom', [$this, 'render_settings'], 8, 2 );

        // Add Thumbnail Column
        add_filter( 'pssg_products_columns', [$this, 'handle_columns'] );
        
        // Handle Thumbnail Data
        add_filter( 'pssg_each_col_data', [$this, 'handle_each_col'], 10, 3 );
        //pssg_quick_edit_table_each_td_data
        //on table only show as image using pssg_quick_edit_table_each_td_data
        add_filter( 'pssg_quick_edit_table_each_td_data', [$this, 'handle_image_quick_edit'], 10, 2 );
    }

    /**
     * Inject Thumbnail column into products columns
     *
     * @param array $columns
     * @return array
     */
    public function handle_columns( $columns )
    {
        $on_off_thumbnail = $this->configs['on_off_thumbnail'] ?? 'off';

        if ( $on_off_thumbnail == 'on' && !isset( $columns['_thumbnail'] ) ) {
            $columns = $this->array_insert_before(
                $columns,
                [
                    '_thumbnail' => [
                        'type'  => '_post_thumbnail',
                        'title' => __( 'Pic', 'product-sync-master-sheet' )
                    ]
                ],
                'title'
            );
        }

        return $columns;
    }

    /**
     * Render Thumbnail data as Google Sheets =IMAGE() formula
     *
     * @param string $html
     * @param array $col
     * @param int $product_id
     * @return string
     */
    public function handle_each_col( $html, $col, $product_id )
    {
        $type = $col['type'] ?? '';
        $thumbnail_size = $this->configs['thumbnail_size'] ?? 'thumbnail'; // You can change this to 'medium', 'large', or any custom size
        if( $type === '_post_thumbnail' ){
            $product = wc_get_product($product_id);
            if( $product ){
                $image_id = $product->get_image_id();
                $image_url = wp_get_attachment_image_url($image_id, $thumbnail_size);
                $full_image_url = wp_get_attachment_image_url($image_id, 'full');
                if( ! empty( $image_url ) && filter_var( $image_url, FILTER_VALIDATE_URL ) ){
                    //$html = '=IMAGE("' . $image_url . '", 4, 30, 30)';
                    //user HYPERLINK formula to make the image clickable and link to the full image
                    $html = '=HYPERLINK("' . $full_image_url . '", IMAGE("' . $image_url . '", 4, 30, 30))';

                }
            }
        }

        return $html;
    }

    /**
     * Render Thumbnail in Quick Edit table
     *
     * @param array $each_td_data
     * @param int $product_id
     * @return array
     */
    public function handle_image_quick_edit( $each_td_data, $product_id )
    {
        if($each_td_data['keyword'] === '_thumbnail'){
            $product = wc_get_product($product_id);
            if( $product ){
                $image_id = $product->get_image_id();
                $image_url = wp_get_attachment_image_url($image_id, 'thumbnail');
                if( ! empty( $image_url ) && filter_var( $image_url, FILTER_VALIDATE_URL ) ){
                    $each_td_data['td'] = '<img src="' . esc_url( $image_url ) . '" alt="' . esc_attr__( 'Product Thumbnail', 'product-sync-master-sheet' ) . '" style="max-width: 25px; height: auto;" />';
                } else {
                    $each_td_data['td'] = '';
                }
            }
        }
        
        // $col = $each_td_data['col'] ?? [];
        // $type = $col['type'] ?? '';
        // $thumbnail_size = $this->configs['thumbnail_size'] ?? 'thumbnail'; // You can change this to 'medium', 'large', or any custom size

        // if( $type === '_post_thumbnail' ){
        //     $product = wc_get_product($product_id);
        //     if( $product ){
        //         $image_id = $product->get_image_id();
        //         $image_url = wp_get_attachment_image_url($image_id, $thumbnail_size);
        //         if( ! empty( $image_url ) && filter_var( $image_url, FILTER_VALIDATE_URL ) ){
        //             $each_td_data['data'] = '<img src="' . esc_url( $image_url ) . '" alt="' . esc_attr__( 'Product Thumbnail', 'product-sync-master-sheet' ) . '" style="max-width: 50px; height: auto;" />';
        //         } else {
        //             $each_td_data['data'] = '';
        //         }
        //     }
        // }

        return $each_td_data;
    }


    /**
     * Render settings for Thumbnail column
     *
     * @param array $saved_data
     * @param string $page_name
     * @return void
     */
    public function render_settings($saved_data, $page_name)
    {
        if( $page_name !== 'settings' ) return;

        $on_off_thumbnail = $saved_data['on_off_thumbnail'] ?? 'off';
        $checked_thumbnail = $on_off_thumbnail == 'on' ? 'checked' : '';
        ?>
        <tr>
            <td>
                <div class="pssg-form-control">
                    <div class="form-label col-lg-4">
                        <label for="on_off_thumbnail"> <?php echo esc_html__( 'Thumbnail', 'product-sync-master-sheet' ); ?></label>
                    </div>
                    <div class="form-field col-lg-8">
                        <?php 
                        // dd(defined( 'PSSGP_DEV_VERSION' ) );
                        ?>
                        <label class="switch">
                            <input name="on_off_thumbnail" type="checkbox" id="on_off_thumbnail" <?php echo esc_attr( $checked_thumbnail ); ?>>
                            <div class="slider round">
                                <span class="on">ON</span><span class="off">OFF</span>
                            </div>
                        </label>
                    </div>
                </div>
            </td>
            <td>
                <div class="pssg-form-info">
                    <p><?php echo esc_html__( 'Enable/Disable Product Thumbnail column in Google Sheet.', 'product-sync-master-sheet' ); ?></p>
                </div> 
            </td>
        </tr>
        <?php
    }
}
