import 'package:intl/intl.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/mautic.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/service/mautic.dart';
import 'package:aitrainer_app/util/app_language.dart';

class MauticRepository {
  final CustomerRepository customerRepository;

  const MauticRepository({required this.customerRepository});

  Future<void> sendMauticSubscription() async {
    Mautic mautic = Mautic();
    mautic.formId = 2;
    mautic.databaseId = Cache().userLoggedIn!.customerId!;
    mautic.firstname = customerRepository.customer!.firstname == null ? "" : customerRepository.customer!.firstname!;
    mautic.lastname = customerRepository.customer!.name == null ? "" : customerRepository.customer!.name!;
    mautic.email = customerRepository.customer!.email == null ? "" : customerRepository.customer!.email!;
    if (mautic.email == null || mautic.email!.contains("privaterelay.appleid.com")) {
      return;
    }
    mautic.fitnessLevel = customerRepository.customer!.fitnessLevel == null ? "" : customerRepository.customer!.fitnessLevel!;
    mautic.goal = customerRepository.customer!.goal == null ? "" : customerRepository.customer!.goal!;
    mautic.subscriptionDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
    mautic.language = AppLanguage().appLocal.languageCode;

    await MauticApi().sendMauticForm(mautic);

    customerRepository.customer!.syncedDate = DateTime.now();
    await customerRepository.saveCustomer();
  }

  Future<void> sendMauticDataChange() async {
    Mautic mautic = Mautic();
    mautic.formId = 3;
    mautic.databaseId = Cache().userLoggedIn!.customerId!;
    mautic.firstname = customerRepository.customer!.firstname == null ? "" : customerRepository.customer!.firstname!;
    mautic.lastname = customerRepository.customer!.name == null ? "" : customerRepository.customer!.name!;
    mautic.email = customerRepository.customer!.email == null ? "" : customerRepository.customer!.email!;
    if (mautic.email == null || mautic.email!.contains("privaterelay.appleid.com")) {
      return;
    }
    mautic.fitnessLevel = customerRepository.customer!.fitnessLevel == null ? "" : customerRepository.customer!.fitnessLevel!;
    mautic.goal = customerRepository.customer!.goal == null ? "" : customerRepository.customer!.goal!;
    mautic.language = AppLanguage().appLocal.languageCode;

    await MauticApi().sendMauticForm(mautic);
  }

  Future<void> sendMauticPurchase() async {
    Mautic mautic = Mautic();
    mautic.formId = 4;
    mautic.firstname = customerRepository.customer!.firstname == null ? "" : customerRepository.customer!.firstname!;
    mautic.lastname = customerRepository.customer!.name == null ? "" : customerRepository.customer!.name!;
    mautic.email = customerRepository.customer!.email == null ? "" : customerRepository.customer!.email!;
    if (mautic.email == null || mautic.email!.contains("privaterelay.appleid.com")) {
      return;
    }
    mautic.purchaseDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());

    await MauticApi().sendMauticForm(mautic);
  }

  Future<void> sendMauticExercise() async {
    Mautic mautic = Mautic();
    mautic.formId = 5;
    mautic.firstname = customerRepository.customer!.firstname == null ? "" : customerRepository.customer!.firstname!;
    mautic.lastname = customerRepository.customer!.name == null ? "" : customerRepository.customer!.name!;
    mautic.email = customerRepository.customer!.email == null ? "" : customerRepository.customer!.email!;
    if (mautic.email == null || mautic.email!.contains("privaterelay.appleid.com")) {
      return;
    }
    mautic.exerciseDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
    mautic.databaseId = Cache().userLoggedIn!.customerId!;

    await MauticApi().sendMauticForm(mautic);
  }

  Future<void> sendMauticTrial() async {
    Mautic mautic = Mautic();
    mautic.formId = 6;
    mautic.email = customerRepository.customer!.email == null ? "" : customerRepository.customer!.email!;
    if (mautic.email == null || mautic.email!.contains("privaterelay.appleid.com")) {
      return;
    }
    mautic.trialDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
    mautic.databaseId = Cache().userLoggedIn!.customerId!;

    await MauticApi().sendMauticForm(mautic);
  }
}
