import 'dart:convert';

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

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

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

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

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