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

const data = ref<any>({});

function dir(deg: number) {
	if(deg > 22.5 && deg <= 67.5) return 'NE';
	if(deg > 67.5 && deg <= 112.5) return 'E';
	if(deg > 112.5 && deg <= 157.5) return 'SE';
	if(deg > 157.5 && deg <= 202.5) return 'S';
	if(deg > 202.5 && deg <= 247.5) return 'SW';
	if(deg > 247.5 && deg <= 292.5) return 'W';
	if(deg > 292.5 && deg <= 337.5) return 'NW';
	return 'N';
}

onMounted(async () => {
	data.value = await api.current();
	console.log(data.value);
});

</script>

<style scoped lang="scss">
.today-card {
	border-radius: 12px;
	background: var(--surface);
	border: 1px solid var(--border);
	padding: 14px;
	display: flex;
	flex-direction: column;
	gap: 12px;
}

.today-header {
	display: flex;
	align-items: center;
	gap: 12px;
}

.today-icon {
	width: 52px;
	height: 52px;
	object-fit: contain;
	flex-shrink: 0;
}

.today-title {
	flex: 1;
	min-width: 0;
}

.today-date {
	font-size: 11px;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.08em;
	color: var(--text-muted);
}

.today-label {
	font-size: 12px;
	font-weight: 600;
	color: var(--text-muted);
}

.hi-lo {
	display: flex;
	gap: 8px;
	font-size: 15px;
	font-weight: 600;
	margin-top: 2px;

	.hi { color: var(--text); }
	.lo { color: var(--text-muted); font-weight: 400; }
}

.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;
}

.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);
}

.stat-sub {
	font-size: 11px;
	color: var(--text-muted);
}

.section-label {
	font-size: 10px;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.08em;
	color: var(--text-muted);
	margin-bottom: -4px;
}
</style>

<template>
	<div class="today-card">
		<div class="flex-r align-items-center justify-between px-3">
			<div class="flex-c justify-center align-items-center">
				<img v-if="data.icon" :src="'https://openweathermap.org/img/wn/' + data.icon + '@2x.png'" class="today-icon" alt="weather" />
				<div class="today-label">{{ data.label }}</div>
			</div>
			<div class="flex-r align-items-center gap-2">
				<div class="fs-6">
					{{ data.temperature?.toFixed(0) }}°C
				</div>
				<div class="flex-c fg-muted">
					<span class="hi">↑ {{ 0 }}°</span>
					<span class="lo">↓ {{ 0 }}°</span>
				</div>
			</div>
		</div>

		<div class="divider" />

		<div class="stat-grid">
			<div class="stat"><span class="stat-label">Precipitation</span><span class="stat-value">{{ !data.precipitation && data.raining == 'True' ? 'Spitting' : (data.precipitation || 0).toFixed(1) + 'mm'}}</span></div>
			<div class="stat"><span class="stat-label">Humidity</span><span class="stat-value">{{ (data.humidity || 0).toFixed(0) }}%</span></div>
			<div class="stat"><span class="stat-label">Wind</span><span class="stat-value">{{ data.wind_gusts || 0 }} Kph <span class="fg-muted">({{ dir(data.wind_direction || 0) }})</span></span></div>
			<div class="stat"><span class="stat-label">Clouds</span><span class="stat-value">{{ (data.clouds || 0).toFixed(0) }}%</span></div>
			<div class="stat"><span class="stat-label">Air Quality</span><span class="stat-value">{{ data.air_quality }} <span class="fg-muted">({{data.air_quality_label}})</span></span></div>
			<div class="stat"><span class="stat-label">UV index</span><span class="stat-value">{{ (data.uv_index || 0).toFixed(1) }} <span class="fg-muted">({{data.uv_index_label}})</span></span></div>
		</div>
	</div>
</template>
