import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:unit2/model/passo/land_ext.dart';
import 'package:unit2/model/passo/property_assessment.dart';
import 'package:http/http.dart' as http;
import 'package:unit2/model/passo/property_assessment_edit.dart';
import 'package:unit2/utils/request.dart';
import 'package:unit2/utils/urls.dart';

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

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

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

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

  Future<http.Response?> add(LandExt assessment) async {
    Map<dynamic, dynamic> statusResponse = {};
    String path = Url.instance.getLandExt();
    Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      'X-Client-Key': xClientKey,
      'X-Client-Secret': xClientKeySecret
    };
    http.Response? response;
    try {
      response = await Request.instance.postRequest(
          param: {}, path: path, body: assessment.toJson(), headers: headers);
    } catch (e) {
      log(e.toString());
    }
    return response;
  }

  Future<http.Response?> update(LandExt assessment, id) async {
    String path = Url.instance.getLandExt();
    Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      'X-Client-Key': xClientKey,
      'X-Client-Secret': xClientKeySecret
    };
    Map<String, String> params = {
      "landappr_details_id": id.toString(),
    };
    http.Response? response;
    try {
      response = await Request.instance.putRequest(
          path: path,
          body: assessment.toJson(),
          headers: headers,
          param: params);
    } catch (e) {
      log(e.toString());
    }
    return response;
  }

  // Future<LandExtEdit> fetchEdit(tempID) async {
  //   http.Response response = await http.get(Uri.parse(
  //       '$baseUrl${Url.instance.getLandExt()}?bldgappr_details_id=$tempID'));
  //   print('Assessment');
  //   print(response.body);
  //   if (response.statusCode == 200) {
  //     final jsonData = jsonDecode(response.body);
  //     final dataList = jsonData['data'] as List<dynamic>;
  //     final result =
  //         LandExtEdit.fromJson(dataList[0] as Map<String, dynamic>);

  //     return result;
  //   } else {
  //     throw Exception(response.reasonPhrase);
  //   }
  // }

  // Future<http.Response?> addEdit(LandExtEdit assessment) async {
  //   http.Response? response;
  //   try {
  //     response = await http.post(
  //         Uri.parse("$baseUrl${Url.instance.getLandExt()}"),
  //         headers: {
  //           HttpHeaders.contentTypeHeader: "application/json",
  //         },
  //         body: jsonEncode(assessment.toJson()));
  //   } catch (e) {
  //     log(e.toString());
  //   }
  //   return response;
  // }

  // Future<http.Response?> updateEdit(
  //     LandExtEdit assessment, id) async {
  //   print(id);
  //   http.Response? response;
  //   try {
  //     response = await http.put(Uri.parse(
  //             // ignore: unnecessary_brace_in_string_interps
  //             "$baseUrl${Url.instance.getLandExt()}?bldgappr_details_id=${id}"),
  //         headers: {
  //           HttpHeaders.contentTypeHeader: "application/json",
  //         },
  //         body: jsonEncode(assessment.toJson()));
  //   } catch (e) {
  //     log(e.toString());
  //   }
  //   return response;
  // }
}
