import 'package:intl/intl.dart';

class PrayersWidgetModel {
  int? fajr;
  String? sunrise;
  int? dhuhr;
  int? asr;
  String? sunset;
  int? maghrib;
  int? isha;
  String? imsak;
  String? midnight;
  String? firstthird;
  String? lastthird;
  String? dateFor;

  PrayersWidgetModel({this.fajr, this.sunrise, this.dhuhr, this.asr, this.sunset, this.maghrib, this.isha, this.imsak, this.midnight, this.firstthird, this.lastthird, this.dateFor});

  PrayersWidgetModel.fromJson(Map<String, dynamic> json) {
    fajr = dateTimeConversion(json['dateFor'], json['fajr']);
    sunrise = json['sunrise'];
    dhuhr = dateTimeConversion(json['dateFor'], getDhuhrData(json), isForDhuhr: true);
    asr = dateTimeConversion(json['dateFor'], json['asr']);
    sunset = json['sunset'];
    maghrib = dateTimeConversion(json['dateFor'], json['maghrib']);
    isha = dateTimeConversion(json['dateFor'], json['isha']);
    imsak = json['imsak'];
    midnight = json['midnight'];
    firstthird = json['firstthird'];
    lastthird = json['lastthird'];
    dateFor = json['dateFor'];
  }

  static String getDhuhrData(dynamic json) {
    return (json['dhuhr'] as String).contains('PM') ? json['dhuhr'] : json['dhuhr'] + ' PM';
  }

  static dateTimeConversion(String date, String time, {bool isForDhuhr = false}) {
    if (isForDhuhr) {
      return DateFormat('DD MMM yyyy hh:mm a').parse("$date $time").millisecondsSinceEpoch;
    }
    return DateFormat('DD MMM yyyy hh:mm').parse("$date $time").millisecondsSinceEpoch;
  }

  @override
  String toString() {
    return 'PrayersWidgetModel{fajr: $fajr, sunrise: $sunrise, dhuhr: $dhuhr, asr: $asr, sunset: $sunset, maghrib: $maghrib, isha: $isha, imsak: $imsak, midnight: $midnight, firstthird: $firstthird, lastthird: $lastthird, dateFor: $dateFor}';
  }
}
