<script setup lang="ts">
import Loading from '@/components/Loading.vue';
import { inject, onMounted, ref } from 'vue';
import { api } from '../services/api';

const d = ref<any>(null);
const openGraph = inject<(key: string) => void>('openGraph');

function dir(deg: number) {
	if(deg > 22.5 && deg <= 67.5) return 'North-East';
	if(deg > 67.5 && deg <= 112.5) return 'East';
	if(deg > 112.5 && deg <= 157.5) return 'South-East';
	if(deg > 157.5 && deg <= 202.5) return 'South';
	if(deg > 202.5 && deg <= 247.5) return 'South-West';
	if(deg > 247.5 && deg <= 292.5) return 'West';
	if(deg > 292.5 && deg <= 337.5) return 'North-West';
	return 'North';
}

onMounted(async () => d.value = await api.current());
</script>

<style scoped lang="scss">
.wind-card {
	position: relative;
	border-radius: 12px;
	background: var(--surface);
	border: 1px solid var(--border);
	padding: 14px;
	display: flex;
	flex-direction: column;
	gap: 12px;
}
.card-title {
	font-size: 13px;
	font-weight: 700;
	color: var(--text);
}
.divider {
	height: 1px;
	background: var(--border);
}
.stat-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
	gap: 10px;
}
.stat {
	display: flex;
	flex-direction: column;
	gap: 2px;
	cursor: pointer;
	border-radius: 8px;
	padding: 4px 6px;
	margin: -4px -6px;
	transition: background 0.15s;
	&:hover { background: var(--hover); }
}
.stat-label {
	font-size: 10px;
	text-transform: uppercase;
	letter-spacing: 0.06em;
	color: var(--text-muted);
}
.stat-value {
	font-size: 14px;
	font-weight: 600;
	color: var(--text);
}
</style>

<template>
	<div class="wind-card" v-if="d">
		<div class="card-title">🍃 Wind</div>

		<div class="stat-grid">
			<div class="stat" @click="openGraph?.('wind_speed')">
				<span class="stat-label">Speed</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading/></div>
				<span v-else class="stat-value">{{ ~~(d.wind_speed) }} <span class="fg-muted">Kph</span></span>
			</div>
			<div class="stat" @click="openGraph?.('wind_gusts')">
				<span class="stat-label">Gusts</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading/></div>
				<span v-else class="stat-value">{{ ~~(d.wind_gusts) }} <span class="fg-muted">Kph</span></span>
			</div>
			<div class="stat">
				<span class="stat-label">Direction</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading/></div>
				<span v-else class="stat-value">{{ dir(d.wind_direction) }}</span>
			</div>
		</div>
	</div>
</template>
