import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:jubo/model/modelCreditCard.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 CreditCardService {
  Future<SharedPreferences> _storage() {
    return SharedPreferences.getInstance();
  }

  Future<void> postCreditCard(ModelCreditCard creditCard) async {
    try {
      dynamic json;
      String method = "/ws_jubo/service/v1/paymeth";

        json = creditCard.toJson();


      /*
      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\n'+body.toString();
      }

      //final m = new ModelCreditCard.fromJson(body['data']);

    } catch (e) {
      throw e;
    }
  }
}
