<?php
/**
 * Elite Notification Dashboard Module
 *
 * @package Elite_Notification_Lite\Modules
 */

namespace Elite_Notification_Lite\Modules;

use Appsbd_Lite\V4\libs\Ajax_Confirm_Response;
use Elite_Notification_Lite\Core\Elite_Notification_Module_Lite;
use Elite_Notification_Lite\Models\Database\Mapbd_Dummy_Notification;

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

/**
 * Class Elite_Notification_Dashboard
 *
 * @package Elite_Notification_Lite\Modules
 */
class Elite_Notification_Dashboard extends Elite_Notification_Module_Lite {
	/**
	 * The initialize is generated by appsbd
	 */
	public function initialize() {
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		$this->add_admin_ajax_action( 'get-dashboard-data', array( $this, 'get_dashboard_data' ) );
	}

	/**
	 * The get dashboard data is generated by appsbd
	 */
	public function get_dashboard_data() {
		$response                    = new Ajax_Confirm_Response();
		$response_data               = new \stdClass();
		$response_data->total_views  = Elite_Notification_Source::get_module_option( 'global_view', 0 );
		$response_data->total_clicks = Elite_Notification_Source::get_module_option( 'global_click', 0 );
		$response_data->click_rates  = 0;
		if ( $response_data->total_views <= $response_data->total_clicks ) {
			$response_data->click_rates = 100;
		} else {
			$response_data->click_rates = sprintf( '%.2f', ( ( $response_data->total_clicks / $response_data->total_views ) * 100 ) );
		}
		$all_sources                  = Elite_Notification_Source::get_all_sources_data();
		$response_data->active_source = 0;
		foreach ( $all_sources as $source ) {
			if ( 'A' == $source->status ) {
				++$response_data->active_source;
			}
		}
		$response_data->number_of_notification = count( Elite_Notification_Settings::get_module_instance()->get_notifications( false ) );
		$response_data->dummy_active           = 0;
		if ( Elite_Notification_Settings::get_module_option( 'dummy_enable', 'I' ) == 'A' ) {
			$dobj = new Mapbd_Dummy_Notification();
			$dobj->status( 'A' );
			$response_data->dummy_active = $dobj->count_all();
		}
		$response_data->total_views  = apbd_convert_number_to_k( $response_data->total_views );
		$response_data->total_clicks = apbd_convert_number_to_k( $response_data->total_clicks );
		$response->display_with_response( true, $response_data );
	}
}
