import 'wayz_common_models.dart';

class WzWeatherLive {
  final String province;
  final String city;
  final String adcode;
  final String weather;
  final String temperature;
  final String winddirection;
  final String windpower;
  final String humidity;
  final String reporttime;

  WzWeatherLive({
    required this.province,
    required this.city,
    required this.adcode,
    required this.weather,
    required this.temperature,
    required this.winddirection,
    required this.windpower,
    required this.humidity,
    required this.reporttime,
  });

  factory WzWeatherLive.fromJson(Map<String, dynamic> json) {
    return WzWeatherLive(
      province: WzJson.toStringOrNull(json['province']) ?? '',
      city: WzJson.toStringOrNull(json['city']) ?? '',
      adcode: WzJson.toStringOrNull(json['adcode']) ?? '',
      weather: WzJson.toStringOrNull(json['weather']) ?? '',
      temperature: WzJson.toStringOrNull(json['temperature']) ?? '',
      winddirection: WzJson.toStringOrNull(json['winddirection']) ?? '',
      windpower: WzJson.toStringOrNull(json['windpower']) ?? '',
      humidity: WzJson.toStringOrNull(json['humidity']) ?? '',
      reporttime: WzJson.toStringOrNull(json['reporttime']) ?? '',
    );
  }

  Map<String, dynamic> toJson() => {
        'province': province,
        'city': city,
        'adcode': adcode,
        'weather': weather,
        'temperature': temperature,
        'winddirection': winddirection,
        'windpower': windpower,
        'humidity': humidity,
        'reporttime': reporttime,
      };
}

class WzWeatherCast {
  final String date;
  final String week;
  final String dayweather;
  final String nightweather;
  final String daytemp;
  final String nighttemp;
  final String daywind;
  final String nightwind;
  final String daypower;
  final String nightpower;

  WzWeatherCast({
    required this.date,
    required this.week,
    required this.dayweather,
    required this.nightweather,
    required this.daytemp,
    required this.nighttemp,
    required this.daywind,
    required this.nightwind,
    required this.daypower,
    required this.nightpower,
  });

  factory WzWeatherCast.fromJson(Map<String, dynamic> json) {
    return WzWeatherCast(
      date: WzJson.toStringOrNull(json['date']) ?? '',
      week: WzJson.toStringOrNull(json['week']) ?? '',
      dayweather: WzJson.toStringOrNull(json['dayweather']) ?? '',
      nightweather: WzJson.toStringOrNull(json['nightweather']) ?? '',
      daytemp: WzJson.toStringOrNull(json['daytemp']) ?? '',
      nighttemp: WzJson.toStringOrNull(json['nighttemp']) ?? '',
      daywind: WzJson.toStringOrNull(json['daywind']) ?? '',
      nightwind: WzJson.toStringOrNull(json['nightwind']) ?? '',
      daypower: WzJson.toStringOrNull(json['daypower']) ?? '',
      nightpower: WzJson.toStringOrNull(json['nightpower']) ?? '',
    );
  }

  Map<String, dynamic> toJson() => {
        'date': date,
        'week': week,
        'dayweather': dayweather,
        'nightweather': nightweather,
        'daytemp': daytemp,
        'nighttemp': nighttemp,
        'daywind': daywind,
        'nightwind': nightwind,
        'daypower': daypower,
        'nightpower': nightpower,
      };
}

class WzWeatherForecast {
  final String city;
  final String adcode;
  final String province;
  final String reporttime;
  final List<WzWeatherCast> casts;

  WzWeatherForecast({
    required this.city,
    required this.adcode,
    required this.province,
    required this.reporttime,
    required this.casts,
  });

  factory WzWeatherForecast.fromJson(Map<String, dynamic> json) {
    return WzWeatherForecast(
      city: WzJson.toStringOrNull(json['city']) ?? '',
      adcode: WzJson.toStringOrNull(json['adcode']) ?? '',
      province: WzJson.toStringOrNull(json['province']) ?? '',
      reporttime: WzJson.toStringOrNull(json['reporttime']) ?? '',
      casts: (json['casts'] as List? ?? [])
          .map((e) => WzWeatherCast.fromJson((e as Map).cast<String, dynamic>()))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() => {
        'city': city,
        'adcode': adcode,
        'province': province,
        'reporttime': reporttime,
        'casts': casts.map((e) => e.toJson()).toList(),
      };
}

class WzWeatherResponse {
  final String status;
  final String count;
  final String info;
  final String infocode;
  final List<WzWeatherLive> lives;
  final List<WzWeatherForecast> forecasts;

  WzWeatherResponse({
    required this.status,
    required this.count,
    required this.info,
    required this.infocode,
    required this.lives,
    required this.forecasts,
  });

  factory WzWeatherResponse.fromJson(Map<String, dynamic> json) {
    return WzWeatherResponse(
      status: WzJson.toStringOrNull(json['status']) ?? '',
      count: WzJson.toStringOrNull(json['count']) ?? '',
      info: WzJson.toStringOrNull(json['info']) ?? '',
      infocode: WzJson.toStringOrNull(json['infocode']) ?? '',
      lives: (json['lives'] as List? ?? [])
          .map((e) => WzWeatherLive.fromJson((e as Map).cast<String, dynamic>()))
          .toList(),
      // doc says `forecasts: Forecast`, but sample shows array.
      forecasts: (json['forecasts'] as List? ?? [])
          .map((e) => WzWeatherForecast.fromJson((e as Map).cast<String, dynamic>()))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() => {
        'status': status,
        'count': count,
        'info': info,
        'infocode': infocode,
        'lives': lives.map((e) => e.toJson()).toList(),
        'forecasts': forecasts.map((e) => e.toJson()).toList(),
      };
}

