<?php

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'WC_Aelia_CurrencySwitcher' ) ) {
    return;
}

use Aelia\WC\CurrencySwitcher\WC_Aelia_CurrencySwitcher;
use Aelia\WC\CurrencySwitcher\WC_Aelia_Currencies_Manager;


if ( ! class_exists( 'PWBE_WC_Aelia_CurrencySwitcher' ) ) :

final class PWBE_WC_Aelia_CurrencySwitcher {

    function __construct() {
        global $wpdb;

        add_filter( 'pwbe_product_columns', array( $this, 'pwbe_product_columns' ) );
        add_filter( 'pwbe_results_decode_array', array( $this, 'results_decode_array' ), 10, 4 );
        add_filter( 'pwbe_before_save_array_value', array( $this, 'before_save_array_value' ), 10, 3 );
        add_filter( 'pwbe_save_array_value', array( $this, 'save_array_value' ), 10, 3 );
    }

    function pwbe_product_columns( $columns ) {
        $new_columns = array();
        $insert_after_column = __( 'Sale price', 'woocommerce' );

        $currencyprices_manager = WC_Aelia_CurrencySwitcher::instance()->currencyprices_manager();
        $enabled_currencies = $currencyprices_manager->enabled_currencies(false);
        $base_currency = WC_Aelia_CurrencySwitcher::settings()->base_currency();

        foreach( $enabled_currencies as $currency ) {
            if( $currency == $base_currency ) {
                continue;
            }

            $new_columns[] = array(
                'name' => __('Regular Price', 'woocommerce') . ' (' . $currency . ')',
                'type' => 'currency',
                'table' => 'meta',
                'field' => WC_Aelia_CurrencyPrices_Manager::FIELD_REGULAR_CURRENCY_PRICES . "___$currency",
                'readonly' => 'false',
                'visibility' => 'both',
                'sortable' => 'true',
                'views' => array( 'all', 'standard' ),
            );

            $new_columns[] = array(
                'name' => __('Sale Price', 'woocommerce') . ' (' . $currency . ')',
                'type' => 'currency',
                'table' => 'meta',
                'field' => WC_Aelia_CurrencyPrices_Manager::FIELD_SALE_CURRENCY_PRICES . "___$currency",
                'readonly' => 'false',
                'visibility' => 'both',
                'sortable' => 'true',
                'views' => array( 'all', 'standard' ),
            );
        }

        $insert_index = count( $columns );
        for ( $x = 0; $x < count( $columns ); $x++ ) {
            if ( $columns[ $x ]['name'] === $insert_after_column ) {
                $insert_index = $x + 1;
                break;
            }
        }

        array_splice( $columns, $insert_index, 0, $new_columns );

        return $columns;
    }

    function results_decode_array( $array_value, $field, $pwbe_product, $array_name ) {
        if ( strpos( $field, WC_Aelia_CurrencyPrices_Manager::FIELD_REGULAR_CURRENCY_PRICES ) !== false || strpos( $field, WC_Aelia_CurrencyPrices_Manager::FIELD_SALE_CURRENCY_PRICES ) !== false ) {
            // Handle JSON data.
            if ( !is_array( $array_value ) ) {
                $json_value = json_decode( $array_value, true);
                if ( json_last_error() == JSON_ERROR_NONE ) {
                    $array_value = $json_value;
                }
            }
        }

        return $array_value;
    }

    function before_save_array_value( $stored_data, $meta_key, $field ) {
        if ( strpos( $meta_key, WC_Aelia_CurrencyPrices_Manager::FIELD_REGULAR_CURRENCY_PRICES ) !== false || strpos( $meta_key, WC_Aelia_CurrencyPrices_Manager::FIELD_SALE_CURRENCY_PRICES ) !== false ) {
            if ( !empty( $stored_data ) ) {
                // Handle JSON data.
                $json_value = json_decode( $stored_data, true);
                if ( json_last_error() == JSON_ERROR_NONE ) {
                    $stored_data = $json_value;
                }
            }
        }

        return $stored_data;
    }

    function save_array_value( $array_value, $meta_key, $field ) {
        if ( strpos( $meta_key, WC_Aelia_CurrencyPrices_Manager::FIELD_REGULAR_CURRENCY_PRICES ) !== false || strpos( $meta_key, WC_Aelia_CurrencyPrices_Manager::FIELD_SALE_CURRENCY_PRICES ) !== false ) {
            $array_value = json_encode( $array_value );
        }

        return $array_value;
    }
}

new PWBE_WC_Aelia_CurrencySwitcher();

endif;
