<?php

namespace Fotogenic_AI;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

final class Admin {
    private static ?Admin $instance = null;

    private function __construct() {}

    public static function instance(): Admin {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function init(): void {
        add_filter( 'plugin_action_links_' . plugin_basename( FOTOGENIC_AI_FILE ), [ $this, 'add_settings_link' ] );
        add_action( 'admin_notices', [ $this, 'maybe_show_block_editor_notice' ] );
        add_filter( 'admin_footer_text', [ $this, 'filter_admin_footer_text' ], 20 );
        add_filter( 'update_footer', [ $this, 'filter_update_footer' ], 20 );
    }

    public function add_settings_link( array $links ): array {
        if ( fotogenic_ai_user_can_access() ) {
            $settings_url = fotogenic_ai_get_settings_url();
            $settings_link = sprintf(
                '<a href="%s">%s</a>',
                esc_url( $settings_url ),
                esc_html__( 'Settings', 'fotogenic-ai' )
            );
            array_unshift( $links, $settings_link );

            $studio_url = fotogenic_ai_get_studio_url( [] );
            $studio_link = sprintf(
                '<a href="%s">%s</a>',
                esc_url( $studio_url ),
                esc_html__( 'Studio', 'fotogenic-ai' )
            );
            array_unshift( $links, $studio_link );
        }

        return $links;
    }

    public function maybe_show_block_editor_notice(): void {
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        if ( ! $screen || 'product' !== $screen->post_type ) {
            return;
        }

        if ( ! is_block_product_editor_active() ) {
            return;
        }

        ?>
        <div class="notice notice-warning">
            <p><?php esc_html_e( 'Fotogenic AI supports the classic product editor only. Switch to the classic editor to use image generation.', 'fotogenic-ai' ); ?></p>
        </div>
        <?php
    }

    public static function is_classic_product_editor_screen( ?string $hook = null ): bool {
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        $hook = $hook ?? ( $screen ? $screen->base : '' );
        if ( ! in_array( $hook, [ 'post.php', 'post-new.php', 'post', 'post-new' ], true ) ) {
            return false;
        }

        $post_type = '';
        if ( $screen && ! empty( $screen->post_type ) ) {
            $post_type = $screen->post_type;
        } elseif ( isset( $GLOBALS['typenow'] ) ) {
            $post_type = (string) $GLOBALS['typenow'];
        } else {
            $post = get_post();
            if ( $post instanceof \WP_Post ) {
                $post_type = (string) $post->post_type;
            }
        }

        if ( 'product' !== $post_type ) {
            return false;
        }

        if ( is_block_product_editor_active() ) {
            return false;
        }

        return true;
    }

    public function filter_admin_footer_text( string $text ): string {
        return $this->is_plugin_admin_page() ? '' : $text;
    }

    public function filter_update_footer( string $text ): string {
        return $this->is_plugin_admin_page() ? '' : $text;
    }

    private function is_plugin_admin_page(): bool {
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        if ( ! $screen ) {
            return false;
        }

        $screen_id = (string) ( $screen->id ?? '' );
        $screen_base = (string) ( $screen->base ?? '' );
        $allowed = [
            'toplevel_page_fotogenic-studio',
            'fotogenic-ai_page_fotogenic-ai',
        ];

        return in_array( $screen_id, $allowed, true ) || in_array( $screen_base, $allowed, true );
    }
}
