<?php

namespace iwbvel\classes\product_table;

defined('ABSPATH') || exit(); // Exit if accessed directly

use iwbvel\classes\helpers\Meta_Field as Meta_Field_Helper;
use iwbvel\classes\helpers\Sanitizer;
use iwbvel\classes\repositories\meta_field\ACF_Plugin_Fields;
use iwbvel\classes\repositories\Column;
use iwbvel\classes\repositories\Product;
use iwbvel\classes\repositories\Setting;

class RowHandler
{
    const CELL_CONTENT_LIMIT = 30;

    private static $instance;
    private $sticky_first_columns;
    private $product_object;
    private $value;
    private $product_repository;
    private $column_key;
    private $decoded_column_key;
    private $column_data;
    private $field_type;
    private $fields_method;
    private $meta_fields;
    private $acf_fields;
    private $acf_fields_name;
    private $acf_taxonomy_name;
    private $enable_thumbnail_popup;
    private $display_cell_content;

    public static function get_instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    private function __construct()
    {
        $setting_repository = Setting::get_instance();
        $settings = $setting_repository->get_settings();
        $this->sticky_first_columns = isset($settings['sticky_first_columns']) ? $settings['sticky_first_columns'] : 'yes';
        $this->enable_thumbnail_popup = isset($settings['enable_thumbnail_popup']) ? $settings['enable_thumbnail_popup'] : 'yes';
        $this->display_cell_content = (!empty($settings['display_cell_content']) && $settings['display_cell_content'] == 'short') ? 'short' : 'long';
        $this->field_type = "";
        $this->fields_method = $this->get_fields_method();

        $acf = ACF_Plugin_Fields::get_instance('product');
        $this->acf_fields = $acf->get_fields();
        $this->acf_fields_name = (is_array($this->acf_fields)) ? array_keys($this->acf_fields) : [];
        $this->product_repository = Product::get_instance();
    }

    public function get_row($product_object, $columns)
    {
        if (!($product_object instanceof \WC_Product)) {
            return '';
        }

        $this->product_object = $product_object;
        $values = $this->product_repository->get_product_column_values($product_object, $columns);

        $output = '<tr data-item-id="' . esc_attr($product_object->get_id()) . '" data-item-type="' . esc_attr($product_object->get_type()) . '">';
        $output .= $this->get_static_columns();
        if (!empty($columns) && is_array($columns)) {
            foreach ($columns as $column_key => $column_data) {
                $this->column_key = $column_key;
                $this->column_data = $column_data;
                $this->value = $values[$column_data['name']];
                if (in_array($column_key, $this->acf_fields_name)) {
                    $this->column_data['content_type'] = Meta_Field_Helper::get_field_type_by_acf_type($this->acf_fields[$column_key])['column_type'];
                }
                $this->decoded_column_key = (substr($this->column_key, 0, 3) == 'pa_') ? strtolower(urlencode($this->column_key)) : urlencode($this->column_key);
                $output .= $this->get_field();
            }
        }

        $output .= "</tr>";
        return $output;
    }

    private function get_field()
    {
        $output = '';
        $this->field_type = '';

        $color = $this->get_column_colors_style();
        $sub_name = (!empty($this->column_data['sub_name'])) ? $this->column_data['sub_name'] : '';
        $update_type = (!empty($this->column_data['update_type'])) ? $this->column_data['update_type'] : '';
        $output .= '<td data-item-id="' . esc_attr($this->product_object->get_id()) . '" data-item-title="' . esc_attr($this->product_object->get_name()) . '" data-col-title="' . esc_attr($this->column_data['title']) . '" data-field="' . esc_attr($this->column_key) . '" data-field-type="' . esc_attr($this->field_type) . '" data-name="' . esc_attr($this->column_data['name']) . '" data-sub-name="' . esc_attr($sub_name) . '" data-update-type="' . esc_attr($update_type) . '" data-fetch-type="' . esc_attr($this->column_data['fetch_type']) . '" style="' . esc_attr($color['background']) . ' ' . esc_attr($color['text']) . '"';
        if ($this->column_data['editable'] === true && !in_array($this->column_data['content_type'], ['multi_select', 'multi_select_attribute'])) {
            $output .= ' data-content-type="' . esc_attr($this->column_data['content_type']) . '" data-action="inline-editable"';
        }
        $output .= '>';

        if (empty($this->column_data['allowed_type']) || (!empty($this->column_data['allowed_type']) && in_array($this->product_object->get_type(), $this->column_data['allowed_type']))) {
            $generated = $this->generate_field();
            if (is_array($generated) && isset($generated['field']) && isset($generated['includes'])) {
                $output .= $generated['field'];
            } else {
                $output .= $generated;
            }
        } else {
            $value = '<i class="iwbvel-icon-slash"></i>';
            $output .= $value;
        }

        $output .= '</td>';
        return $output;
    }

    private function get_column_colors_style()
    {
        $color['background'] = (!empty($this->column_data['background_color']) && $this->column_data['background_color'] != '#fff' && $this->column_data['background_color'] != '#ffffff') ? 'background:' . esc_attr($this->column_data['background_color']) . ';' : '';
        $color['text'] = (!empty($this->column_data['text_color'])) ? 'color:' . esc_attr($this->column_data['text_color']) . ';' : '';
        return $color;
    }

    private function generate_field()
    {
        if (isset($this->fields_method[$this->column_data['content_type']]) && method_exists($this, $this->fields_method[$this->column_data['content_type']])) {
            return $this->{$this->fields_method[$this->column_data['content_type']]}();
        } else {
            return (is_array($this->value)) ? implode(',', $this->value) : $this->value;
        }
    }

    private function get_id_column()
    {
        $output = '';
        if (Column::SHOW_ID_COLUMN === true) {
            $id_for_edit = ($this->product_object->get_type() == 'variation') ? $this->product_object->get_parent_id() : $this->product_object->get_id();
            $delete_type = 'trash';
            $delete_label = esc_html__('Delete product', 'ithemeland-bulk-variation-editing-for-woocommerce');
            $restore_button = '';
            $view_button = '';
            $edit_button = '';

            if ($this->product_object->get_status() == 'trash') {
                $delete_type = 'permanently';
                $delete_label = esc_html__('Delete permanently', 'ithemeland-bulk-variation-editing-for-woocommerce');
                $restore_button = '<button type="button" style="height: 28px;" class="iwbvel-ml5 iwbvel-button-flat iwbvel-text-green iwbvel-float-right iwbvel-restore-item-btn" data-item-id="' . esc_attr($this->product_object->get_id()) . '" title="' . esc_attr__('Restore', 'ithemeland-bulk-variation-editing-for-woocommerce') . '"><span class="iwbvel-icon-rotate-cw"></span></button>';
            } else {
                $view_button = '<a href="' . esc_url(get_the_permalink($this->product_object->get_id())) . '" target="_blank" title="' . esc_attr__('View on site', 'ithemeland-bulk-variation-editing-for-woocommerce') . '" style="height: 28px;" class="iwbvel-item-view-icon iwbvel-float-right iwbvel-ml5"><span class="iwbvel-icon-eye1" style="vertical-align: middle;"></span></a>';
                $edit_button = '<a href="' . admin_url("post.php?post=" . esc_attr($id_for_edit) . "&action=edit") . '" target="_blank" class="iwbvel-ml5 iwbvel-float-right" title="' . esc_attr__('Edit product', 'ithemeland-bulk-variation-editing-for-woocommerce') . '" style="height: 28px;"><span class="iwbvel-icon-pencil" style="vertical-align: middle;"></span></a>';
            }

            $sticky_class = ($this->sticky_first_columns == 'yes') ? 'iwbvel-td-sticky iwbvel-td-sticky-id iwbvel-gray-bg' : '';
            $output .= '<td data-item-id="' . esc_attr($this->product_object->get_id()) . '" data-item-title="' . esc_attr($this->product_object->get_name()) . '" data-col-title="ID" class="' . esc_attr($sticky_class) . '">';
            $output .= '<label class="iwbvel-td140">';
            $output .= '<input type="checkbox" class="iwbvel-check-item" data-item-type="' . esc_attr($this->product_object->get_type()) . '" value="' . esc_attr($this->product_object->get_id()) . '" title="Select Item">';
            $output .= esc_html($this->product_object->get_id());
            $output .= $restore_button;
            $output .= $view_button;
            $output .= '<button type="button" class="iwbvel-ml5 iwbvel-button-flat iwbvel-float-right iwbvel-text-red iwbvel-delete-item-btn" data-delete-type="' . esc_attr($delete_type) . '" data-item-id="' . esc_attr($this->product_object->get_id()) . '" title="' . $delete_label . '"><span class="iwbvel-icon-trash-2"></span></button>';
            if ($this->product_object->get_type() != 'variation') {
                $output .= '<button title="Edit Variations" style="height: 28px;" type="button" class="iwbvel-ml5 iwbvel-button-flat iwbvel-float-right iwbvel-variation-item-btn" data-toggle="float-side-modal-after-confirm" data-target="#iwbvel-float-side-modal-variations-bulk-edit"><span class="iwbvel-icon-tree"></span></button>';
            }
            $output .= $edit_button;
            $output .= "</label>";
            $output .= "</td>";
        }
        return $output;
    }

    private function get_static_columns()
    {
        $output = '';
        $output .= $this->get_id_column();
        $static_columns = Column::get_static_columns();
        if (!empty($static_columns)) {
            foreach ($static_columns as $static_column) {
                if (!isset($static_column['fetch_type']) || !isset($static_column['name'])) {
                    continue;
                }
                if ($static_column['name'] == 'title' && $this->product_object->get_type() == 'variation') {
                    $full_text = $value = $this->product_object->get_title() . ' - ' . wc_get_formatted_variation($this->product_object->get_variation_attributes(), true, false);
                } else {
                    $full_text = $value = $this->product_repository->get_product_field($this->product_object, $static_column['fetch_type'], $static_column['name']);
                }
                if ($this->display_cell_content == 'short' && strlen($value) > self::CELL_CONTENT_LIMIT) {
                    $value = mb_substr($value, 0, self::CELL_CONTENT_LIMIT) . '...';
                }
                $sticky_class = ($this->sticky_first_columns == 'yes') ? 'iwbvel-td-sticky iwbvel-td-sticky-title iwbvel-gray-bg' : '';
                $output .= '<td class="' . esc_attr($sticky_class) . '" data-update-type="woocommerce_field" data-fetch-type="' . esc_attr($static_column['fetch_type']) . '" data-name="' . esc_attr($static_column['name']) . '" data-item-id="' . esc_attr($this->product_object->get_id()) . '" data-item-title="' . esc_attr($this->product_object->get_name()) . '" data-col-title="' . esc_attr($static_column['label']) . '" data-field="' . esc_attr($static_column['name']) . '" data-field-type="" data-content-type="text" data-action="inline-editable">';
                $output .= '<span data-action="inline-editable" data-full-text="' . esc_attr($full_text) . '" class="iwbvel-td160">' . esc_html($value) . '</span>';
                $output .= '</td>';
            }
        }
        return $output;
    }

    private function get_fields_method()
    {
        return [
            'text' => 'text_field',
            'password' => 'text_field',
            'email' => 'text_field',
            'url' => 'text_field',
            'textarea' => 'textarea_field',
            'image' => 'image_field',
            'gallery' => 'gallery_field',
            'regular_price' => 'regular_price_field',
            'sale_price' => 'sale_price_field',
            'numeric' => 'numeric_field',
            'numeric_without_calculator' => 'numeric_without_calculator_field',
            'checkbox_dual_mode' => 'checkbox_dual_mode_field',
            'checkbox' => 'checkbox_dual_mode_field',
            'radio' => 'radio_field',
            'file' => 'select_custom_field_files_field',
            'select_files' => 'select_files_field',
            'select_author' => 'select_author_field',
            'select_products' => 'select_products_field',
            'select' => 'select_field',
            'yith_shop_vendor' => 'yith_shop_vendor_field',
            'wc_product_vendor' => 'wc_product_vendor_field',
            'date' => 'date_picker_field',
            'date_picker' => 'date_picker_field',
            'date_time_picker' => 'date_time_picker_field',
            'time_picker' => 'time_picker_field',
            'color_picker' => 'color_picker_field',
            'taxonomy' => 'multi_select_field',
            'multi_select' => 'multi_select_field',
            'yith_product_badge' => 'yith_product_badge_field',
            'ithemeland_badge' => 'ithemeland_badge_field',
            'yikes_custom_product_tabs' => 'yikes_custom_product_tabs_field',
            'it_wc_dynamic_pricing_select_roles' => 'it_wc_dynamic_pricing_select_roles_field',
            'it_pricing_rules_product' => 'it_pricing_rules_product_field',
            'it_wc_dynamic_pricing_all_fields' => 'it_wc_dynamic_pricing_all_fields_field',
        ];
    }

    private function text_field()
    {
        $full_text = $value = (is_array($this->value)) ? implode(',', $this->value) : $this->value;
        if ($this->display_cell_content == 'short' && strlen($value) > self::CELL_CONTENT_LIMIT) {
            $value = mb_substr($value, 0, self::CELL_CONTENT_LIMIT) . '...';
        }

        $action = ($this->column_data['editable'] === true) ? 'inline-editable' : '';

        return "<span data-action='" . esc_attr($action) . "' class='iwbvel-td160' data-full-text='" . esc_attr($full_text) . "'>" . esc_html($value) . "</span>";
    }

    private function textarea_field()
    {
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-load-text-editor' : '';
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-text-editor' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat " . esc_attr($action) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function image_field()
    {
        $output = '';
        $image_id = 0;
        if (isset($this->value['id'])) {
            $image_id = intval($this->value['id']);
        }
        if (isset($this->value) && is_numeric($this->value)) {
            $image_id = intval($this->value);
        }

        $image_url = wp_get_attachment_image_src($image_id, [40, 40]);
        $large = wp_get_attachment_image_src($image_id, 'large');
        $large = (!empty($large[0])) ? $large[0] : false;
        $full_size = wp_get_attachment_image_src($image_id, 'full');
        $full_size = (!empty($full_size[0])) ? $full_size[0] : esc_url(IWBVEL_IMAGES_URL . "/woocommerce-placeholder.png");
        $image = (!empty($image_url[0])) ? "<img src='" . esc_url($image_url[0]) . "' alt='' width='40' height='40' />" : '<button class="iwbvel-button iwbvel-button-flat"><i class="iwbvel-icon-image"></i></button>';
        $hover_box_class = ($this->enable_thumbnail_popup == 'yes') ? 'iwbvel-thumbnail' : '';

        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-image' : '';

        $output .= "<span data-toggle='modal' class='{$hover_box_class}' data-target='" . esc_attr($modal) . "' data-id='iwbvel-" . esc_attr($this->column_key) . "-" . esc_attr($this->product_object->get_id()) . "' class='iwbvel-image-inline-edit' data-full-image-src='" . esc_url($full_size) . "' data-image-id='" . esc_attr($image_id) . "'>";
        if ($this->enable_thumbnail_popup == 'yes' && !empty($large)) {
            $output .= '<div class="iwbvel-original-thumbnail"><img src="' . esc_url($large) . '"></div>';
        }
        $output .= $image;
        $output .= "</span>";
        return $output;
    }

    private function gallery_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-gallery' : '';

        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' class='iwbvel-button iwbvel-button-flat iwbvel-button-gallery' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-field='" . esc_attr($this->column_key) . "'><i class='iwbvel-icon-images'></i></button>";
    }

    private function regular_price_field()
    {
        $price = ($this->value != '') ? number_format(floatval($this->value), wc_get_price_decimals()) : '';
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-regular-price' : '';
        $output = "<span data-action='inline-editable' class='iwbvel-numeric-content iwbvel-td120'>" . esc_html($price) . "</span><button type='button' data-toggle='modal' class='iwbvel-calculator' data-target='" . esc_attr($modal) . "'></button>";
        return $output;
    }

    private function sale_price_field()
    {
        $price = ($this->value != '') ? number_format(floatval($this->value), wc_get_price_decimals()) : '';
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-sale-price' : '';
        $output = "<span data-action='inline-editable' class='iwbvel-numeric-content iwbvel-td120'>" . esc_html($price) . "</span><button type='button' data-toggle='modal' class='iwbvel-calculator' data-target='" . esc_attr($modal) . "'></button>";
        return $output;
    }

    private function numeric_field()
    {
        $field_arr = explode('_-_', $this->decoded_column_key);
        $field = (!empty($field_arr[0])) ? $field_arr[0] : $this->decoded_column_key;

        if (!empty($field_arr[1]) && is_array($this->value)) {
            if (!empty($this->value[0])) {
                $decoded = json_decode($this->value[0], true);
                $this->value = (is_array($decoded) && isset($decoded[$field_arr[1]])) ? $decoded[$field_arr[1]] : '';
            } else {
                $this->value = '';
            }
        }

        $action = ($this->column_data['editable'] === true) ? 'inline-editable' : '';

        return "<span data-action='" . esc_attr($action) . "' class='iwbvel-numeric-content iwbvel-td120'>" . esc_html($this->value) . "</span><button type='button' data-toggle='modal' class='iwbvel-calculator' data-field='" . esc_attr($this->column_key) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field-type='" . esc_attr($this->field_type) . "' data-target='#iwbvel-modal-numeric-calculator'></button>";
    }

    private function numeric_without_calculator_field()
    {
        $action = ($this->column_data['editable'] === true) ? 'inline-editable' : '';
        return "<span data-action='" . esc_attr($action) . "' class='iwbvel-numeric-content iwbvel-td120'>" . esc_html($this->value) . "</span>";
    }

    private function checkbox_dual_mode_field()
    {
        $checked =  ($this->value && $this->value !== 'no') ? 'checked="checked"' : '';
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        return "<label><input type='checkbox' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' value='yes' class='iwbvel-dual-mode-checkbox " . esc_attr($action) . "' " . esc_attr($checked) . "><span>" . esc_html__('Yes', 'ithemeland-bulk-variation-editing-for-woocommerce') . "</span></label>";
    }

    private function checkbox_field()
    {
        $output = "";
        if (!empty($this->acf_fields[$this->decoded_column_key]['choices']) && is_array($this->acf_fields[$this->decoded_column_key]['choices'])) {
            $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
            foreach ($this->acf_fields[$this->decoded_column_key]['choices'] as $choice_key => $choice_value) {
                $selected = isset($this->value) ? unserialize($this->value) : null;
                $checked = !empty($selected) && is_array($selected) && in_array($choice_key, $selected) ? 'checked="checked"' : '';
                $output = "<label><input type='checkbox' name='" . esc_attr($this->decoded_column_key . '-' . $this->product_object->get_id()) . "' value='" . esc_attr($choice_key) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' class='iwbvel-dual-mode-checkbox " . esc_attr($action) . "' " . esc_attr($checked) . ">" . esc_html($choice_value) . "</label>";
            }
        }
        return $output;
    }

    private function radio_field()
    {
        $output = '';
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        if (!empty($this->acf_fields[$this->decoded_column_key]['choices']) && is_array($this->acf_fields[$this->decoded_column_key]['choices'])) {
            foreach ($this->acf_fields[$this->decoded_column_key]['choices'] as $choice_key => $choice_value) {
                $checked = isset($this->value) && $this->value == $choice_key ? 'checked="checked"' : '';
                $output .= "<label><input type='radio' name='" . esc_attr($this->decoded_column_key . '-' . $this->product_object->get_id()) . "' value='" . esc_attr($choice_key) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' class='iwbvel-dual-mode-checkbox " . esc_attr($action) . "' " . esc_attr($checked) . ">" . esc_html($choice_value) . "</label>";
            }
        }

        if (empty($this->meta_fields)) {
            $this->meta_fields = $this->get_meta_fields();
        }

        if (!empty($this->meta_fields[$this->decoded_column_key]) && !empty($this->meta_fields[$this->decoded_column_key]['key_value'])) {
            $options = Meta_Field_Helper::key_value_field_to_array($this->meta_fields[$this->decoded_column_key]['key_value']);
            if (!empty($options) && is_array($options)) {
                $output .= "<select class='" . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' data-field-type='" . esc_attr($this->field_type) . "'>";
                $output .= '<option value="">Select</option>';
                foreach ($options as $option_key => $option_value) {
                    $selected = isset($this->value) && $this->value == $option_key ? 'selected' : '';
                    $output .= "<option value='{$option_key}' $selected>{$option_value}</option>";
                }
                $output .= '</select>';
            }
        }

        return $output;
    }

    private function file_field()
    {
        $file_id = (isset($this->value)) ? intval($this->value) : null;
        $file_url = !empty($file_id) ? wp_get_attachment_url($file_id) : 0;
        $file_url = !empty($file_url) ? esc_url($file_url) : '';
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-file' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-file-id='" . $file_id . "' data-file-url='" . $file_url . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function select_custom_field_files_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-custom-field-files' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function select_files_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-select-files' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function select_author_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-select-author' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function select_products_field()
    {
        $children_ids = '';
        switch ($this->column_key) {
            case '_children':
                if (is_array($this->value)) {
                    $children_ids = implode(',', $this->value);
                } else {
                    $children_ids = (!empty(unserialize($this->value))) ? implode(',', unserialize($this->value)) : '';
                }
                break;
            case 'upsell_ids':
            case 'cross_sell_ids':
                if (!empty($this->value) && is_array($this->value)) {
                    $children_ids = implode(',', $this->value);
                }
                break;
        }
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-select-products' : '';
        return  "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-children-ids='" . esc_attr($children_ids) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function select_field()
    {
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        $output = "<select class='" . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' data-field-type='" . esc_attr($this->field_type) . "'>";
        if (isset($this->column_data['options']) && is_array($this->column_data['options'])) {
            $product_repository = Product::get_instance();
            switch ($this->column_key) {
                case 'status':
                    $this->column_data['options'] = $product_repository->get_product_statuses();
                    break;
                case 'stock_status':
                    $this->column_data['options'] = wc_get_product_stock_status_options();
                    break;
                case 'backorders':
                    $this->column_data['options'] = wc_get_product_backorder_options();
                    break;
                case 'product_type':
                    $this->column_data['options'] = wc_get_product_types();
                    break;
                case 'catalog_visibility':
                    $this->column_data['options'] = wc_get_product_visibility_options();
                    break;
                case 'tax_class':
                    $this->column_data['options'] = $product_repository->get_tax_classes();
                    if ($this->product_object->get_type() == 'variation') {
                        $this->column_data['options'] = ['parent' => esc_html__('Same as parent', 'ithemeland-bulk-variation-editing-for-woocommerce')] + $this->column_data['options'];
                        $variation_tax_class = $this->product_object->get_tax_class();
                    }
                    break;
                case 'shipping_class':
                    $this->column_data['options'] = [
                        -1 => 'No Shipping Class',
                    ];
                    $shipping_items = wc()->shipping()->get_shipping_classes();
                    if (!empty($shipping_items)) {
                        foreach ($shipping_items as $shipping_class) {
                            $this->column_data['options'][$shipping_class->term_id] = $shipping_class->name;
                        }
                    }
                    break;
            }

            foreach ($this->column_data['options'] as $option_key => $option_value) {
                if (!empty($variation_tax_class)) {
                    $selected = ($option_key == $variation_tax_class) ? 'selected' : '';
                } else {
                    if (is_array($this->value)) {
                        $selected = (in_array($option_key, $this->value)) ? 'selected' : '';
                    } else {
                        $selected = ($option_key == $this->value) ? 'selected' : '';
                    }
                }
                $output .= "<option value='{$option_key}' $selected>{$option_value}</option>";
            }
        } else {
            if ($this->column_data['field_type'] == 'custom_field') {
                if (empty($this->meta_fields)) {
                    $this->meta_fields = $this->get_meta_fields();
                }

                if (!empty($this->meta_fields[$this->column_data['name']]) && !empty($this->meta_fields[$this->column_data['name']]['key_value'])) {
                    $options = Meta_Field_Helper::key_value_field_to_array($this->meta_fields[$this->column_data['name']]['key_value']);
                    if (!empty($options) && is_array($options)) {
                        foreach ($options as $option_key => $option_value) {
                            $selected = isset($this->value) && $this->value == $option_key ? 'selected' : '';
                            $output .= "<option value='{$option_key}' $selected>{$option_value}</option>";
                        }
                    }
                }
            }
        }

        if (!empty($this->acf_fields[$this->decoded_column_key]['choices']) && is_array($this->acf_fields[$this->decoded_column_key]['choices'])) {
            foreach ($this->acf_fields[$this->decoded_column_key]['choices'] as $choice_key => $choice_value) {
                $selected = isset($this->value) && $this->value == $choice_key ? 'selected' : '';
                $output .= "<option value='" . esc_attr($choice_key) . "' $selected>" . esc_html($choice_value) . "</option>";
            }
        }

        if (!empty($this->acf_fields[$this->decoded_column_key]['taxonomy'])) {
            $options = $this->get_taxonomy_terms($this->acf_fields[$this->decoded_column_key]['taxonomy']);
            if (!empty($options) && count($options)) {
                foreach ($options as $option_key => $option_value) {
                    $selected = isset($this->value) && $this->value == $option_key ? 'selected' : '';
                    $output .= "<option value='" . esc_attr($option_key) . "' $selected>" . esc_html($option_value) . "</option>";
                }
            }
        }

        $output .= '</select>';

        return $output;
    }

    private function yith_shop_vendor_field()
    {
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        $output = "<select class='" . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' data-field-type='" . esc_attr($this->field_type) . "'>";
        $output .= "<option value=''>" . esc_html__('Select', 'ithemeland-bulk-variation-editing-for-woocommerce') . "</option>";
        $product_repository = Product::get_instance();
        $yith_shop_vendor_object = $product_repository->get_yith_vendors();
        if (!empty($yith_shop_vendor_object)) {
            foreach ($yith_shop_vendor_object as $vendor) {
                if ($vendor instanceof \WP_Term) {
                    $selected = (in_array($vendor->slug, $this->value)) ? 'selected' : '';
                    $output .= "<option value='{$vendor->slug}' $selected>{$vendor->name}</option>";
                }
            }
        }
        $output .= '</select>';
        return $output;
    }

    private function wc_product_vendor_field()
    {
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        $output = "<select class='" . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' data-field-type='" . esc_attr($this->field_type) . "'>";
        $output .= "<option value=''>" . esc_html__('Select', 'ithemeland-bulk-variation-editing-for-woocommerce') . "</option>";
        $product_repository = Product::get_instance();
        $wc_product_vendor_object = $product_repository->get_wc_product_vendors();
        if (!empty($wc_product_vendor_object)) {
            foreach ($wc_product_vendor_object as $vendor) {
                if ($vendor instanceof \WP_Term) {
                    $selected = (in_array($vendor->slug, $this->value)) ? 'selected' : '';
                    $output .= "<option value='{$vendor->slug}' $selected>{$vendor->name}</option>";
                }
            }
        }
        $output .= '</select>';
        return $output;
    }

    private function date_picker_field()
    {
        $date = (!empty($this->value)) ? gmdate('Y/m/d', strtotime($this->value)) : '';
        $clear_button = ($this->decoded_column_key != 'date_created') ? "<button type='button' class='iwbvel-clear-date-btn iwbvel-inline-edit-clear-date' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' value=''><img src='" . esc_url(IWBVEL_IMAGES_URL . 'calendar_clear.svg') . "' alt='Clear' title='Clear Date'></button>" : '';
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        return "<input type='text' class='iwbvel-datepicker " . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' value='" . esc_attr($date) . "'>" . wp_kses($clear_button, Sanitizer::allowed_html());
    }

    private function date_time_picker_field()
    {
        $date = (!empty($this->value)) ? gmdate('Y/m/d H:i', strtotime($this->value)) : '';
        $clear_button = "<button type='button' class='iwbvel-clear-date-btn iwbvel-inline-edit-clear-date' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' value=''><img src='" . esc_url(IWBVEL_IMAGES_URL . 'calendar_clear.svg') . "' alt='Clear' title='Clear Date'></button>";
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        return "<input type='text' class='iwbvel-datetimepicker " . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' value='" . esc_attr($date) . "'>" . wp_kses($clear_button, Sanitizer::allowed_html());
    }

    private function time_picker_field()
    {
        $date = (!empty($this->value)) ? gmdate('H:i', strtotime($this->value)) : '';
        $clear_button = "<button type='button' class='iwbvel-clear-date-btn iwbvel-inline-edit-clear-date' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' value=''><img src='" . esc_url(IWBVEL_IMAGES_URL . 'calendar_clear.svg') . "' alt='Clear' title='Clear Date'></button>";
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        return "<input type='text' class='iwbvel-timepicker " . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' value='" . esc_attr($date) . "'>" . wp_kses($clear_button, Sanitizer::allowed_html());
    }

    private function color_picker_field()
    {
        $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
        return "<input type='text' class='iwbvel-color-picker-field " . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' value='" . esc_attr($this->value) . "'><button type='button' class='iwbvel-inline-edit-color-action'>" . esc_html__('Apply', 'ithemeland-bulk-variation-editing-for-woocommerce') . "</button>";
    }

    private function multi_select_field()
    {
        if (isset($this->acf_fields[$this->column_key]['taxonomy'])) {
            $values = '';
            $taxonomy = esc_attr($this->acf_fields[$this->column_key]['taxonomy']);
            $checked_ids = !is_array($this->value) ? unserialize($this->value) : $this->value;
            if (!empty($checked_ids)) {
                $checked = get_terms([
                    'taxonomy' => $this->acf_taxonomy_name,
                    'hide_empty' => false,
                    'include' => $checked_ids,
                    'fields' => 'id=>name'
                ]);
            }
        } else {
            $taxonomy = $this->column_key;
            $checked = wp_get_post_terms($this->product_object->get_id(), $taxonomy, ['fields' => 'names']);
        }

        if (!empty($checked) && is_array($checked)) {
            $checked_iteration = 1;
            foreach ($checked as $id => $name) {
                $separate = '';
                if ($this->display_cell_content == 'short') {
                    if ($checked_iteration < 2 && count($checked) > 1) {
                        $separate = ", ";
                    }
                    $values .= '<span class="iwbvel-category-item">' . esc_html($name) . $separate . ' </span>';
                    if ($checked_iteration >= 2 && count($checked) > 2) {
                        $values .= '...';
                        break;
                    }
                } else {
                    if ($checked_iteration < count($checked)) {
                        $separate = ", ";
                    }
                    $values .= '<span class="iwbvel-category-item">' . esc_html($name) . $separate . ' </span>';
                }

                $checked_iteration++;
            }
        }

        $list_html = strip_tags(wp_kses($values, Sanitizer::allowed_html()), '<span><ul><label><li>');
        if (mb_substr($this->decoded_column_key, 0, 3) == 'pa_') {
            $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-product-attribute' : '';
            $output = "<span data-toggle='modal' class='iwbvel-is-attribute-modal iwbvel-product-attribute' data-target='" . esc_attr($modal) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "'>";
            $output .= (!empty($list_html)) ? $list_html : 'No items';
            $output .= "</span>";
        } else {
            if (isset($this->acf_fields[$this->column_key]['taxonomy'])) {
                $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-acf-multi-select' : '';
                $output = "<span data-toggle='modal' class='iwbvel-is-taxonomy-modal iwbvel-acf-taxonomy-multi-select' data-target='" . esc_attr($modal) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "'>";
                $output .= (!empty($list_html)) ? $list_html : 'No items';
                $output .= "</span>";
            } else {
                $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-product-taxonomy' : '';
                $output = "<span data-toggle='modal' class='iwbvel-is-taxonomy-modal' data-target='" . esc_attr($modal) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "'>";
                $output .= (!empty($list_html)) ? $list_html : 'No items';
                $output .= "</span>";
            }
        }

        return $output;
    }

    private function yith_product_badge_field()
    {
        $output = "";
        if (defined("YITH_WCBM_INIT")) {
            // is premium plugin - multiple
            $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-product-badges' : '';
            $output = "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
        } else {
            // is free plugin - single
            $product_badges = get_posts(['post_type' => 'yith-wcbm-badge', 'posts_per_page' => -1, 'order' => 'ASC']);
            $action = ($this->column_data['editable'] === true) ? 'iwbvel-inline-edit-action' : '';
            $output = "<select class='" . esc_attr($action) . "' data-field='" . esc_attr($this->column_key) . "' data-item-id='" . esc_attr($this->product_object->get_id()) . "' title='Select " . esc_attr($this->column_data['label']) . "' data-field-type='" . esc_attr($this->field_type) . "'>";
            $output .= "<option value=''>" . esc_html__('No badge', 'ithemeland-bulk-variation-editing-for-woocommerce') . "</option>";
            if (!empty($product_badges)) {
                foreach ($product_badges as $badge) {
                    if ($badge instanceof \WP_Post) {
                        if (is_array($this->value)) {
                            $selected = (in_array($badge->ID, $this->value)) ? 'selected' : '';
                        } else {
                            $selected = ($badge->ID == $this->value) ? 'selected' : '';
                        }
                        $output .= "<option value='{$badge->ID}' $selected>{$badge->post_title}</option>";
                    }
                }
            }
            $output .= '</select>';
        }

        return $output;
    }

    private function ithemeland_badge_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-ithemeland-badge' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat iwbvel-ithemeland-badge-button' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function yikes_custom_product_tabs_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-yikes-custom-product-tabs' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function it_wc_dynamic_pricing_all_fields_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-it-wc-dynamic-pricing-all-fields' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "'data-item-type='" . esc_attr($this->product_object->get_type()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function it_pricing_rules_product_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-it-wc-dynamic-pricing' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function it_wc_dynamic_pricing_select_roles_field()
    {
        $modal = ($this->column_data['editable'] === true) ? '#iwbvel-modal-it-wc-dynamic-pricing-select-roles' : '';
        return "<button type='button' data-toggle='modal' data-target='" . esc_attr($modal) . "' class='iwbvel-button iwbvel-button-flat' data-item-id='" . esc_attr($this->product_object->get_id()) . "' data-item-name='" . esc_attr($this->product_object->get_name()) . "' data-field='" . esc_attr($this->column_key) . "' data-field-type='" . esc_attr($this->field_type) . "'><i class='iwbvel-icon-edit'></i></button>";
    }

    private function get_taxonomy_terms($taxonomy)
    {
        $terms = get_terms([
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
        ]);

        $options = [];
        if (!empty($terms) && count($terms)) {
            foreach ($terms as $term) {
                if ($term instanceof \WP_Term) {
                    $options[$term->term_id] = $term->name;
                }
            }
        }

        return $options;
    }

    private function get_meta_fields()
    {
        return get_option('iwbvel_meta_fields', []);
    }
}
