<?php

namespace SovereignStack\SecuritySafe;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

require_once( SECSAFE_DIR_ADMIN_TABLES . '/Table.php' );

/**
 * Class TableLogins
 *
 * @package SecuritySafe
 */
final class TableLogins extends Table {

	/**
	 * Get a list of columns. The format is:
	 * 'internal-name' => 'Title'
	 *
	 * @package WordPress
	 * @since   3.1.0
	 * @abstract
	 *
	 * @return array
	 */
	function get_columns() {

		return [
			'date' => __( 'Date', 'security-safe' ),
			'username' => __( 'Username', 'security-safe' ),
			'ip' => __( 'IP Address', 'security-safe' ),
			'user_agent' => __( 'User Agent', 'security-safe' ),
			'status' => __( 'Status', 'security-safe' ),
			'threats' => __( 'Threat', 'security-safe' ),
			'details' => __( 'Details', 'security-safe' ),
		];
	}

	public function display_charts() {

		if( $this->hide_charts() ) {
			return;
		}

		$days = 30;
		$days_ago = $days - 1;

		echo '
    <div class="table">
        <div class="tr">

            <div class="chart chart-logins-line td td-9 center">

                <h3>' . esc_html( sprintf( __( 'Login Attempts Over The Past %d Days', 'security-safe' ), $days ) ) . '</h3>
                <div id="chart-line"></div>

            </div><div class="chart chart-logins-pie td td-3 center">

                <h3>' . esc_html__( 'Login Distribution', 'security-safe' ) . '</h3>
                <div id="chart-pie"></div>

            </div>

        </div>
    </div>';

		$charts = [];

		$columns = [
			[
				'id' => 'total',
				'label' => __( 'Total', 'security-safe' ),
				'color' => '#aaaaaa',
				'type' => 'area-spline',
				'db' => 'logins',

			],
			[
				'id' => 'threats',
				'label' => __( 'Threats', 'security-safe' ),
				'color' => '#f6c600',
				'type' => 'bar',
				'db' => 'logins_threats',
			],
			[
				'id' => 'blocked',
				'label' => __( 'Blocked', 'security-safe' ),
				'color' => '#0073aa',
				'type' => 'bar',
				'db' => 'logins_blocked',
			],
			[
				'id' => 'failed',
				'label' => __( 'Failed', 'security-safe' ),
				'color' => '#dc3232',
				'type' => 'bar',
				'db' => 'logins_failed',
			],
			[
				'id' => 'success',
				'label' => __( 'Success', 'security-safe' ),
				'color' => '#029e45',
				'type' => 'bar',
				'db' => 'logins_success',
			],
		];

		$charts[] = [
			'id' => 'chart-line',
			'type' => 'line',
			'columns' => $columns,
			'y-label' => __( '# Login Attempts', 'security-safe' ),
		];

		// Remove unused columns total, threats
		unset( $columns[0], $columns[1] );

		$charts[] = [
			'id' => 'chart-pie',
			'type' => 'pie',
			'columns' => $columns,
		];

		$args = [
			'date_start' => date( 'Y-m-d 00:00:00', strtotime( '-' . $days_ago . ' days' ) ),
			'date_end' => date( 'Y-m-d 23:59:59', time() ),
			'date_days' => $days,
			'date_days_ago' => $days_ago,
			'charts' => $charts,
		];

		// Load Charts
		Admin::load_charts( $args );
	}

	/**
	 * Set the type of data to display
	 *
	 * @since  2.0.0
	 */
	protected function set_type() {

		$this->type = 'logins';
	}

	/**
	 * Get the array of searchable columns in the database
	 *
	 * @since  2.0.0
	 * @return  array An unassociated array.
	 */
	protected function get_searchable_columns() {

		return [
			'uri',
			'ip',
			'username',
		];
	}

	protected function get_status() {

		return [
			//  'key'       => 'label'
			'success' => __( 'Success', 'security-safe' ),
			'failed' => __( 'Failed', 'security-safe' ),
			'blocked' => __( 'Blocked', 'security-safe' ),
		];
	}

	/**
	 * Add filters and per_page options
	 */
	protected function bulk_actions( $which = '' ) {

		$this->bulk_actions_load( $which );
	}
}
