<?php

class AiImageField {

	static function show() {
		?>
        <h3>Ai Image</h3>
        <div>
            <div id="image-container"></div>
            <div id="loading-icon" class="loader"></div>
            <div id="fullpage" onclick="this.style.display='none';"></div>
            <textarea id="promt-field-id" class="textarea-metabox" style="margin-top: 20px;"
                      placeholder="<?php _e("Enter your promt", "a2m")?>"></textarea>
            <div class="ai-image-buttons">
                <button id="gen-button-id" type="button">
                    <?php _e("Generate", "a2m")?>
                </button>
                <button id="add-image-to-post-button" disabled type="button" onclick="this.disabled = true">
                    <?php _e("Add", "a2m")?>
                </button>
            </div>
        </div>
        <?php
	}

    static function gen_button_handler() {
        $promt_from_user = $_POST["promt-field-name"];
        $image_url = Ai2MediaService::get_image($promt_from_user);

        $response = array(
                "image_url" => $image_url
//                "image_url" => "https://i.ibb.co/ynp5ZvV/2024-01-12-08-17-07-PM.png" // картинка для тестов
        );

        wp_send_json($response);
    }
}

add_action('wp_ajax_gen_button_action', [AiImageField::class, 'gen_button_handler']);
add_action( "admin_post", [AiImageField::class, "generate_image_button_handler"] );
add_action('wp_ajax_gen_button_action', [AiImageField::class, 'gen_button_handler']);
