import 'dart:convert';

import 'package:unit2/model/passo/barangay.dart';
import 'package:unit2/model/passo/class_components.dart';
import 'package:http/http.dart' as http;
import 'package:unit2/utils/request.dart';
import 'package:unit2/utils/urls.dart';

class BarangayServices {
  static final BarangayServices _instance = BarangayServices();
  static BarangayServices get instance => _instance;

  String xClientKey = "unitK3CQaXiWlPReDsBzmmwBZPd9Re1z";
  String xClientKeySecret = "unitcYqAN7GGalyz";

  Future<List<Brgy>> fetch(id) async {
    String path = Url.instance.getBarangay();
    Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      'X-Client-Key': xClientKey,
      'X-Client-Secret': xClientKeySecret
    };
    Map<String, String> params = {
      "city_code": id.toString(),
    };
    try {
      http.Response response = await Request.instance
          .getRequest(param: params, path: path, headers: headers);
      if (response.statusCode == 200) {
        final List result = jsonDecode(response.body)['data'];

        return result.map(((e) => Brgy.fromJson(e))).toList();
      } else {
        throw Exception(response.reasonPhrase);
      }
    } catch (e) {
      throw e.toString();
    }
  }
}
