<?php

namespace Clonable\Controllers;

use Clonable\Helpers\Functions;
use Clonable\Helpers\Json;
use Clonable\Helpers\MultiCurrency;
use Clonable\Models\ClonableWooCommerce;
use Clonable\Traits\Validation;
use Clonable\Views\WoocommerceView;

/**
 * Controller for actions in the analytics view.
 */
class WoocommerceController extends Controller
{
    use Validation;
    //<editor-fold desc="Singleton pattern">
    private static $instance = null;

    private function __construct()
    {
        // Keep constructor private
        $this->view = new WoocommerceView();
    }

    public static function get_instance()
    {
        if (self::$instance == null) {
            self::$instance = new WoocommerceController();
        }
        return self::$instance;
    }
    //</editor-fold>

    public function validate($request)
    {
        $this->validate_fields($request, ClonableWooCommerce::$fields);
    }

    public function clonable_woocommerce_analytics_validate($input)
    {
        if (empty($input)) {
            return $input;
        }
        $old_input = get_option('clonable_woocommerce_allowed_origins');

        $json_input = Json::handle_input($input);
        foreach ($json_input as $location) {
            $split_location = explode('/', $location, 2);
            $domain = $split_location[0];

            // Validate each domain name
            if (!filter_var($domain, FILTER_VALIDATE_DOMAIN, ['flags' => FILTER_FLAG_HOSTNAME])) {
                add_settings_error('clonable_woocommerce_allowed_origins', 'err_invalid_domain', "The domain " . esc_html($domain) . " is invalid. Enter just the domain, without http://.");
                return $old_input;
            }

            // Make sure the location does not end in a /
            if (Functions::str_ends_with($location, '/')) {
                add_settings_error('clonable_woocommerce_allowed_origins', 'err_invalid_end', "The location " . esc_html($location) . "  should not end with a /");
                return $old_input;
            }
        }


        if (empty($json_input)) {
            return null;
        }
        return Json::handle_output(json_encode($json_input));
    }

    public function clonable_woocommerce_analytics_enabled_validate($input)
    {
        return $this->validate_checkbox($input, "clonable_woocommerce_analytics_enabled");
    }

    public function clonable_woocommerce_exclusions_enabled_validate($input)
    {
        return $this->validate_checkbox($input, "clonable_product_exclusions_enabled");
    }

    public function clonable_woocommerce_module_enabled_validate($input)
    {
        return $this->validate_checkbox($input, "clonable_woocommerce_module_enabled");
    }

    public function clonable_default_country_by_domain_validate($input)
    {

        if (empty($input)) {
            return $input;
        }

        $old_input = get_option('clonable_default_country_by_domain');
        $rows = Json::handle_input($input);

        if (!is_array($rows)) {
            add_settings_error('clonable_default_country_by_domain', 'err_invalid_input', 'Invalid input format.');
            return $old_input;
        }

        foreach ($rows as $row) {
            if (empty($row->domain)) {
                add_settings_error('clonable_default_country_by_domain', 'err_empty_domain', 'Domain cannot be empty.');
                return $old_input;
            }

            $domain_part = preg_replace('/^https?:\/\//', '', $row->domain);
            $domain_part = explode('/', $domain_part, 2)[0];

            if (!filter_var($domain_part, FILTER_VALIDATE_DOMAIN, ['flags' => FILTER_FLAG_HOSTNAME])) {
                add_settings_error(
                    'clonable_default_country_by_domain',
                    'err_invalid_domain',
                    "Het domein " . esc_html($row->domain) . " is niet valide."
                );
                return $old_input;
            }

            if (!empty($row->billing_country) && !preg_match('/^[A-Z]{2}$/', $row->billing_country)) {
                add_settings_error('clonable_default_country_by_domain', 'err_invalid_billing_country', "Landcode " . esc_html($row->billing_country) . " is niet valide. Gebruik een 2-letter ISO landcode.");
                return $old_input;
            }

            if (!empty($row->shipping_country) && !preg_match('/^[A-Z]{2}$/', $row->shipping_country)) {
                add_settings_error('clonable_default_country_by_domain', 'err_invalid_shipping_country', "De verzendlandcode " . esc_html($row->shipping_country) . " is niet valide. Gebruik een 2-letter ISO landcode.");
                return $old_input;
            }
        }

        return Json::handle_output(json_encode($rows));
    }
}
