import 'dart:convert';
import 'dart:io';
import 'package:siro_driver/constant/api_key.dart';
import 'package:siro_driver/constant/box_name.dart';
import 'package:siro_driver/main.dart';
import 'package:http/http.dart' as http;

import '../../constant/links.dart';
import 'encrypt_decrypt.dart';
import 'upload_image.dart';

Future<String> faceDetector() async {
  await ImageController().choosFace(AppLink.uploadEgypt, 'face_detect');
  await Future.delayed(const Duration(seconds: 2));

  var headers = {
    'Authorization': 'Basic ${AK.basicCompareFaces}',
    'Content-Type': 'application/json'
  };

  var request = http.Request(
      'POST', Uri.parse('https://mohkh.online:5000/compare_faces'));

  request.body = json.encode({
    "url1":
        "${AppLink.seferCairoServer}/card_image/id_front-${(box.read(BoxName.driverID))}.jpg",
    "url2":
        "https://api.sefer.live/sefer/card_image/face_detect-${(box.read(BoxName.driverID))}.jpg"
  });
  request.headers.addAll(headers);

  try {
    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
      String result = await response.stream.bytesToString();
      return result;
    } else {
      return 'Error: ${response.reasonPhrase}';
    }
  } catch (e) {
    return 'Error: $e';
  }
}

Future<String> faceDetector2(String url1, String url2) async {
  var headers = {
    'Authorization': 'Basic ${AK.basicCompareFaces}',
    'Content-Type': 'application/json'
  };

  var request = http.Request(
      'POST', Uri.parse('https://mohkh.online:5000/compare_faces'));

  request.body = json.encode({"url1": url1, "url2": url2});
  request.headers.addAll(headers);

  try {
    http.StreamedResponse response = await request.send();
    if (response.statusCode == 200) {
      return await response.stream.bytesToString();
    } else {
      return 'Error: ${response.reasonPhrase}';
    }
  } catch (e) {
    return 'Exception: $e';
  }
}
