import 'dart:convert';

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

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

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

  Future<List<LandSubClassification>> fetch(cityCode, classCode) async {
    String path = Url.instance.getLandSubClassification();
    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": cityCode.toString(),
      "classification_id": classCode.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) => LandSubClassification.fromJson(e))).toList();
      } else {
        throw Exception(response.reasonPhrase);
      }
    } catch (e) {
      throw e.toString();
    }
  }
}
