<?php

class WPML_TM_Unsent_Jobs_Notice_Hooks {
	protected $dismissed_option_key;

	private $wpml_tm_notice_email_notice;

	private $wpml_admin_notices;

	private $wp_api;

	public function __construct( WPML_TM_Unsent_Jobs_Notice $wpml_tm_notice_email_notice, WPML_WP_API $wp_api, $dismissed_option_key ) {
		$this->wpml_tm_notice_email_notice = $wpml_tm_notice_email_notice;
		$this->wpml_admin_notices          = wpml_get_admin_notices();
		$this->wp_api                      = $wp_api;
		$this->dismissed_option_key        = $dismissed_option_key;
	}

	public function add_hooks() {
		add_action( 'wpml_tm_jobs_translator_notification', array( $this, 'email_for_job' ) );
		add_action( 'wpml_tm_jobs_notification', array( $this, 'add_notice' ) );
		add_action( 'shutdown', array( $this, 'remove_notice' ) );
	}

	public function email_for_job( $args ) {
		$job_set   = array_key_exists( 'job', $args ) && $args['job'];
		$event_set = array_key_exists( 'event', $args ) && $args['event'];
		if ( $job_set && $event_set ) {
			if ( 'unsent' === $args['event'] ) {
				$this->wpml_tm_notice_email_notice->add_job( $args );
			} else {
				$this->wpml_tm_notice_email_notice->remove_job( $args );
			}
		}
	}

	public function add_notice() {
		$this->wpml_tm_notice_email_notice->add_notice( $this->wpml_admin_notices, $this->get_dismissed_option_key() );
	}

	public function remove_notice() {
		if ( $this->wp_api->is_jobs_tab() ) {
			$this->wpml_admin_notices->remove_notice( WPML_TM_Unsent_Jobs_Notice::NOTICE_GROUP_ID, WPML_TM_Unsent_Jobs_Notice::NOTICE_ID );
		}
	}

	private function get_dismissed_option_key() {
		return $this->dismissed_option_key;
	}
}
