import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:jubo/model/person.dart';
import 'package:jubo/util/params.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:http_parser/http_parser.dart';

Future<http.Response> requestPost(
    dynamic json, String method, String authorization) async {
  return http.post("$urlPrincipal$method",
      headers: {
        //"Authorization": authorization,
        "Content-Type": "application/json"
      },
      body: jsonEncode(json)).timeout(Duration(seconds: 10));
}

Future<http.Response> requestDelete(
    int id, String method, String authorization) async {
  return http.delete("$urlPrincipal$method$id",
      headers: {
        //"Authorization": authorization,
        "Content-Type": "application/json"
      }).timeout(Duration(seconds: 10));
}

Future<http.Response> requestPut(
    dynamic json, String method, String authorization) async {
  return http.put("$urlPrincipal$method",
      headers: {
        //"Authorization": authorization,
        "Content-Type": "application/json"
      },
      body: jsonEncode(json)).timeout(Duration(seconds: 10));
}

/*
Future<StreamedResponse> requestPostFormData(
    String json, File file, String method, String authorization) async {
  http.Response rsp;
  var uri = Uri.parse("$urlPrincipal$method");
  var request = new http.MultipartRequest("POST", uri);
  //"{'password':'123321','name':'elaine','photo':'','birth':'2021-05-11','type':'Usuário','email':'elaine@123.com'}"
  request.fields['content'] = json;
  if (file != null) {
    request.files.add(new http.MultipartFile.fromBytes(
        'file', await file.readAsBytes(),
        contentType: new MediaType('image', 'jpeg')));
  }

  return request.send();
}
*/

Future<http.Response> requestPostFormData(
    String json, File file, String method, String authorization) async {
  http.Response rsp;
  var uri = Uri.parse("$urlPrincipal$method");
  var request = new http.MultipartRequest("POST", uri);
  //"{'password':'123321','name':'elaine','photo':'','birth':'2021-05-11','type':'Usuário','email':'elaine@123.com'}"
  request.fields['content'] = json;
  if (file != null) {
    request.files.add(new http.MultipartFile.fromBytes(
        'file', await file.readAsBytes(),
        contentType: new MediaType('image', 'jpeg')));
  }

  var stream = await request.send().timeout(Duration(seconds: 10));

  return await http.Response.fromStream(stream).then((response) {
    return response;
  });
}

Future<http.Response> requestGet(
    dynamic json, String method, String authorization) async {
  return http.post("$urlPrincipal$method",
      headers: {
        //"Authorization": authorization,
        "Content-Type": "application/json"
      },
      body: jsonEncode(json)).timeout(Duration(seconds: 10));
}
