import 'dart:convert';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/model/user.dart';
import 'package:aitrainer_app/service/api.dart';
import 'package:aitrainer_app/model/cache.dart';

class CustomerApi {
  final APIClient _client = new APIClient();

  Future<List<Customer>> getRealCustomers(String param) async {
    final body = await _client.get("customers/", param);
    final Iterable json = jsonDecode(body);
    final List<Customer> customers = json.map((customer) =>
      Customer.fromJson(customer)).toList();

    return customers;
  }

  Future<void> saveCustomer(Customer customer) async {
    String body = JsonEncoder().convert(customer.toJson());
    print(" ===== saving customer id: " + customer.customerId.toString() + ":" +
      body);
    await _client.post(
      "customers/" + customer.customerId.toString(),
      body);
  }

  Future<void> addCustomer(Customer customer) async {
    String body = JsonEncoder().convert(customer.toJson());
    print(" ===== add new customer: " + body);
    await _client.post(
      "customers",
      body);
  }

  Future<void> addUser(User user) async {
    String body = JsonEncoder().convert(user.toJson());
    print(" ===== register new user: " + body);
    final String responseBody = await _client.post(
      "registration",
      body);
    Customer customer;
    try {
      int status = jsonDecode(responseBody)['status'];
      if (status != null) {
        throw new Exception(jsonDecode(responseBody)['error']);
      } else {
        customer = Customer.fromJson(jsonDecode(responseBody));
        Cache().afterRegistration(customer);
      }
    } on FormatException catch (exception) {
      throw new Exception(responseBody);
    }
  }

  Future<void> getUser(User user) async {
    String body = JsonEncoder().convert(user.toJson());
    print(" ===== login the user: " + body);
    final String responseBody = await _client.post(
      "login",
      body);
    Customer customer;
    try {
      customer = Customer.fromJson(jsonDecode(responseBody));
      await Cache().afterLogin(customer);
    } on FormatException catch (exception) {
      throw new Exception(responseBody);
    }
  }


  Future<void> getCustomer(int customerId) async {
    String body = "";
    print(" ===== get the customer by id: " + customerId.toString());
    try {
      final String responseBody = await _client.get(
        "customers/" + customerId.toString(),
        body);
      Customer customer = Customer.fromJson(jsonDecode(responseBody));
      print(" --- Customer: " + customer.toJson().toString());
      Cache().afterRegistration(customer);
    } catch (exception) {
      print("Exception: " + exception.toString());
      print(" === go to registration ");
      Cache().logout();
      Cache().startPage = "registration";
    }
  }

  Future<Customer> getTrainee(int customerId) async {
    String body = "";
    Customer customer;
    print(" ===== get Trainee customer by id: " + customerId.toString());
    try {
      final String responseBody = await _client.get(
        "customers/" + customerId.toString(),
        body);
      customer = Customer.fromJson(jsonDecode(responseBody));
      print(" --- Trainee: " + customer.toJson().toString());
    } catch (exception) {
      print("Exception: " + exception.toString());
      throw Exception(exception);
    }
    return customer;
  }

  Future<List<Customer>> getTrainees(int trainerId) async {
    List<Customer> trainees = List<Customer>();
    print("Get trainees list");
    try {
      String body = "";
      final String responseBody = await _client.get(
        "customers/trainees/" + trainerId.toString(),
        body
      );
      final Iterable json = jsonDecode(responseBody);
      trainees = json.map((customer) => Customer.fromJson(customer)).toList();
    } catch (exception) {
      print("Exception: " + exception.toString());
      throw Exception(exception);
    }
    return trainees;
  }
}