import 'dart:io';

import 'package:aitrainer_app/model/mautic.dart';
import 'package:aitrainer_app/service/logging.dart';

class MauticApi with Logging {
  final String mauticUrl = "https://mautic.aitrainer.app/form/submit?formId=";

  Future<void> sendMauticForm(Mautic model) async {
    final String body = model.toForm();
    log(" ===== mautic subscription:" + body);
    HttpClient client = new HttpClient();

    String url = mauticUrl + model.formId.toString();

    var uri = Uri.parse(url);
    final HttpClientRequest request = await client.postUrl(uri);
    request.headers.set('Content-Type', 'application/x-www-form-urlencoded');
    request.headers.set('cache-control', 'no-cache');

    request.write(body);
    HttpClientResponse result = await request.close();
    client.close();
    if (!(result.statusCode == 200 || result.statusCode == 302)) {
      trace("mautic response: ${result.statusCode}");
      //throw Exception("Network error, try again later!");
    }
  }
}
