import 'dart:convert';
import 'package:http/http.dart' as http;

class FirebaseData {
  final String? pagi;
  final String? siang;
  final String? malam;
  final int? pagiPutaran;
  final int? siangPutaran;
  final int? malamPutaran;

  String url =
      'https://jam-makan-default-rtdb.asia-southeast1.firebasedatabase.app/jam_makan.json';

  FirebaseData({
    this.pagi,
    this.siang,
    this.malam,
    this.pagiPutaran,
    this.siangPutaran,
    this.malamPutaran,
  });

  Future<void> toJson() async {
    Map<String, dynamic> json = {};

    if (pagi != null) {
      json['pagi'] = pagi;
    }
    if (siang != null) {
      json['siang'] = siang;
    }
    if (malam != null) {
      json['malam'] = malam;
    }
    if (pagiPutaran != null) {
      json['pagiPutaran'] = pagiPutaran;
    }
    if (siangPutaran != null) {
      json['siangPutaran'] = siangPutaran;
    }
    if (malamPutaran != null) {
      json['malamPutaran'] = malamPutaran;
    }

    await http.patch(Uri.parse(url), body: jsonEncode(json));
  }

  Future<FirebaseData> getData() async {
    final response = await http.get(Uri.parse(url));

    Map<String, dynamic> data = json.decode(response.body);
    return FirebaseData(
      pagi: data['pagi'],
      siang: data['siang'],
      malam: data['malam'],
      pagiPutaran: data['pagiPutaran'],
      siangPutaran: data['siangPutaran'],
      malamPutaran: data['malamPutaran'],
    );
  }
}
