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

class DirectService {


  Future<void> postDirect(ModelDirect direct) async {
    try {
      dynamic json;
      String method = "/ws_jubo/service/v1/direct";
      if (direct.id != null) {
        json = direct.toJson2();
      } else {
        json = direct.toJsonNew2();
      }

      /*
      StreamedResponse stream =
          await generic.requestPostFormData(json, image, method, "");

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

       */

      http.Response result = await generic.requestPost(json, method, "");

      Map<String, dynamic> body;

      body = jsonDecode(result.body);

      //print(result.statusCode);
      //print(result.body);
      if (result.statusCode != 200 || body['error'] == true) {
        throw 'Falha de comunicação';
      }

      //m = jsonResponse.map((job) => new ModelPerson.fromJson(job));
      //final m = new ModelDirect.fromJson(body['data']);
      final m = body['data']['id'];

    } catch (e) {
      throw e;
    }
  }


}
