-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1377 lines (1179 loc) · 56.4 KB
/
script.js
File metadata and controls
1377 lines (1179 loc) · 56.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Chart from 'chart.js/auto';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import 'leaflet/dist/leaflet.css'; // Leaflet Styles
import L from 'leaflet';
import { initMap, updateMap, resizeMap } from './mapUtils.js';
import { saveCity, getSavedCities, removeCity } from './storageUtils.js';
// Register the plugin
Chart.register(ChartDataLabels);
// DOM Elements
const currentDateEl = document.getElementById('current-date');
const dailyListEl = document.getElementById('daily-list');
const hourlyCtx = document.getElementById('hourlyChart').getContext('2d');
const citySearch = document.getElementById('city-search');
const menuToggle = document.getElementById('menu-toggle');
const sidebar = document.querySelector('.sidebar');
const historyListEl = document.getElementById('history-list');
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
async function fetchWithRetry(url, options = {}, retries = 2, timeoutMs = 5000) {
for (let i = 0; i <= retries; i++) {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetch(`${API_BASE_URL}${url}`, { ...options, signal: controller.signal });
clearTimeout(id);
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
return await res.json();
} catch (error) {
clearTimeout(id);
if (i === retries) throw error;
console.warn(`[Frontend] Fetch retry ${i+1}/${retries} for ${url}`);
}
}
}
function showDashboardState(state, message = "") {
const overlay = document.getElementById('dashboard-state-overlay');
const grid = document.getElementById('main-dashboard-grid');
const panel = document.getElementById('intelligence-panel');
const forecasts = document.getElementById('main-forecasts-grid');
if (!overlay) return;
const icon = document.getElementById('dashboard-state-icon');
const text = document.getElementById('dashboard-state-text');
if (state === 'loading') {
overlay.style.display = 'flex';
icon.className = 'ri-loader-4-line ri-spin';
icon.style.color = 'var(--text-secondary)';
text.textContent = message || 'Loading weather data...';
text.style.color = 'var(--text-secondary)';
if (grid) grid.style.display = 'none';
if (panel) panel.style.display = 'none';
if (forecasts) forecasts.style.display = 'none';
} else if (state === 'error') {
overlay.style.display = 'flex';
icon.className = 'ri-error-warning-line';
icon.style.color = '#ff6b6b';
text.textContent = message || 'Unable to fetch data. Please try again.';
text.style.color = '#ff6b6b';
if (grid) grid.style.display = 'none';
if (panel) panel.style.display = 'none';
if (forecasts) forecasts.style.display = 'none';
} else {
// Success
overlay.style.display = 'none';
if (grid) grid.style.display = '';
if (panel) panel.style.display = '';
if (forecasts) forecasts.style.display = '';
}
}
function safeVal(val) {
return (val === null || val === undefined) ? "Data unavailable" : val;
}
let weatherChart;
let fullForecastData = [];
let currentCityData = null; // Store current city for saving
let isMapInitialized = false;
let currentForecastDuration = 6;
let currentForecastMetric = 'temperature';
let rawForecastData = null;
let currentDailyType = 'future';
// ── City Clock ────────────────────────────────────────────────────────────────
let cityClockInterval = null;
let cityClocktimezone = null; // IANA timezone string, e.g. 'Europe/London'
function startCityClock(tzId, cityLabel) {
if (cityClockInterval) clearInterval(cityClockInterval);
cityClocktimezone = tzId;
const labelEl = document.getElementById('city-clock-label');
if (labelEl) labelEl.textContent = cityLabel + ' Time';
const tzShort = tzId.split('/').pop().replace(/_/g, ' ');
const tzEl = document.getElementById('city-clock-tz');
if (tzEl) tzEl.textContent = tzId.replace(/_/g, ' ');
function tick() {
try {
const now = new Date();
const cityTime = new Date(now.toLocaleString('en-US', { timeZone: tzId }));
const h12 = cityTime.getHours() % 12 || 12;
const min = cityTime.getMinutes();
const sec = cityTime.getSeconds();
const ampm = cityTime.getHours() >= 12 ? 'PM' : 'AM';
// Digital
const timeEl = document.getElementById('city-clock-time');
const ampmEl = document.getElementById('city-clock-ampm');
if (timeEl) timeEl.textContent = `${String(h12).padStart(2,'0')}:${String(min).padStart(2,'0')}`;
if (ampmEl) ampmEl.textContent = ampm;
// Analog hands
const secDeg = sec * 6;
const minDeg = min * 6 + sec * 0.1;
const hrDeg = (cityTime.getHours() % 12) * 30 + min * 0.5;
const sH = document.getElementById('city-sec-hand');
const mH = document.getElementById('city-min-hand');
const hH = document.getElementById('city-hour-hand');
if (sH) sH.style.transform = `translateX(-50%) rotate(${secDeg}deg)`;
if (mH) mH.style.transform = `translateX(-50%) rotate(${minDeg}deg)`;
if (hH) hH.style.transform = `translateX(-50%) rotate(${hrDeg}deg)`;
} catch (e) {
console.warn('City clock tick error:', e);
}
}
tick();
cityClockInterval = setInterval(tick, 1000);
}
// -- Initialization --
function init() {
updateClock();
setInterval(updateClock, 1000); // 1s tick
// Auto-Location with better error handling
if (navigator.geolocation) {
document.body.style.cursor = 'wait';
navigator.geolocation.getCurrentPosition(
(pos) => {
if (hasUserSearched) return; // Prevent shifting if user already searched
const { latitude, longitude } = pos.coords;
console.log(`Location found: ${latitude}, ${longitude}`);
loadCity(`${latitude},${longitude}`);
},
(err) => {
if (hasUserSearched) return; // Prevent shifting
console.warn("Location access denied or error:", err.message);
loadCity('Kolkata'); // Default fallback
alert("Could not access your location. Defaulting to Kolkata.");
},
{
timeout: 10000,
enableHighAccuracy: true
}
);
} else {
console.warn("Geolocation not supported");
loadCity('Kolkata');
}
// Global flag to prevent late geolocation callbacks from overriding user searches
let hasUserSearched = false;
// Event Listeners
const searchInput = document.getElementById('city-search');
const searchBtn = document.getElementById('search-btn');
searchBtn.addEventListener('click', () => {
const city = searchInput.value.trim();
if (city) {
hasUserSearched = true;
document.getElementById('search-suggestions').classList.add('hidden');
loadCity(city);
}
});
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const city = searchInput.value.trim();
if (city) {
hasUserSearched = true;
document.getElementById('search-suggestions').classList.add('hidden');
loadCity(city);
}
}
});
// Autocomplete Logic
let searchTimeout;
const suggestionsBox = document.getElementById('search-suggestions');
searchInput.addEventListener('input', (e) => {
const query = e.target.value.trim();
clearTimeout(searchTimeout);
if (query.length < 3) {
suggestionsBox.classList.add('hidden');
return;
}
searchTimeout = setTimeout(async () => {
try {
const data = await fetchWithRetry(`/api/search?q=${encodeURIComponent(query)}`);
if (data && data.length > 0) {
suggestionsBox.innerHTML = '';
data.forEach(item => {
const li = document.createElement('li');
li.textContent = `${item.name}, ${item.country}`;
li.addEventListener('click', () => {
hasUserSearched = true;
searchInput.value = item.name;
suggestionsBox.classList.add('hidden');
loadCity(item.name);
});
suggestionsBox.appendChild(li);
});
suggestionsBox.classList.remove('hidden');
} else {
suggestionsBox.classList.add('hidden');
}
} catch (err) {
console.error('Search autocomplete error:', err);
}
}, 800); // 800ms debounce to prevent API spam
});
// Hide suggestions on outside click
document.addEventListener('click', (e) => {
if (!searchInput.contains(e.target) && !suggestionsBox.contains(e.target)) {
suggestionsBox.classList.add('hidden');
}
});
menuToggle.addEventListener('click', () => {
sidebar.classList.toggle('active');
});
// Close sidebar when clicking outside on mobile
document.addEventListener('click', (e) => {
if (window.innerWidth <= 900 &&
!sidebar.contains(e.target) &&
!menuToggle.contains(e.target) &&
sidebar.classList.contains('active')) {
sidebar.classList.remove('active');
}
});
// View Navigation Logic
const navBtns = document.querySelectorAll('.nav-btn');
const viewSections = document.querySelectorAll('.view-section');
navBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
e.preventDefault();
navBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
viewSections.forEach(section => section.classList.remove('active'));
viewSections.forEach(section => section.style.display = 'none'); // Explicit hide
const targetId = btn.getAttribute('data-target');
const targetSection = document.getElementById(targetId);
if (targetSection) {
targetSection.style.display = 'block'; // Explicit show first
// A small delay to allow transition if any, but mainly for Leaflet to detect size
setTimeout(() => {
targetSection.classList.add('active');
// Map Specific Logic
if (targetId === 'map-view') {
// If map needs initialization (first time view)
if (!isMapInitialized && currentCityData) {
const container = document.getElementById('map-container');
container.innerHTML = ''; // Clear placeholder
initMap(container, currentCityData.lat, currentCityData.lon);
isMapInitialized = true;
}
resizeMap();
}
// Saved Specific Logic
if (targetId === 'saved-view') {
renderSavedList();
}
// History Specific Logic
if (targetId === 'history-view') {
if (currentCityData) {
loadHistory(currentCityData.name);
} else {
historyListEl.innerHTML = '<div class="daily-item"><p>Please select a city first.</p></div>';
}
}
}, 10);
}
});
});
// Double click to save
const cityNameEl = document.getElementById('city-name');
if (cityNameEl) {
cityNameEl.title = "Double-click to save to favorites";
cityNameEl.style.cursor = 'pointer';
cityNameEl.addEventListener('dblclick', () => {
if (currentCityData) {
const saved = saveCity(currentCityData);
if (saved) {
alert(`${currentCityData.name} saved to favorites!`);
} else {
alert(`${currentCityData.name} is already saved.`);
}
}
});
}
// Hourly Forecast Toggles
const metricBtns = document.querySelectorAll('#metric-toggles .toggle-btn');
const durationBtns = document.querySelectorAll('#duration-toggles .toggle-btn');
metricBtns.forEach(btn => {
btn.addEventListener('click', () => {
metricBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentForecastMetric = btn.dataset.metric;
renderHourlyChart();
});
});
durationBtns.forEach(btn => {
btn.addEventListener('click', () => {
durationBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentForecastDuration = parseInt(btn.dataset.duration, 10);
renderHourlyChart();
});
});
// Daily Forecast Past/Future Toggles
const dailyBtns = document.querySelectorAll('#daily-toggles .toggle-btn');
dailyBtns.forEach(btn => {
btn.addEventListener('click', () => {
dailyBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentDailyType = btn.dataset.type;
if (currentDailyType === 'past') {
if (currentCityData) loadPastDailyForecast(currentCityData.name);
} else {
renderForecast();
}
});
});
// Initial Load of Saved Cities
renderSavedList();
}
// Top summary bar was removed
function updateClock() {
const now = new Date();
// Digital Time HH:MM AM/PM
const timeEl = document.getElementById('current-time');
if (timeEl) {
timeEl.textContent = now.toLocaleTimeString('en-US', {
hour12: true,
hour: '2-digit',
minute: '2-digit'
});
}
// Analog Clock Hands
const s = now.getSeconds();
const m = now.getMinutes();
const h = now.getHours() % 12;
const secDeg = s * 6; // 360 / 60
const minDeg = (m * 6) + (s * 0.1); // 360 / 60 + subtle second shift
const hrDeg = (h * 30) + (m * 0.5); // 360 / 12 + subtle minute shift
const secHand = document.getElementById('sec-hand');
const minHand = document.getElementById('min-hand');
const hrHand = document.getElementById('hour-hand');
if (secHand) secHand.style.transform = `translateX(-50%) rotate(${secDeg}deg)`;
if (minHand) minHand.style.transform = `translateX(-50%) rotate(${minDeg}deg)`;
if (hrHand) hrHand.style.transform = `translateX(-50%) rotate(${hrDeg}deg)`;
// GMT Time
const gmtEl = document.getElementById('gmt-time');
if (gmtEl) {
// e.g., 07:30 GMT
const gmtStr = now.toLocaleTimeString('en-GB', { timeZone: 'UTC', hour: '2-digit', minute: '2-digit' });
gmtEl.textContent = `GMT ${gmtStr}`;
}
// Date
const options = { weekday: 'long', month: 'long', day: 'numeric' };
if (currentDateEl) {
currentDateEl.textContent = now.toLocaleDateString('en-US', options);
}
// Greeting
const hrs = now.getHours();
let greet;
if (hrs >= 5 && hrs < 12) {
greet = "Good Morning";
} else if (hrs >= 12 && hrs < 17) {
greet = "Good Afternoon";
} else if (hrs >= 17 && hrs < 21) {
greet = "Good Evening";
} else {
greet = "Good Night";
}
const greetEl = document.getElementById('greeting');
if (greetEl) greetEl.textContent = greet;
}
// -- API Integration --
let isCityLoading = false;
async function loadCity(query) {
if (!query || typeof query !== 'string' || !query.trim()) return;
if (isCityLoading) return;
isCityLoading = true;
showDashboardState('loading');
document.body.style.cursor = 'wait';
try {
let fetchUrl;
if (query.includes(',')) {
const [lat, lon] = query.split(',');
fetchUrl = `/api/weather?lat=${lat.trim()}&lon=${lon.trim()}`;
} else {
fetchUrl = `/api/weather?q=${encodeURIComponent(query)}`;
}
const data = await fetchWithRetry(fetchUrl);
if (!data || !data.success) throw new Error(data?.message || 'City not found');
updateUI(data.data.raw, data.data);
showDashboardState('success');
} catch (error) {
console.error(error);
if (error.message.includes("City not found")) {
showDashboardState('error', "Location data unavailable");
} else {
showDashboardState('error');
}
} finally {
document.body.style.cursor = 'default';
citySearch.value = '';
setTimeout(() => { isCityLoading = false; }, 800);
}
}
async function loadHistory(cityName) {
const listEl = document.getElementById('history-picker');
const containerEl = document.getElementById('hist-grid-container');
const loadingEl = document.getElementById('hist-loading');
if (!listEl) return;
listEl.innerHTML = '<span style="color:#aaa; padding:10px;">Loading historical data...</span>';
containerEl.style.display = 'none';
loadingEl.style.display = 'block';
const dates = [];
for (let i = 1; i <= 7; i++) {
const d = new Date();
d.setDate(d.getDate() - i);
dates.push(d.toISOString().split('T')[0]); // YYYY-MM-DD
}
try {
const promises = dates.map(dt => fetchWithRetry(`/api/history?q=${encodeURIComponent(cityName)}&dt=${dt}`));
const settled = await Promise.allSettled(promises);
const results = settled.filter(res => res.status === 'fulfilled').map(res => res.value);
listEl.innerHTML = ''; // Clear loading
const validDays = [];
results.forEach(data => {
if (data.forecast && data.forecast.forecastday && data.forecast.forecastday[0]) {
const dayObj = data.forecast.forecastday[0];
dayObj._city = cityName; // manually pass city context
validDays.push(dayObj);
}
});
if (results.length === 0) {
listEl.innerHTML = '<span style="color:#aaa; padding:10px;">No history available.</span>';
loadingEl.innerHTML = '<p>No historical data available.</p>';
return;
}
if (validDays.length < 7 && validDays.length > 0) {
const noteEl = document.createElement('div');
noteEl.style.cssText = 'color: #888; font-size:0.85rem; padding: 8px 12px; background: rgba(255,255,255,0.05); border-radius: 8px; margin-bottom: 10px;';
noteEl.innerHTML = '⚠️ Showing available data (limited by API plan)';
listEl.appendChild(noteEl);
}
// ── Fetch Historical AQI from Open-Meteo (since WeatherAPI omits it) ──
try {
const { lat, lon } = currentCityData;
// Get past 7 days AQI in one call
const aqiRes = await fetch(`https://air-quality-api.open-meteo.com/v1/air-quality?latitude=${lat}&longitude=${lon}&hourly=us_aqi,carbon_monoxide&past_days=7`);
if (aqiRes.ok) {
const aqiData = await aqiRes.json();
const times = aqiData.hourly.time;
const usAqi = aqiData.hourly.us_aqi;
const co = aqiData.hourly.carbon_monoxide;
// Attach to validDays
validDays.forEach(day => {
const dayDate = day.date; // YYYY-MM-DD
// Find indices matching this day
const dayIndices = times.map((t, i) => t.startsWith(dayDate) ? i : -1).filter(i => i !== -1);
if (dayIndices.length > 0) {
const dayAqis = dayIndices.map(i => usAqi[i]).filter(x => x != null);
const dayCos = dayIndices.map(i => co[i]).filter(x => x != null);
if (dayAqis.length > 0) {
// Assign max AQI and avg CO directly to the day object for the renderer
day._openMeteoAqi = Math.max(...dayAqis);
day._openMeteoCo = Math.round(dayCos.reduce((a,b)=>a+b,0)/dayCos.length);
}
}
});
}
} catch(e) { console.warn("Could not fetch historical AQI", e); }
// Render Day Chips
validDays.forEach((day, index) => {
const chip = document.createElement('div');
chip.className = 'history-day-chip';
if (index === 0) chip.classList.add('active'); // auto-select first day
const dObj = new Date(day.date);
const shortStr = dObj.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' });
chip.innerHTML = `
<div class="chip-date">${shortStr}</div>
<div class="chip-temp">${Math.round(day.day.avgtemp_c)}°</div>
`;
chip.addEventListener('click', () => {
document.querySelectorAll('.history-day-chip').forEach(c => c.classList.remove('active'));
chip.classList.add('active');
renderHistoryDashboard(day);
});
listEl.appendChild(chip);
});
// Hide loading, show grid, render first day
loadingEl.style.display = 'none';
containerEl.style.display = 'block';
renderHistoryDashboard(validDays[0]);
} catch (err) {
console.error("History Error:", err);
listEl.innerHTML = '<span style="color:#ff6b6b; padding:10px;">Error loading history.</span>';
loadingEl.innerHTML = '<p>Error loading historical dashboard.</p>';
}
}
function renderHistoryDashboard(dayData) {
const day = dayData.day;
const astro = dayData.astro;
const hours = dayData.hour || [];
// Current Weather Hero
document.getElementById('hist-city-name').textContent = dayData._city;
document.getElementById('hist-temperature').textContent = `${Math.round(day.avgtemp_c)}°C`;
document.getElementById('hist-condition').textContent = day.condition.text;
document.getElementById('hist-feels-like').textContent = `High: ${day.maxtemp_c}° / Low: ${day.mintemp_c}°`;
// Visual Icon
const iconEl = document.getElementById('hist-weather-icon');
if (iconEl) {
const cond = day.condition.text.toLowerCase();
if (cond.includes('rain') || cond.includes('drizzle')) iconEl.className = 'ri-heavy-showers-line huge-icon';
else if (cond.includes('cloud') || cond.includes('overcast')) iconEl.className = 'ri-cloudy-line huge-icon';
else if (cond.includes('sunny') || cond.includes('clear')) iconEl.className = 'ri-sun-line huge-icon';
else iconEl.className = 'ri-sun-cloudy-line huge-icon';
}
// Wind
document.getElementById('hist-wind-status').innerHTML = `${day.maxwind_kph} <small>km/h</small>`;
const histRose = document.getElementById('hist-wind-rose');
const avgWindDeg = hours.length > 0
? Math.round(hours.reduce((s, h) => s + (h.wind_degree || 0), 0) / hours.length)
: null;
if (histRose && avgWindDeg !== null) histRose.style.setProperty('--rotation', `${avgWindDeg}deg`);
const avgWindDir = hours.length > 0 ? (hours[12] || hours[hours.length - 1]).wind_dir : 'N/A';
document.getElementById('hist-wind-dir').textContent = `Direction: ${avgWindDir}`;
// AQI – use Open-Meteo historical values if available
let aqiVal = null;
let coAvg = 0;
if (day._openMeteoAqi != null) {
aqiVal = day._openMeteoAqi;
coAvg = day._openMeteoCo || 0;
} else {
// Fallback to hourly if WeatherAPI magically provided it
const hourlyAqiVals = hours.map(h => h.air_quality?.['us-epa-index']).filter(v => v != null);
const hourlyCoVals = hours.map(h => h.air_quality?.co).filter(v => v != null);
const aqiMap = {1: 30, 2: 70, 3: 130, 4: 180, 5: 250, 6: 350};
if (hourlyAqiVals.length > 0) {
const maxEpa = Math.max(...hourlyAqiVals);
aqiVal = aqiMap[maxEpa] || 50;
coAvg = hourlyCoVals.length > 0 ? Math.round(hourlyCoVals.reduce((a, b) => a + b, 0) / hourlyCoVals.length) : 0;
}
}
if (aqiVal !== null) {
document.getElementById('hist-aqi-value').textContent = aqiVal;
document.getElementById('hist-co-level').textContent = `CO: ${coAvg} µg/m³`;
const aqiInfo = getAqiStatus(aqiVal);
document.getElementById('hist-aqi-status').textContent = aqiInfo.text;
document.getElementById('hist-aqi-status').style.color = aqiInfo.color;
const pct = Math.min(100, (aqiVal / 300) * 100);
const bar = document.getElementById('hist-aqi-progress');
if (bar) { bar.style.width = `${pct}%`; bar.style.backgroundColor = aqiInfo.color; }
const tipEl = document.getElementById('hist-aqi-tip');
if (tipEl) {
if (aqiVal <= 50) tipEl.textContent = 'Good air quality for the day.';
else if (aqiVal <= 100) tipEl.textContent = 'Air quality was acceptable.';
else if (aqiVal <= 150) tipEl.textContent = 'Sensitive groups may have been affected.';
else tipEl.textContent = 'Air quality was unhealthy on this day.';
}
} else {
document.getElementById('hist-aqi-value').textContent = 'Insufficient data';
document.getElementById('hist-aqi-status').textContent = 'Insufficient data';
document.getElementById('hist-co-level').textContent = 'CO: Insufficient data';
const bar = document.getElementById('hist-aqi-progress');
if (bar) { bar.style.width = '0%'; bar.style.backgroundColor = 'transparent'; }
const tipEl = document.getElementById('hist-aqi-tip');
if (tipEl) tipEl.textContent = 'Historical AQI not available.';
}
// Humidity
document.getElementById('hist-humidity').innerHTML = `${day.avghumidity}<small>%</small>`;
const histDropFill = document.getElementById('hist-humidity-drop');
if (histDropFill) histDropFill.style.setProperty('--fill', `${day.avghumidity}%`);
// Visibility
document.getElementById('hist-visibility').innerHTML = `${day.avgvis_km} <small>km</small>`;
// Pressure – average from hourly
const pressVals = hours.map(h => h.pressure_mb).filter(v => v != null && v > 0);
const pressEl = document.getElementById('hist-pressure');
if (pressEl && pressVals.length > 0) {
const avgPress = Math.round(pressVals.reduce((a, b) => a + b, 0) / pressVals.length);
pressEl.innerHTML = `${avgPress} <small style="font-size: 0.9rem">hPa</small>`;
const trendEl = document.getElementById('hist-pressure-trend');
if (trendEl) {
if (avgPress > 1020) trendEl.textContent = 'High / Stable';
else if (avgPress < 1000) trendEl.textContent = 'Low / Stormy';
else trendEl.textContent = 'Normal';
}
} else if (pressEl) {
pressEl.innerHTML = `Insufficient data`;
document.getElementById('hist-pressure-trend').textContent = 'Insufficient data';
}
// Sun Times
document.getElementById('hist-sunrise-time').textContent = astro.sunrise;
document.getElementById('hist-sunset-time').textContent = astro.sunset;
// Rainfall
document.getElementById('hist-rainfall').innerHTML = `${day.totalprecip_mm} <small>mm</small>`;
// UV Index + label
const uvEl = document.getElementById('hist-uv-index');
const uvLabelEl = document.getElementById('hist-uv-label');
if (uvEl) uvEl.textContent = day.uv;
if (uvLabelEl) {
const uv = day.uv;
if (uv <= 2) uvLabelEl.textContent = 'Low';
else if (uv <= 5) uvLabelEl.textContent = 'Moderate';
else if (uv <= 7) uvLabelEl.textContent = 'High';
else if (uv <= 10) uvLabelEl.textContent = 'Very High';
else uvLabelEl.textContent = 'Extreme';
}
}
async function loadPastDailyForecast(cityName) {
const listEl = document.getElementById('daily-list');
if (!listEl) return;
listEl.innerHTML = '<div class="daily-item"><p>Loading historical data...</p></div>';
const dates = [];
for (let i = 1; i <= 7; i++) {
const d = new Date();
d.setDate(d.getDate() - i);
dates.push(d.toISOString().split('T')[0]); // YYYY-MM-DD
}
try {
const promises = dates.map(dt => fetchWithRetry(`/api/history?q=${encodeURIComponent(cityName)}&dt=${dt}`));
const settled = await Promise.allSettled(promises);
const results = settled.filter(res => res.status === 'fulfilled').map(res => res.value);
listEl.innerHTML = ''; // Clear loading
results.forEach(data => {
if (data.forecast && data.forecast.forecastday && data.forecast.forecastday[0]) {
const day = data.forecast.forecastday[0];
const item = createForecastItem(day);
listEl.appendChild(item);
}
});
if (results.length === 0) {
listEl.innerHTML = '<div class="daily-item"><p>No history available.</p></div>';
} else if (results.length < 7) {
const noticeEl = document.createElement('div');
noticeEl.className = 'daily-item';
noticeEl.style.cssText = 'color:#888; font-size:0.85rem; padding:8px;';
noticeEl.textContent = '⚠️ Showing available data (limited by API plan)';
listEl.prepend(noticeEl);
}
} catch (err) {
console.error("Past Daily Error:", err);
listEl.innerHTML = '<div class="daily-item"><p>Error loading history.</p></div>';
}
}
async function updateUI(data, fused = null) {
const current = data.current;
const location = data.location;
const forecast = data.forecast.forecastday;
// Cache current city data
currentCityData = {
name: location.name,
lat: location.lat,
lon: location.lon
};
// Start city-local clock (uses IANA tz from WeatherAPI location.tz_id)
const tzId = location.tz_id;
if (tzId) startCityClock(tzId, location.name);
// Update Map if initialized
if (isMapInitialized) {
updateMap(location.lat, location.lon);
}
// 1. Header & Current
document.getElementById('city-name').textContent = location.name;
const tempC = Math.round(current.temp_c);
const tempF = Math.round(current.temp_f);
document.getElementById('temperature').textContent = `${tempC}°C / ${tempF}°F`;
// Use smart condition from fusion engine if available, else fall back to API text
const smartCondition = fused?.condition ?? current.condition.text;
document.getElementById('condition').textContent = smartCondition;
// Confidence badge
const badge = document.getElementById('confidence-badge');
if (badge && fused) {
badge.innerHTML = `
<span style="color:${fused.confidence_color}">${fused.confidence_icon} ${fused.confidence_label}</span>
· <span>Sources: ${fused.sources_count}</span>
· <span>Updated: ${fused.last_updated}</span>`;
badge.style.display = 'flex';
}
// Feels like
const feelsEl = document.getElementById('feels-like');
if (feelsEl) {
const fl = fused?.feels_like ?? current.feelslike_c;
feelsEl.textContent = `Feels like ${Math.round(fl)}°C`;
}
// Use native timezone hour for daylight checks and theme
let localHour = 12; // default
try {
if (location.tz_id) {
localHour = parseInt(new Date().toLocaleString('en-US', {
timeZone: location.tz_id,
hour: 'numeric',
hour12: false
}), 10);
}
} catch (e) { console.warn("Locale parsing error:", e); }
const isDay = (localHour >= 6 && localHour < 18);
// Update target greeting based on new timezone hour!
let greet = 'Good Night';
if (localHour >= 5 && localHour < 12) greet = 'Good Morning';
else if (localHour >= 12 && localHour < 17) greet = 'Good Afternoon';
else if (localHour >= 17 && localHour < 21) greet = 'Good Evening';
const greetEl = document.getElementById('greeting');
if (greetEl) greetEl.textContent = greet;
// Background dynamic logic
const conditionText = current.condition.text.toLowerCase();
const windKph = current.wind_kph;
// Theme Injection
document.body.classList.remove('theme-light', 'theme-dark');
document.body.classList.add(isDay ? 'theme-light' : 'theme-dark');
const themeIcon = document.getElementById('theme-icon');
if (themeIcon) {
themeIcon.className = isDay ? 'ri-sun-fill theme-icon' : 'ri-moon-fill theme-icon';
themeIcon.style.color = isDay ? '#FFD700' : '#A0A0B0'; // Yellow sun, silver moon
}
// Evaluate folder path based on time
const folder = isDay ? 'assets/day/' : 'assets/night/';
let bgImage = isDay ? 'assets/day/Cloudy.jpeg' : 'assets/night/cloudy.jpeg'; // base fallback
if (tempC > 35) {
bgImage = `${folder}heatwave.jpeg`;
} else if (tempC < 5 && !(conditionText.includes('snow') || conditionText.includes('blizzard') || conditionText.includes('ice'))) {
bgImage = isDay ? 'assets/day/Coldwave.png' : 'assets/night/coldwave.png';
} else if (windKph > 35) { // Extremely windy
// If it's night and windy, I only have assets/night/windy.png
// If it's day and windy, I have assets/day/windy.jpeg
bgImage = isDay ? 'assets/day/windy.jpeg' : 'assets/night/windy.png';
} else if (conditionText.includes('snow') || conditionText.includes('blizzard') || conditionText.includes('ice') || conditionText.includes('sleet')) {
bgImage = isDay ? 'assets/day/Snowy.jpeg' : 'assets/night/snowy.jpeg';
} else if (conditionText.includes('thunder') || conditionText.includes('storm') || conditionText.includes('torrential')) {
bgImage = `${folder}stormy.jpeg`; // stormy.jpeg is lowercase in both day and night
} else if (conditionText.includes('rain') || conditionText.includes('drizzle')) {
bgImage = isDay ? 'assets/day/Rainy.jpeg' : 'assets/night/rainy.jpeg';
} else if (conditionText.includes('mist') || conditionText.includes('fog')) {
if (current.vis_km > 7) {
bgImage = isDay ? 'assets/day/Sunny.jpeg' : 'assets/night/clear.jpeg';
} else {
bgImage = `${folder}foggy.jpeg`;
}
} else if (conditionText.includes('cloud') || conditionText.includes('overcast')) {
// assets/day/Cloudy.jpeg exists with capital C, assets/night/cloudy.jpeg exists with lowercase c
bgImage = isDay ? 'assets/day/Cloudy.jpeg' : 'assets/night/cloudy.jpeg';
} else if (conditionText.includes('sun') || conditionText.includes('clear')) {
bgImage = isDay ? 'assets/day/Sunny.jpeg' : 'assets/night/clear.jpeg';
} else {
bgImage = isDay ? 'assets/day/Sunny.jpeg' : 'assets/night/clear.jpeg';
}
document.body.style.backgroundImage = `url('${bgImage}')`;
// UV Index + label
const uvVal = current.uv;
document.getElementById('uv-index').textContent = uvVal;
const uvLabelEl = document.getElementById('uv-label');
if (uvLabelEl) {
if (uvVal <= 2) uvLabelEl.textContent = 'Low';
else if (uvVal <= 5) uvLabelEl.textContent = 'Moderate';
else if (uvVal <= 7) uvLabelEl.textContent = 'High';
else if (uvVal <= 10) uvLabelEl.textContent = 'Very High';
else uvLabelEl.textContent = 'Extreme';
}
// Wind Rotation
const rose = document.getElementById('wind-rose');
if (rose) rose.style.setProperty('--rotation', `${current.wind_degree}deg`);
// Use innerHTML to keep styling for units
document.getElementById('wind-status').innerHTML = `${current.wind_kph} <small>km/h</small>`;
document.getElementById('wind-dir').textContent = `Direction: ${current.wind_dir} (${current.wind_degree}°)`;
document.getElementById('humidity').innerHTML = `${current.humidity}<small>%</small>`;
// Humidity water drop dynamic fill
const dropFill = document.getElementById('humidity-drop');
if (dropFill) dropFill.style.setProperty('--fill', `${current.humidity}%`);
document.getElementById('visibility').innerHTML = `${fused?.visibility ?? current.vis_km} <small>km</small>`;
// Atmospheric Pressure
const pressEl = document.getElementById('pressure');
if (pressEl) {
const pMb = fused?.pressure_mb ?? current.pressure_mb;
pressEl.innerHTML = `${Math.round(pMb)} <small style="font-size: 0.9rem">hPa</small>`;
const trendEl = document.getElementById('pressure-trend');
if (trendEl) {
if (pMb > 1020) trendEl.textContent = "High / Stable";
else if (pMb < 1000) trendEl.textContent = "Low / Stormy";
else trendEl.textContent = "Normal";
}
}
// Sun data from today's forecast
const todayAstro = forecast[0].astro;
document.getElementById('sunrise-time').textContent = todayAstro.sunrise;
document.getElementById('sunset-time').textContent = todayAstro.sunset;
// Rainfall (using precip_mm)
document.getElementById('rainfall').innerHTML = `${current.precip_mm} <small>mm</small>`;
// AQI Logic
let aqiVal = 0;
let coVal = 0;
if (current.air_quality) {
const epaIndex = current.air_quality['us-epa-index'];
coVal = Math.round(current.air_quality.co);
const map = {1: 30, 2: 70, 3: 130, 4: 180, 5: 250, 6: 350};
aqiVal = map[epaIndex] || 50;
}
document.getElementById('aqi-value').textContent = aqiVal;
document.getElementById('co-level').textContent = `CO: ${coVal} µg/m³`;
const aqiInfo = getAqiStatus(aqiVal);
document.getElementById('aqi-status').textContent = aqiInfo.text;
document.getElementById('aqi-status').style.color = aqiInfo.color;
const percent = Math.min(100, (aqiVal / 300) * 100);
const bar = document.getElementById('aqi-progress');
bar.style.width = `${percent}%`;
bar.style.backgroundColor = aqiInfo.color;
// Update AQI Tip
const tipEl = document.getElementById('aqi-tip');
if (tipEl) {
if (aqiVal <= 50) tipEl.textContent = "Tip: Perfect day for outdoor activities!";
else if (aqiVal <= 100) tipEl.textContent = "Tip: Air quality is acceptable, enjoy your day.";
else if (aqiVal <= 150) tipEl.textContent = "Tip: Sensitive groups should limit prolonged outdoor exertion.";
else tipEl.textContent = "Tip: Wear a mask and stay indoors to protect your health.";
}
// 3. Hourly Chart
rawForecastData = forecast;
renderHourlyChart();
// 4. Daily Forecast (Full 14 days if available)
fullForecastData = forecast.map(day => {
const date = new Date(day.date);
return {
dateObj: date,
dayName: date.toLocaleDateString('en-US', { weekday: 'short' }),
dateStr: date.toLocaleDateString('en-US', { disableYear: true }),
high: Math.round(day.day.maxtemp_c),
low: Math.round(day.day.mintemp_c),
highF: Math.round(day.day.maxtemp_f),
lowF: Math.round(day.day.mintemp_f),
condition: day.day.condition.text,
isToday: date.getDate() === new Date().getDate(),
rawDay: day // keep raw if needed
};
});
// Fallback logic for full 7 day prediction via Open-Meteo since free Weather API limits forecast to 3 days
if (fullForecastData.length < 7 && currentCityData) {
try {
const omRes = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${currentCityData.lat}&longitude=${currentCityData.lon}&daily=weathercode,temperature_2m_max,temperature_2m_min&timezone=auto&forecast_days=7`);
if (omRes.ok) {
const omData = await omRes.json();
if (omData && omData.daily && omData.daily.time) {
fullForecastData = omData.daily.time.map((dateStr, i) => {
const date = new Date(dateStr);
// Make date object local noon to avoid timezone shift dropping a day
date.setHours(12, 0, 0, 0);
const code = omData.daily.weathercode[i];
let condition = "Clear";
if (code >= 1 && code <= 3) condition = "Cloudy";
else if (code >= 45 && code <= 48) condition = "Fog";
else if (code >= 51 && code <= 67) condition = "Rain";
else if (code >= 71 && code <= 77) condition = "Snow";
else if (code >= 95) condition = "Thunderstorm";
return {
dateObj: date,
dayName: date.toLocaleDateString('en-US', { weekday: 'short' }),
dateStr: date.toLocaleDateString('en-US', { disableYear: true }),
high: Math.round(omData.daily.temperature_2m_max[i]),
low: Math.round(omData.daily.temperature_2m_min[i]),
highF: Math.round(omData.daily.temperature_2m_max[i] * 9/5 + 32),