<?php

/**
 * Abstract online solution settings sync for webshop/catering.
 *
 * @package northmill-online-shared
 *
 * @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Data\Payment_Methods;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\WP_Data_Sync_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Abstract_Online_Solution_Settings_Model;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties\Settings_Color_Model;
defined('ABSPATH') || exit;
/**
 * Abstract_Online_Solution_Webshop_Settings_Sync class.
 *
 * @used Northmill Webshop Theme
 * @used Northmill Catering Theme
 */
abstract class Abstract_Online_Solution_Webshop_Settings_Sync extends Abstract_Online_Solution_Settings_Sync
{
    /**
     * {@inheritDoc}
     */
    protected function process_base_data_from_hub(object $hub_data): array
    {
        return array_merge(parent::process_base_data_from_hub($hub_data), array('SitePublic' => $hub_data->SitePublic, 'EnableAccountCreation' => $hub_data->EnableAccountCreation, 'ColorGeneralBackground' => isset($hub_data->ColorGeneralBackground) ? new Settings_Color_Model($hub_data->ColorGeneralBackground) : null, 'ColorHeaderBackground' => isset($hub_data->ColorHeaderBackground) ? new Settings_Color_Model($hub_data->ColorHeaderBackground) : null, 'ColorHeaderText' => isset($hub_data->ColorHeaderText) ? new Settings_Color_Model($hub_data->ColorHeaderText) : null, 'ColorHeaderLink' => isset($hub_data->ColorHeaderLink) ? new Settings_Color_Model($hub_data->ColorHeaderLink) : null, 'ColorFooterBackground' => isset($hub_data->ColorFooterBackground) ? new Settings_Color_Model($hub_data->ColorFooterBackground) : null, 'ColorFooterHeading' => isset($hub_data->ColorFooterHeading) ? new Settings_Color_Model($hub_data->ColorFooterHeading) : null, 'ColorFooterText' => isset($hub_data->ColorFooterText) ? new Settings_Color_Model($hub_data->ColorFooterText) : null, 'ColorFooterLink' => isset($hub_data->ColorFooterLink) ? new Settings_Color_Model($hub_data->ColorFooterLink) : null, 'ColorTypographyHeading' => isset($hub_data->ColorTypographyHeading) ? new Settings_Color_Model($hub_data->ColorTypographyHeading) : null, 'ColorTypographyText' => isset($hub_data->ColorTypographyText) ? new Settings_Color_Model($hub_data->ColorTypographyText) : null, 'ColorTypographyLink' => isset($hub_data->ColorTypographyLink) ? new Settings_Color_Model($hub_data->ColorTypographyLink) : null, 'ColorButtonBackground' => isset($hub_data->ColorButtonBackground) ? new Settings_Color_Model($hub_data->ColorButtonBackground) : null, 'ColorButtonText' => isset($hub_data->ColorButtonText) ? new Settings_Color_Model($hub_data->ColorButtonText) : null, 'ColorButtonAltBackground' => isset($hub_data->ColorButtonAltBackground) ? new Settings_Color_Model($hub_data->ColorButtonAltBackground) : null, 'ColorButtonAltText' => isset($hub_data->ColorButtonAltText) ? new Settings_Color_Model($hub_data->ColorButtonAltText) : null, 'ContentAboutUs' => $hub_data->ContentAboutUs, 'ContentContactUs' => $hub_data->ContentContactUs, 'ContentTermsOfPurchase' => $hub_data->ContentTermsOfPurchase, 'ContentPrivacyPolicy' => $hub_data->ContentPrivacyPolicy));
    }
    /**
     * {@inheritDoc}
     */
    public function get_settings_model(): Abstract_Online_Solution_Settings_Model
    {
        $settings_model = parent::get_settings_model();
        $settings_model->SitePublic = (bool) get_option('blog_public', \false);
        $settings_model->EnableAccountCreation = 'yes' === get_option('woocommerce_enable_signup_and_login_from_checkout', 'no');
        $theme_mods = get_theme_mods();
        $settings_model->ColorGeneralBackground = isset($theme_mods['background_color']) ? new Settings_Color_Model('#' . $theme_mods['background_color']) : null;
        $settings_model->ColorHeaderBackground = isset($theme_mods['storefront_header_background_color']) ? new Settings_Color_Model($theme_mods['storefront_header_background_color']) : null;
        $settings_model->ColorHeaderText = isset($theme_mods['storefront_header_text_color']) ? new Settings_Color_Model($theme_mods['storefront_header_text_color']) : null;
        $settings_model->ColorHeaderLink = isset($theme_mods['storefront_header_link_color']) ? new Settings_Color_Model($theme_mods['storefront_header_link_color']) : null;
        $settings_model->ColorFooterBackground = isset($theme_mods['storefront_footer_background_color']) ? new Settings_Color_Model($theme_mods['storefront_footer_background_color']) : null;
        $settings_model->ColorFooterHeading = isset($theme_mods['storefront_footer_heading_color']) ? new Settings_Color_Model($theme_mods['storefront_footer_heading_color']) : null;
        $settings_model->ColorFooterText = isset($theme_mods['storefront_footer_text_color']) ? new Settings_Color_Model($theme_mods['storefront_footer_text_color']) : null;
        $settings_model->ColorFooterLink = isset($theme_mods['storefront_footer_link_color']) ? new Settings_Color_Model($theme_mods['storefront_footer_link_color']) : null;
        $settings_model->ColorTypographyHeading = isset($theme_mods['storefront_heading_color']) ? new Settings_Color_Model($theme_mods['storefront_heading_color']) : null;
        $settings_model->ColorTypographyText = isset($theme_mods['storefront_text_color']) ? new Settings_Color_Model($theme_mods['storefront_text_color']) : null;
        $settings_model->ColorTypographyLink = isset($theme_mods['storefront_accent_color']) ? new Settings_Color_Model($theme_mods['storefront_accent_color']) : null;
        $settings_model->ColorButtonBackground = isset($theme_mods['storefront_button_background_color']) ? new Settings_Color_Model($theme_mods['storefront_button_background_color']) : null;
        $settings_model->ColorButtonText = isset($theme_mods['storefront_button_text_color']) ? new Settings_Color_Model($theme_mods['storefront_button_text_color']) : null;
        $settings_model->ColorButtonAltBackground = isset($theme_mods['storefront_button_alt_background_color']) ? new Settings_Color_Model($theme_mods['storefront_button_alt_background_color']) : null;
        $settings_model->ColorButtonAltText = isset($theme_mods['storefront_button_alt_text_color']) ? new Settings_Color_Model($theme_mods['storefront_button_alt_text_color']) : null;
        $content_about_us_id = WP_Data_Sync_Helpers::get_post_id('ContentAboutUs', 'page');
        $content_contact_us_id = WP_Data_Sync_Helpers::get_post_id('ContentContactUs', 'page');
        $content_terms_id = WP_Data_Sync_Helpers::get_post_id('ContentTermsOfPurchase', 'page');
        $content_privacy_policy_id = WP_Data_Sync_Helpers::get_post_id('ContentPrivacyPolicy', 'page');
        $settings_model->ContentAboutUs = $content_about_us_id ? WP_Data_Sync_Helpers::get_post_content($content_about_us_id) : '';
        $settings_model->ContentContactUs = $content_contact_us_id ? WP_Data_Sync_Helpers::get_post_content($content_contact_us_id) : '';
        $settings_model->ContentTermsOfPurchase = $content_terms_id ? WP_Data_Sync_Helpers::get_post_content($content_terms_id) : '';
        $settings_model->ContentPrivacyPolicy = $content_privacy_policy_id ? WP_Data_Sync_Helpers::get_post_content($content_privacy_policy_id) : '';
        return $settings_model;
    }
    /**
     * {@inheritDoc}
     */
    protected function update_settings_from_hub_inner(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        parent::update_settings_from_hub_inner($settings_model);
        WP_Data_Sync_Helpers::update_post('ContentAboutUs', __('About us', 'northmill-online-shared'), 'page', $settings_model->ContentAboutUs);
        WP_Data_Sync_Helpers::update_post('ContentContactUs', __('Contact us', 'northmill-online-shared'), 'page', $settings_model->ContentContactUs);
        WP_Data_Sync_Helpers::update_post('ContentTermsOfPurchase', __('Terms of purchase', 'northmill-online-shared'), 'page', $settings_model->ContentTermsOfPurchase);
        WP_Data_Sync_Helpers::update_post('ContentPrivacyPolicy', __('Privacy policy', 'northmill-online-shared'), 'page', $settings_model->ContentPrivacyPolicy);
        update_option('blog_public', $settings_model->SitePublic);
        update_option('woocommerce_enable_signup_and_login_from_checkout', $settings_model->EnableAccountCreation ? 'yes' : 'no');
        update_option('woocommerce_enable_myaccount_registration', $settings_model->EnableAccountCreation ? 'yes' : 'no');
        set_theme_mod('background_color', $settings_model->ColorGeneralBackground->get_color(\false));
        set_theme_mod('storefront_header_background_color', $settings_model->ColorHeaderBackground->get_color());
        set_theme_mod('storefront_header_text_color', $settings_model->ColorHeaderText->get_color());
        set_theme_mod('storefront_header_link_color', $settings_model->ColorHeaderLink->get_color());
        set_theme_mod('storefront_footer_background_color', $settings_model->ColorFooterBackground->get_color());
        set_theme_mod('storefront_footer_heading_color', $settings_model->ColorFooterHeading->get_color());
        set_theme_mod('storefront_footer_text_color', $settings_model->ColorFooterText->get_color());
        set_theme_mod('storefront_footer_link_color', $settings_model->ColorFooterLink->get_color());
        set_theme_mod('storefront_heading_color', $settings_model->ColorTypographyHeading->get_color());
        set_theme_mod('storefront_text_color', $settings_model->ColorTypographyText->get_color());
        set_theme_mod('storefront_accent_color', $settings_model->ColorTypographyLink->get_color());
        set_theme_mod('storefront_button_background_color', $settings_model->ColorButtonBackground->get_color());
        set_theme_mod('storefront_button_text_color', $settings_model->ColorButtonText->get_color());
        set_theme_mod('storefront_button_alt_background_color', $settings_model->ColorButtonAltBackground->get_color());
        set_theme_mod('storefront_button_alt_text_color', $settings_model->ColorButtonAltText->get_color());
        $nav_menu_locations = get_nav_menu_locations();
        $primary_id = $nav_menu_locations['primary'] ?? 0;
        $secondary_id = $nav_menu_locations['secondary'] ?? 0;
        $handheld_id = $nav_menu_locations['handheld'] ?? 0;
        $content_terms_id = WP_Data_Sync_Helpers::get_post_id('ContentTermsOfPurchase', 'page');
        if ($settings_model->ContentTermsOfPurchase) {
            WP_Data_Sync_Helpers::add_nav_menu_item($secondary_id, (int) $content_terms_id);
            WP_Data_Sync_Helpers::add_nav_menu_item($handheld_id, (int) $content_terms_id);
            update_option('woocommerce_terms_page_id', $content_terms_id);
        } else {
            WP_Data_Sync_Helpers::remove_nav_menu_item($secondary_id, (int) $content_terms_id);
            WP_Data_Sync_Helpers::remove_nav_menu_item($handheld_id, (int) $content_terms_id);
            update_option('woocommerce_terms_page_id', '');
        }
        $content_privacy_policy_id = WP_Data_Sync_Helpers::get_post_id('ContentPrivacyPolicy', 'page');
        if ($settings_model->ContentPrivacyPolicy) {
            WP_Data_Sync_Helpers::add_nav_menu_item($secondary_id, (int) $content_privacy_policy_id);
            WP_Data_Sync_Helpers::add_nav_menu_item($handheld_id, (int) $content_privacy_policy_id);
            update_option('wp_page_for_privacy_policy', $content_privacy_policy_id);
        } else {
            WP_Data_Sync_Helpers::remove_nav_menu_item($secondary_id, (int) $content_privacy_policy_id);
            WP_Data_Sync_Helpers::remove_nav_menu_item($handheld_id, (int) $content_privacy_policy_id);
            delete_option('wp_page_for_privacy_policy');
        }
        $wc_my_account_page_id = get_option('woocommerce_myaccount_page_id');
        if ($settings_model->EnableAccountCreation && $wc_my_account_page_id) {
            WP_Data_Sync_Helpers::add_nav_menu_item($primary_id, (int) $wc_my_account_page_id);
            WP_Data_Sync_Helpers::add_nav_menu_item($handheld_id, (int) $wc_my_account_page_id);
        } else {
            WP_Data_Sync_Helpers::remove_nav_menu_item($primary_id, (int) $wc_my_account_page_id);
            WP_Data_Sync_Helpers::remove_nav_menu_item($handheld_id, (int) $wc_my_account_page_id);
        }
        $about_us_id = WP_Data_Sync_Helpers::get_post_id('ContentAboutUs', 'page');
        if ($settings_model->ContentAboutUs && $about_us_id) {
            WP_Data_Sync_Helpers::add_nav_menu_item($primary_id, (int) $about_us_id);
            WP_Data_Sync_Helpers::add_nav_menu_item($handheld_id, (int) $about_us_id);
        } else {
            WP_Data_Sync_Helpers::remove_nav_menu_item($primary_id, (int) $about_us_id);
            WP_Data_Sync_Helpers::remove_nav_menu_item($handheld_id, (int) $about_us_id);
        }
        $contact_us_id = WP_Data_Sync_Helpers::get_post_id('ContentContactUs', 'page');
        if ($settings_model->ContentContactUs && $contact_us_id) {
            WP_Data_Sync_Helpers::add_nav_menu_item($primary_id, (int) $contact_us_id);
            WP_Data_Sync_Helpers::add_nav_menu_item($handheld_id, (int) $contact_us_id);
        } else {
            WP_Data_Sync_Helpers::remove_nav_menu_item($primary_id, (int) $contact_us_id);
            WP_Data_Sync_Helpers::remove_nav_menu_item($handheld_id, (int) $contact_us_id);
        }
        $this->fill_shop_menu_with_categories($primary_id);
        $this->fill_shop_menu_with_categories($handheld_id);
    }
    /**
     * Fill the shop menu with product categories.
     *
     * @param int $menu_id The menu ID to fill with categories.
     */
    private function fill_shop_menu_with_categories(int $menu_id): void
    {
        $shop_page_id = wc_get_page_id('shop');
        $cats = array_values(get_terms(array('taxonomy' => 'product_cat', 'parent' => 0, 'meta_key' => 'order', 'orderby' => 'meta_value_num', 'order' => 'ASC')));
        $new_cat_ids = empty($cats) ? array() : array_map(function ($a) {
            return $a->term_id;
        }, $cats);
        $menu_items = wp_get_nav_menu_items($menu_id);
        $shop_menu_id = 0;
        $current_cat_ids = array();
        foreach ($menu_items as $menu_item) {
            if ($menu_item->object_id === $shop_page_id && 'page' === $menu_item->object) {
                $shop_menu_id = $menu_item->ID;
            }
            if ($shop_menu_id === $menu_item->menu_item_parent && 'product_cat' === $menu_item->object) {
                $current_cat_ids[] = intval($menu_item->object_id);
            }
        }
        if (!$shop_menu_id) {
            return;
        }
        // Do not run update if there are no changes.
        if ($current_cat_ids === $new_cat_ids) {
            return;
        }
        // Remove old categories.
        foreach ($menu_items as $menu_item) {
            if ($shop_menu_id === $menu_item->menu_item_parent) {
                wp_delete_post($menu_item->ID);
            }
        }
        if (empty($cats)) {
            return;
        }
        // Add categories to menu.
        foreach ($cats as $cat) {
            wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => $cat->name, 'menu-item-object-id' => $cat->term_id, 'menu-item-object' => 'product_cat', 'menu-item-status' => 'publish', 'menu-item-type' => 'taxonomy', 'menu-item-parent-id' => $shop_menu_id));
        }
    }
    /**
     * {@inheritDoc}
     */
    protected function disable_online_solution(): bool
    {
        return update_option('mf_global_active', 0);
    }
    /**
     * {@inheritDoc}
     */
    public function get_valid_payment_methods(): array
    {
        return array(Payment_Methods::WC_COD, Payment_Methods::NMC_NMI, Payment_Methods::NMC_MF_INVOICE, Payment_Methods::NMC_SWISH, Payment_Methods::NMC_RESURS_INVOICE);
    }
}
