
import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../repository/transaksi_repository.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

abstract class InsertTransaksiState extends Equatable {
  final DateTime date;
  const InsertTransaksiState(this.date);
  @override
  List<Object?> get props => [date];
}

class InsertTransaksiStateInit extends InsertTransaksiState {
  InsertTransaksiStateInit() : super(DateTime.now());
}

class InsertTransaksiStateLoading extends InsertTransaksiState {
  InsertTransaksiStateLoading() : super(DateTime.now());
}

class InsertTransaksiStateError extends InsertTransaksiState {
  final String message;
  InsertTransaksiStateError({
    required this.message,
  }) : super(DateTime.now());
}

class InsertTransaksiStateDone extends InsertTransaksiState {
  // final List<ListType> model;
  final String resp;
  InsertTransaksiStateDone({
    required this.resp,
  }) : super(DateTime.now());
}

//notifier
class InsertTransaksiNotifier extends StateNotifier<InsertTransaksiState> {
  final Ref ref;
  InsertTransaksiNotifier({
    required this.ref,
  }) : super(InsertTransaksiStateInit());

  void insertTransaksi(
    String tanggal,
    String tipe,
    String kategoriid,
    String jumlah,
    String catatan,
    String userid,
    String sender,
    String url,
  ) async {
    try {
      state = InsertTransaksiStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await TransaksiRepository(dio: dio).insertTransaksi(
        tanggal,
        tipe,
        kategoriid,
        jumlah,
        catatan,
        userid,
        sender,
        url,
      );
      state = InsertTransaksiStateDone(resp: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = InsertTransaksiStateError(message: e.message.toString());
      } else {
        state = InsertTransaksiStateError(message: e.toString());
      }
    }
  }
}

//provider
final insertTransaksiProvider =
    StateNotifierProvider<InsertTransaksiNotifier, InsertTransaksiState>(
  (ref) => InsertTransaksiNotifier(ref: ref),
);
