import 'dart:convert';
import 'dart:io';
import 'package:easycook_mobile/models/register_form_model.dart';
import 'package:easycook_mobile/repositories/Exception.dart';
import 'package:easycook_mobile/shared/shared_values.dart' as AppConst;
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

class RegisterRepository implements Exception {
  Dio dio = new Dio();

  final String _baseUrl = AppConst.BASE_URL;

  Future<RegisterMessage> register(
      firstName, lastName, username, email, password, phone) async {
    final formData = FormData.fromMap({
      'first_name': firstName,
      'last_name': lastName,
      'username': username,
      'email': email,
      'password': password,
      'phone': phone,
    });
    var response = await dio.post(
      "$_baseUrl/accounts/register/",
      data: formData,
      options: Options(validateStatus: (value) => true, headers: {
        HttpHeaders.contentTypeHeader: 'application/json',
      }),
    );
    debugPrint(response.statusCode.toString());
    if (response.statusCode == 200) {
      return RegisterMessage(message: "suksess");
    }
    if (response.statusCode == 400) {
      throw GeneralException(response.data["message"]);
      // return RegisterMessage.fromJson(response.data);
    }
    return RegisterMessage(message: "message");
  }
}
