<?php

namespace Waypoint\Agent\Admin;

use Waypoint\Agent\Auth\Hmac_Auth;
use Waypoint\Agent\Base\Thing;

class Dashboard extends Thing {

	public function attach_hooks(): void {
		add_action( 'wp_dashboard_setup', [ $this, 'setup' ] );
	}

	public function setup(): void {
		$this->remove_default_widgets();
		$this->add_waypoint_widget();
	}

	private function remove_default_widgets(): void {
		remove_meta_box( 'dashboard_activity',    'dashboard', 'normal' );
		remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' );
		remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
		remove_meta_box( 'dashboard_primary',     'dashboard', 'side' );
	}

	private function add_waypoint_widget(): void {
		wp_add_dashboard_widget(
			'waypoint_agent_widget',
			'Waypoint',
			[ $this, 'render' ]
		);

		// Move to the front of the normal column so it renders first.
		global $wp_meta_boxes;
		$widget = $wp_meta_boxes['dashboard']['normal']['core']['waypoint_agent_widget'] ?? null;
		if ( $widget ) {
			unset( $wp_meta_boxes['dashboard']['normal']['core']['waypoint_agent_widget'] );
			$wp_meta_boxes['dashboard']['normal']['core'] = array_merge(
				[ 'waypoint_agent_widget' => $widget ],
				$wp_meta_boxes['dashboard']['normal']['core']
			);
		}
	}

	public function render(): void {
		$key_set = null !== ( new Hmac_Auth() )->get_key();
		?>
		<style>
		#waypoint_agent_widget .inside { padding: 0; margin: 0; }

		.wa-dash {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 24px 16px 20px;
			gap: 16px;
		}
		.wa-dash__logo svg {
			width: 96px;
			height: auto;
			display: block;
		}
		.wa-dash__meta {
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 8px;
		}
		.wa-dash__version {
			font-size: 11px;
			color: #8c8f94;
			letter-spacing: .3px;
		}
		.wa-dash__status {
			display: inline-flex;
			align-items: center;
			gap: 6px;
			font-size: 11px;
			font-weight: 600;
			text-transform: uppercase;
			letter-spacing: .4px;
			padding: 3px 10px;
			border-radius: 20px;
		}
		.wa-dash__status::before {
			content: '';
			display: inline-block;
			width: 6px;
			height: 6px;
			border-radius: 50%;
		}
		.wa-dash__status--ok {
			background: #edfaef;
			color: #00a32a;
		}
		.wa-dash__status--ok::before  { background: #00a32a; }
		.wa-dash__status--warn {
			background: #fcf0f1;
			color: #d63638;
		}
		.wa-dash__status--warn::before { background: #d63638; }

		.wa-dash__footer {
			width: 100%;
			border-top: 1px solid #f0f0f1;
			padding: 10px 16px 0;
			text-align: center;
		}
		.wa-dash__footer a {
			font-size: 11px;
			color: #8c8f94;
			text-decoration: none;
		}
		.wa-dash__footer a:hover { color: #30225b; }
		</style>

		<div class="wa-dash">
			<div class="wa-dash__logo">
				<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 966.03 483.55" aria-hidden="true" focusable="false">
					<path fill="#30225b" d="M751.01,10.77c-.33,5.85-.6,22.94-.79,45.97-.51,6.49-5.85,11.6-12.48,11.6-.12,0-.31.01-.43.01h-92.89c-39.74-.25-79.65,26.66-81.72,80.69-.05,0,.08,165.44-.05,165.36-1.57,15.37-23.67,15.19-25.16,0-.8-27.41,2.74-86.84-5.07-112.92-18.21-76.7-90.22-133.85-169.92-133.13-78.83-.7-150.21,55.16-169.32,130.74-3.71,14.23-5.65,28.93-5.66,44.23v70.91c-.63,6.4-5.92,11.59-12.61,11.57-6.68.03-11.97-5.18-12.62-11.57v-164.7c0-44.81-36.37-81.17-81.16-81.17S0,104.73,0,149.53v159.07c-.05,96.54,78.39,174.96,174.94,174.95,96.55.01,175-78.41,174.96-174.95.01-4.96-.03-67.04,0-70.04.52-16.65,24.71-16.68,25.23,0,0-1.84.02,68.52,0,70.04-.03,96.54,78.41,174.96,174.96,174.95,96.56,0,174.96-78.39,174.96-174.95v-65.26c0-6.96,5.65-12.61,12.61-12.61h.08c6.58.03,11.97,5.14,12.48,11.59.18,23.06.45,40.17.79,46.03.17,3.05.76,4.79.95,5.23.9,2.08,2.17,3.16,3.07,3.81,1.26.9,3.78,2.46,7.77,1.36.7-.2,3.76-1.66,8.4-4.73,29.66-19.56,160.93-113.33,186.88-133.53,3.79-2.94,5.84-5.03,6.25-5.62,1.57-2.23,1.7-4.18,1.7-5.31s-.13-3.07-1.7-5.3c-.4-.59-2.46-2.69-6.25-5.62-25.95-20.2-157.22-113.97-186.88-133.53-4.64-3.07-7.7-4.54-8.4-4.73-3.99-1.1-6.51.46-7.77,1.35-.9.65-2.17,1.74-3.07,3.82-.18.44-.77,2.18-.95,5.23h0Z"/>
				</svg>
			</div>

			<div class="wa-dash__meta">
				<span class="wa-dash__version">v<?php echo esc_html( WAYPOINT_AGENT_VERSION ); ?></span>
				<?php if ( $key_set ) : ?>
					<span class="wa-dash__status wa-dash__status--ok">Agent Key Set</span>
				<?php else : ?>
					<span class="wa-dash__status wa-dash__status--warn">Agent Key Missing</span>
				<?php endif; ?>
			</div>

			<div class="wa-dash__footer">
				<a href="<?php echo esc_url( admin_url( 'tools.php?page=waypoint-agent' ) ); ?>">Settings</a>
			</div>
		</div>
		<?php
	}
}
