<?php

/**
 * Online Solution Sync General Data.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Sync;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\WC_Data_Sync_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Abstract_Online_Solution_Settings_Sync;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Abstract_Online_Solution_Settings_Model;
defined('ABSPATH') || exit;
/**
 * Class Online_Solutions_Sync_Images
 */
readonly class Online_Solution_Sync_General_Data
{
    const OPTIONS_PREFIX = 'nms_online_solution_';
    /**
     * Load address data for online solution.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to sync.
     */
    public static function load_address_data(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        $settings_model->CompanyName = get_option(self::OPTIONS_PREFIX . 'company_name', null);
        $settings_model->Address1 = get_option('woocommerce_store_address', null);
        $settings_model->Address2 = get_option('woocommerce_store_address_2', null);
        $settings_model->PostCode = get_option('woocommerce_store_postcode', null);
        $settings_model->City = get_option('woocommerce_store_city', null);
        $settings_model->Country = get_option('woocommerce_default_country', null);
        $settings_model->Telephone = get_option(self::OPTIONS_PREFIX . 'phone', null);
        $settings_model->Email = get_option(self::OPTIONS_PREFIX . 'email', null);
    }
    /**
     * Store address data for online solution.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to sync.
     */
    public static function store_address_data(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        update_option(self::OPTIONS_PREFIX . 'company_name', $settings_model->CompanyName);
        update_option('blogname', $settings_model->CompanyName);
        update_option('woocommerce_email_from_name', $settings_model->CompanyName);
        update_option('woocommerce_email_footer_text', $settings_model->CompanyName);
        update_option('woocommerce_store_address', $settings_model->Address1);
        update_option('woocommerce_store_address_2', $settings_model->Address2);
        update_option('woocommerce_store_postcode', $settings_model->PostCode);
        update_option('woocommerce_store_city', $settings_model->City);
        update_option('woocommerce_default_country', 'SE', \true);
        // TODO: Allow for multiple countries.
        update_option(self::OPTIONS_PREFIX . 'phone', $settings_model->Telephone);
        update_option(self::OPTIONS_PREFIX . 'email', $settings_model->Email);
    }
    /**
     * Load content texts for online solution.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to sync.
     */
    public static function load_content_texts(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        $settings_model->ContentHomePage = get_theme_mod('mf_content_home_page', '');
        $settings_model->ContentFooterText = get_theme_mod('mf_content_footer_text', '');
    }
    /**
     * Store content texts for online solution.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to sync.
     */
    public static function store_content_texts(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        set_theme_mod('mf_content_home_page', $settings_model->ContentHomePage);
        set_theme_mod('mf_content_footer_text', $settings_model->ContentFooterText);
    }
    /**
     * Update email addresses in WooCommerce settings.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model containing email addresses.
     */
    public static function update_email_addresses(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        WC_Data_Sync_Helpers::set_wc_email('woocommerce_new_order_settings', $settings_model->Email);
        WC_Data_Sync_Helpers::set_wc_email('woocommerce_cancelled_order_settings', $settings_model->Email);
        WC_Data_Sync_Helpers::set_wc_email('woocommerce_failed_order_settings', $settings_model->Email);
        update_option('woocommerce_stock_email_recipient', $settings_model->Email);
    }
}
