<?php

/**
 * Online Solution Sync Images.
 *
 * @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\Abstracts\Hub_API\Abstract_API_Base;
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_Image_Model;
defined('ABSPATH') || exit;
/**
 * Class Online_Solutions_Sync_Images
 */
readonly class Online_Solution_Sync_Images
{
    /**
     * Load site images from DB.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to populate.
     *
     * @used Northmill Express Order
     * @used Northmill Webshop Theme
     */
    public static function load_site_images(Abstract_Online_Solution_Settings_Model $settings_model): void
    {
        $settings_model->SiteLogotypes = array();
        $custom_logo = self::get_attachment_as_image_model((int) get_theme_mod('custom_logo', 0));
        if ($custom_logo) {
            $settings_model->SiteLogotypes[] = $custom_logo;
        }
        $settings_model->SiteFavicons = array();
        $site_icon = self::get_attachment_as_image_model((int) get_option('site_icon', 0));
        if ($site_icon) {
            $settings_model->SiteFavicons[] = $site_icon;
        }
    }
    /**
     * Store site images in DB.
     *
     * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to populate.
     * @param Abstract_API_Base                       $api API instance to use for image import.
     *
     * @used Northmill Express Order
     * @used Northmill Webshop Theme
     */
    public static function store_site_images(Abstract_Online_Solution_Settings_Model $settings_model, Abstract_API_Base $api): void
    {
        self::process_hub_image_array($settings_model->SiteLogotypes, $api);
        self::process_hub_image_array($settings_model->SiteFavicons, $api);
        $site_main_logotype = self::get_main_attachment($settings_model->SiteLogotypes);
        set_theme_mod('custom_logo', $site_main_logotype);
        $site_main_favicon = self::get_main_attachment($settings_model->SiteFavicons);
        update_option('site_icon', $site_main_favicon);
    }
    /**
     * Process an array of images from the hub.
     *
     * @param mixed             $image_array Array of images to process.
     * @param Abstract_API_Base $api API instance to use for image import.
     *
     * @used Northmill Express Order
     * @used Northmill Webshop Theme
     */
    public static function process_hub_image_array(mixed $image_array, Abstract_API_Base $api): void
    {
        if (!empty($image_array) && is_array($image_array)) {
            foreach ($image_array as $image) {
                WP_Data_Sync_Helpers::import_image($image->get_id(), $image->get_name(), $image->get_url(), $api);
            }
        }
    }
    /**
     * Get the image model for the attachment.
     * Name in Moreflo Light: get_attachment_as_apps_image()
     *
     * @param int $id WordPress Post ID.
     *
     * @return Settings_Image_Model|false
     *
     * @used Northmill Express Order
     */
    public static function get_attachment_as_image_model(int $id): Settings_Image_Model|false
    {
        if (!$id) {
            return \false;
        }
        $attachment = wp_get_attachment_metadata($id);
        if (!$attachment) {
            return \false;
        }
        return new Settings_Image_Model(array('Name' => basename($attachment['file']), 'IsMainFile' => \true, 'URL' => wp_get_attachment_image_url($id), 'WPId' => $id));
    }
    /**
     * Get the main attachment ID from a list of images.
     *
     * @param Settings_Image_Model[] $images Images.
     *
     * @return int|null Main attachment ID, or null if no main image found.
     *
     * @used Northmill Express Order
     */
    public static function get_main_attachment(array $images): ?int
    {
        $main_id = self::get_main_image($images);
        if (is_null($main_id)) {
            return null;
        }
        $posts = get_posts(array('post_type' => 'attachment', 'posts_per_page' => 1, 'ignore_sticky_posts' => \true, 'meta_query' => array(array('key' => 'mf_id', 'value' => $main_id))));
        return empty($posts) ? null : $posts[0]->ID;
    }
    /**
     * Get the main image ID from a list of images.
     *
     * @param Settings_Image_Model[] $images Images.
     *
     * @return string|null Main image ID, or null if no main image found.
     */
    private static function get_main_image(array $images): ?string
    {
        $main_id = null;
        if (!empty($images)) {
            foreach ($images as $image) {
                if ($image->is_main_file()) {
                    return $image->get_id();
                }
                $main_id = $image->get_id();
            }
        }
        return $main_id;
    }
}
