<?php
/*
 * Developed By Devozon
 *
 * Check Out at https://devozon.com/
 * Email: support@devozon.com
 * Message: https://www.facebook.com/devozon
 *
 * Please feel free to contact me at any time if you need a custom script or any development work.
 *
 * Devozon Team:
 * Coded by Sanad Nabil
 * Theme by Ahmed Reda
 * Tested By Smith Carter
 *
 * Devozon All rights reserved
 * Copyright (c) 2023-2024.
 * Last Update 15 \ 1 \ 2024
 */

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

add_action( 'wp_ajax_nopriv_FENSE_BPVT_get_Fense_data', 'FENSE_BPVT_get_Fense_data' );
add_action( 'wp_ajax_FENSE_BPVT_get_Fense_data', 'FENSE_BPVT_get_Fense_data' );

function FENSE_BPVT_get_Fense_data()
{
	$data = ( new FENSE_BPVT_DATA_LOAD() )->run();
	return wp_send_json_success( $data );
}

class FENSE_BPVT_DATA_LOAD
{
	public FENSE_BPVT_FUN $Fense_FUN;
	public array $data;
	public int $AddMinutes = 15;
	public DateTime $time;
	public array $flags;
	public $dbData;
	public $dbTime;
	public $STATS;
	public string $KEY = FENSE_BPVT_PLUGIN::DB_KEY;
	
	public function __construct()
	{
		$this->Fense_FUN = new FENSE_BPVT_FUN();
		$this->time = new DateTime();
		
		/**  Fill Data Types */
		$this->data = $this->__AssignDataTypes();
	}
	
	public function run(): array
	{
		/**  Call The Data */
		$this->grabData();
		
		/**  Call The Data */
		$this->generate();
		
		return $this->data;
	}
	
	public function grabData()
	{
		$this->dbData = FENSE_BPVT_Cache::wp_get( $this->KEY . 'api_data' );
		$this->dbTime = FENSE_BPVT_Cache::wp_get( $this->KEY . 'api_data_time' );
		
		if ( empty( $this->dbData ) || $this->dbTime < $this->dbData ) return $this->__grabDataFromApi();
		
		$this->STATS = json_decode( $this->dbData );
		return $this->STATS;
	}
	
	public function generate()
	{
		/**  Load Flags */
		$this->__flags();
		
		/**  Generate History Html Code */
		$this->__generateHistory();
		
		/**  Load */
		$this->__generateCountries();
		
		/**  Load */
		$this->__generateChart();
		
		/**  Load */
		$this->__generateAccountInfo();
	}
	
	public function __AssignDataTypes(): array
	{
		return [
			'history' => '',
			'countries' => '',
			'chart' => [
				'day' => [],
				'day_count' => [],
				'clean' => [],
				'proxy' => [],
				'vpn' => [],
				'tor' => [],
			],
			'day' => [],
			'day_count' => '',
			'clean' => '',
			'proxy' => '',
			'vpn' => '',
			'tor' => '',
			'stats' => '',
		];
	}
	
	public function __timeNow(): string
	{
		return $this->time->format( 'YmdHi' );
	}
	
	public function __timePlus15(): string
	{
		$this->time->add( new DateInterval( 'PT' . $this->AddMinutes . 'M' ) );
		return $this->time->format( 'YmdHi' );
	}
	
	public function __grabDataFromApi()
	{
		
		$API = wp_remote_retrieve_body( wp_remote_get( esc_url( FENSE_BPVT_PLUGIN::SITE ) . 'wp/' . get_option( FENSE_BPVT_PLUGIN::DB_KEY . 'licence_key' ) ) );
		
		$this->STATS = json_decode( $API );
		
		if ( $this->STATS->status == 'success' ) {
			FENSE_BPVT_Cache::wp_set( FENSE_BPVT_PLUGIN::DB_KEY . 'api_data', $API, HOUR_IN_SECONDS / 4 );
			FENSE_BPVT_Cache::wp_set( FENSE_BPVT_PLUGIN::DB_KEY . 'api_data_time', $this->__timePlus15(), HOUR_IN_SECONDS / 4 );
		}
		
		return $this->STATS;
	}
	
	public function __flags(): void
	{
		$found = FENSE_BPVT_PLUGIN::SITE . 'flags/4x3/';
		$notfound = FENSE_BPVT_PLUGIN::SITE . 'flags/4x3/xx.svg';
		$svg = '.svg';
		
		$this->flags = [ 'found' => $found, 'notfound' => $notfound, 'svg' => $svg ];
	}
	
	public function __generateHistory(): void
	{
		
		foreach ( $this->STATS->history as $dat ) {
			
			$coimage = ( !empty( $dat->country ) ) ? $this->flags['found'] . strtolower( esc_html( $dat->country ) ) . $this->flags['svg'] : $this->flags['notfound'] . $this->Fense_FUN->get_country_by_code( esc_html( $dat->country ) );
			
			$this->data['history'] .= '<tr>
            <td></td>
            <td> ' . $this->Fense_FUN->timeAgo( esc_html( $dat->created_at ) ) . '</td>
            <td> ' . esc_html( $dat->ip ) . '</td >
            <td><img class="avatar-md rounded-5" src = "' . $coimage . '" alt = "stats" >
		' . $this->Fense_FUN->get_country_by_code( esc_html( $dat->country ) ) . '
            </td>
            <td> ' . $this->Fense_FUN->stats_ip_type( (int)$dat->reason ) . '</td >
        </tr > ';
		}
	}
	
	public function __generateCountries(): void
	{
		
		foreach ( $this->STATS->countries as $dat ) {
			
			$this->data['countries'] .= '<tr>
            <td ></td >
            <td >
                <img class="avatar-md rounded-5" src = "' . FENSE_BPVT_PLUGIN::SITE . 'flags/4x3/' . strtolower( esc_html( $dat->country ) ) . $this->flags['svg'] . '" alt = "stats" >
		' . $this->Fense_FUN->get_country_by_code( esc_html( $dat->country ) ) . '
            </td >
            <td > ' . $this->Fense_FUN->visit_format( (int)$dat->total ) . '</td >
            <td > ' . $this->Fense_FUN->visit_format( (int)$dat->proxy ) . '</td >
            <td > ' . $this->Fense_FUN->visit_format( (int)$dat->vpn ) . '</td >
            <td > ' . $this->Fense_FUN->visit_format( (int)$dat->tor ) . '</td >
        </tr > ';
		}
	}
	
	public function __generateChart(): void
	{
		foreach ( $this->STATS->chart as $chart ) {
			$this->data['chart']['day'][] = esc_attr( $chart->day );
			$this->data['chart']['day_count'][] = empty( $chart->Day_count ) ? 0 : (int)$chart->Day_count;
			$this->data['chart']['clean'][] = empty( $chart->clean ) ? 0 : (int)$chart->clean;
			$this->data['chart']['proxy'][] = empty( $chart->proxy ) ? 0 : (int)$chart->proxy;
			$this->data['chart']['vpn'][] = empty( $chart->vpn ) ? 0 : (int)$chart->vpn;
			$this->data['chart']['tor'][] = empty( $chart->tor ) ? 0 : (int)$chart->tor;
		}
	}
	
	public function __generateAccountInfo(): void
	{
		$this->data['today'] = $this->STATS->today;
		$this->data['last7days'] = $this->STATS->last7days;
		$this->data['thismonth'] = $this->STATS->thismonth;
		$this->data['user'] = $this->STATS->user;
		$this->data['subscription_date'] = $this->STATS->subscription_date;
		$this->data['plan'] = $this->STATS->plan;
		$this->data['perday'] = $this->STATS->perday;
		$this->data['status'] = $this->STATS->status;
	}
	
}
