<?php

namespace Pixalia\ImageAssistant\Core;

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

use Pixalia\ImageAssistant\Admin\Page\EyecatchCheck;
use Pixalia\ImageAssistant\Admin\Page\Settings;
use Pixalia\ImageAssistant\Admin\View\Notice;
use Pixalia\ImageAssistant\Admin\View\PostEdit;
use Pixalia\ImageAssistant\Admin\View\PostList;
use Pixalia\ImageAssistant\Admin\View\PostListStatusTabs;
use Pixalia\ImageAssistant\Base\Const\PixaliaConst;
use Pixalia\ImageAssistant\Admin\Menu;
use Pixalia\ImageAssistant\Admin\Assets as AdminAssets;
use Pixalia\ImageAssistant\Api\PixaliaClient;
use Pixalia\ImageAssistant\Api\AjaxHandler;
use Pixalia\ImageAssistant\Repository\OptionRepository;
use Pixalia\ImageAssistant\Repository\PostRepository;
use Pixalia\ImageAssistant\Service\ImageChecker;
use Pixalia\ImageAssistant\Service\ImageAttacher;
use Pixalia\ImageAssistant\Service\MediaUploader;

/**
 * Initiator Class
 */
class Initiator {

	/**
	 * コンストラクタ
	 */
	public function __construct() {
		$this->init();
	}

	/**
	 * プラグインの各機能を初期化し、フックに登録します。
	 */
	public function init() {
		//Internationalization
		$i18n = new Internationalization( 'pixalia-image-assistant' );

		//Util
		$checker  = new ImageChecker();
		$attacher = new ImageAttacher();

		//Repository
		$postRepository   = new PostRepository();
		$optionRepository = new OptionRepository();

		//Service
		$mediaUploader = new MediaUploader();

		//API
		$apiClient = new PixaliaClient( $optionRepository );

		//Admin
		$adminAssets = new AdminAssets();

		// --- ページの描画担当クラスを先にインスタンス化 ---
		$eyecatchCheck = new EyecatchCheck();
		$settingsPage  = new Settings();

		// --- Menuクラスに、描画担当クラスを渡してインスタンス化 ---
		$menu = new Menu( $eyecatchCheck, $settingsPage );

		$postList           = new PostList(); // 依存性を注入
		$postEdit           = new PostEdit();
		$adminNotice        = new Notice();
		$postListStatusTabs = new PostListStatusTabs();

		$ajaxHandler = new AjaxHandler(
			$apiClient,
			$attacher,
			$checker,
			$postRepository,
			$optionRepository,
			$mediaUploader
		);

		// 管理画面関連のフック
		// ===================================
		add_action( 'admin_init', [ $settingsPage, 'register_settings' ] );
		add_action( 'admin_menu', [ $menu, 'register_pages' ] );
		add_action( 'admin_enqueue_scripts', [ $adminAssets, 'enqueue' ] );
		// 一括チェックを促す警告の表示
		add_action( 'admin_notices', [ $adminNotice, 'show_bulk_check_notice' ] );

		// ===================================
		// 投稿一覧画面のフック
		// ===================================
		add_filter( 'manage_posts_columns', [ $postList, 'add_columns' ] );
		add_action( 'manage_posts_custom_column', [ $postList, 'render_custom_column' ], 10, 2 );
		add_action( 'restrict_manage_posts', [ $postList, 'add_filter_dropdown' ] );
		add_action( 'parse_query', [ $postList, 'modify_query' ] );

		// ===================================
		// 投稿編集画面のフック
		// ===================================
		add_action( 'add_meta_boxes', [ $postEdit, 'add' ] );
		// 投稿保存時にImageCheckerでステータスをチェック＆保存
		add_action( 'save_post', [ $checker, 'check_and_save_status_on_save' ], 10, 3 );

		// ===================================
		// AJAX関連のフック
		// ===================================
		$ajaxHandler->register_hooks();
		// OGPデータ取得用のAJAXアクションを登録
		add_action( 'wp_ajax_pixalia_get_ogp_data', [ $ajaxHandler, 'handleGetOgpData' ] );
	}
}
