<?php
namespace ARCHM\Admin;

if ( ! defined('ABSPATH') ) {
    // phpcs:ignore Universal.PHP.RequireExitDieParentheses.Missing
    exit;
}

use Automattic\WooCommerce\Utilities\OrderUtil;

class ArchiveColumn {

    public function __construct() {
		$wc_order_screen_id = OrderUtil::custom_orders_table_usage_is_enabled() ? \wc_get_page_screen_id( 'shop-order' ) : 'shop_order';
        add_action('woocommerce_shop_order_list_table_custom_column', [ $this, 'display_wc_arcive_column_content' ], 10, 2);
        add_filter('woocommerce_shop_order_list_table_columns', [ $this, 'add_wc_archive_column' ]);
        add_filter("manage_{$wc_order_screen_id}_columns", [ $this, 'add_wc_archive_action_column' ], 20);
        add_filter("manage_edit-{$wc_order_screen_id}_columns", [ $this, 'add_wc_archive_action_column' ], 20);
    }

    /**
     * Add the "Archive" column to the post type columns.
     *
     * @param array $columns
     *
     * @return array
     * @since 1.0
     * @author Siam Maruf
     */
    // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.arrayFound
    public function add_archive_column( array $columns ): array {
        $archm_db_switch = get_option('archm_db_switch');
        $archive_column = $columns['archive'];
        unset($columns['archive']);

        $tags_column_index = array_search('tags', array_keys($columns));

        return array_slice($columns, 0, $tags_column_index, true) +
        [ 'archive' => $archive_column ] +
        array_slice($columns, $tags_column_index, count($columns) - 1, true);
    }

    /**
     * Add archive cloumn in WC oder list
     *
     * @return WCcolumn
     */
    public function add_wc_archive_column( $columns ) {
        $status_column_index = array_search('billing_address', array_keys($columns));

        $columns = array_slice($columns, 0, $status_column_index, true) +
        [ 'archive' => __('Archive', 'archive-master') ] +
        array_slice($columns, $status_column_index, count($columns) - 1, true);

        return $columns;
    }

    /**
     * Display content for the "Archive" column.
     *
     * @param string $column_name
     * @param int $post_id
     * @since 1.0
     * @author Siam Maruf
     */
    // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
    public function display_archive_column_content( $column_name, $post_id ): void {
        if ( 'archive' === $column_name ) {

            $is_archived = get_post_meta($post_id, 'is_archived', true);
            $background_color = $is_archived ? '#90EE90' : '#f0f0f0';
            $archive_content = $is_archived ? __('Yes', 'archive-master') : __('No', 'archive-master');
            $style = 'padding:5px 10px;border-radius:50px;background-color: ' . $background_color . ';';

            echo '<span style="' . esc_attr($style) . '">' . esc_html($archive_content) . '</span>';
        } elseif ( 'action' === $column_name ) {
            $button_style = 'background-color:#0073aa;color:#ffffff;padding:6px 12px;border-radius:20px;text-decoration:none;display:inline-block;';
            echo '<a href="javaScript:void(0)" id="_waRestoreBtn" class="wa-restore-button" style="' . esc_attr($button_style) . '" data-post-id="' . esc_attr($post_id) . '">' . esc_html__('Restore', 'archive-master') . '</button>';
        }
    }

    /**
     * Display WC archive column content
     *
     * @return void
     */
    public function display_wc_arcive_column_content( $column_name, $order ) {
		//var_dump($order->get_id());
        if ( 'archive' === $column_name ) {
            $is_archived = (int) get_option('archm_db_switch');
            $background_color = $is_archived ? '#90EE90' : '#f0f0f0';
            $archive_content = $is_archived ? __('Yes', 'archive-master') : __('No', 'archive-master');
            $style = 'padding:5px 10px;border-radius:50px;background-color: ' . $background_color . ';';

            echo '<span style="' . esc_attr($style) . '">' . esc_html($archive_content) . '</span>';
        } elseif ( 'action' === $column_name ) {
            $button_style = 'background-color:#0073aa;color:#ffffff;padding:6px 12px;border-radius:20px;text-decoration:none;display:inline-block;';
            echo '<a href="javaScript:void(0)" id="_waRestoreBtn" class="wa-restore-button" style="' . esc_attr($button_style) . '" data-post-id="' . esc_attr($order->get_id()) . '">' . esc_html__('Restore', 'archive-master') . '</button>';
        }
    }

    /**
     * Fix the title of the "Archive" column.
     *
     * @param array $column_name
     *
     * @return array
     * @since 1.0
     * @author 1.0
     */
    // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.arrayFound
    public function fix_archive_column_title( array $column_name ): array {
        $column_name['archive'] = __('Archive', 'archive-master');
        $archm_db_switch = get_option('archm_db_switch');
        if ( 1 === $archm_db_switch ) {
            $column_name['actions'] = __('Actions', 'archive-master');
        }
        return $column_name;
    }

    /**
     * Add action column for restore
     *
     * @param [type] $columns
     * @return column
     */
    public function add_wc_archive_action_column( $columns ) {
        $is_archived = (int) get_option('archm_db_switch');
        if ( $is_archived ) {
            $columns['action'] = __('Actions', 'archive-master');
        }

        return $columns;
    }
}
