"""Weather data fetching module using Open-Meteo API."""

import requests


class WeatherFetcher:
    """Fetch weather data for hiking locations."""

    def __init__(self):
        """Initialize weather fetcher."""
        self.base_url = 'https://api.open-meteo.com/v1/forecast'

    def get_weather(self, latitude, longitude):
        """Fetch weather data for given coordinates.

        Args:
            latitude: Latitude of the location
            longitude: Longitude of the location

        Returns:
            dict: Weather data with temperature, humidity, precipitation, etc.
                  Returns None if request fails.
        """
        try:
            params = {
                'latitude': latitude,
                'longitude': longitude,
                'current': 'temperature_2m,relative_humidity_2m,precipitation,wind_speed_10m',
                'daily': 'temperature_2m_max,temperature_2m_min,precipitation_probability_max',
                'timezone': 'Asia/Taipei',
                'forecast_days': 3,
            }

            response = requests.get(self.base_url, params=params, timeout=10)
            response.raise_for_status()

            data = response.json()

            # Parse current weather
            current = data.get('current', {})
            daily = data.get('daily', {})

            weather_info = {
                'current_temperature': current.get('temperature_2m'),
                'current_humidity': current.get('relative_humidity_2m'),
                'current_precipitation': current.get('precipitation'),
                'current_wind_speed': current.get('wind_speed_10m'),
                'daily_temp_max': daily.get('temperature_2m_max', []),
                'daily_temp_min': daily.get('temperature_2m_min', []),
                'daily_precipitation_prob': daily.get('precipitation_probability_max', []),
                'daily_time': daily.get('time', []),
            }

            return weather_info

        except requests.exceptions.RequestException as e:
            print(f'天氣資料獲取失敗: {str(e)}')
            return None
        except Exception as e:
            print(f'天氣資料處理失敗: {str(e)}')
            return None

    def format_weather_display(self, weather_info):
        """Format weather data for display.

        Args:
            weather_info: Weather data dictionary from get_weather()

        Returns:
            dict: Formatted weather data for UI display
        """
        if not weather_info:
            return None

        formatted = {
            'temperature': f'{weather_info.get("current_temperature", "N/A")}°C',
            'humidity': f'{weather_info.get("current_humidity", "N/A")}%',
            'precipitation': f'{weather_info.get("current_precipitation", 0)} mm',
            'wind_speed': f'{weather_info.get("current_wind_speed", "N/A")} km/h',
            'forecast': [],
        }

        # Add 3-day forecast
        times = weather_info.get('daily_time', [])
        temp_max = weather_info.get('daily_temp_max', [])
        temp_min = weather_info.get('daily_temp_min', [])
        precip_prob = weather_info.get('daily_precipitation_prob', [])

        for i in range(min(3, len(times))):
            formatted['forecast'].append(
                {
                    'date': times[i],
                    'temp_max': temp_max[i] if i < len(temp_max) else 'N/A',
                    'temp_min': temp_min[i] if i < len(temp_min) else 'N/A',
                    'precip_prob': precip_prob[i] if i < len(precip_prob) else 'N/A',
                }
            )

        return formatted
