import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'vehicle_model.dart';
import 'vehicle_api.dart';

class VehicleApplyState {
  final VehicleModel vehicle;
  final bool isSubmitting;
  const VehicleApplyState({required this.vehicle, this.isSubmitting = false});
  VehicleApplyState copyWith({VehicleModel? vehicle, bool? isSubmitting}) =>
      VehicleApplyState(
        vehicle: vehicle ?? this.vehicle,
        isSubmitting: isSubmitting ?? this.isSubmitting,
      );
}

class VehicleApplyController extends StateNotifier<VehicleApplyState> {
  final VehicleApi _api;
  VehicleApplyController(this._api)
    : super(
        VehicleApplyState(
          vehicle: VehicleModel(
            id: '',
            applicationNo: '',
            applicantId: '',
            applicantName: '',
            deptId: '',
            deptName: '',
            purpose: '',
            startTime: DateTime.now(),
            endTime: DateTime.now().add(const Duration(hours: 2)),
            origin: '',
            destination: '',
            reason: '',
            createTime: DateTime.now(),
            updateTime: DateTime.now(),
          ),
        ),
      );

  void updatePurpose(String v) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(purpose: v));
  void updateVehicleType(String v) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(vehicleType: v));
  void updateStartTime(DateTime t) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(startTime: t));
  void updateEndTime(DateTime t) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(endTime: t));
  void updateOrigin(String v) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(origin: v));
  void updateDestination(String v) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(destination: v));
  void updatePassengerCount(int v) => state = state.copyWith(
    vehicle: state.vehicle.copyWith(passengerCount: v),
  );
  void updateDriver(String v) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(driver: v));
  void updateEstimatedMileage(double v) => state = state.copyWith(
    vehicle: state.vehicle.copyWith(estimatedMileage: v),
  );
  void updateReason(String v) =>
      state = state.copyWith(vehicle: state.vehicle.copyWith(reason: v));
  Future<bool> submit() async {
    state = state.copyWith(isSubmitting: true);
    try {
      await _api.submit(state.vehicle.copyWith(status: 'pending'));
      return true;
    } catch (_) {
      return false;
    } finally {
      state = state.copyWith(isSubmitting: false);
    }
  }

  Future<bool> saveDraft() async {
    state = state.copyWith(isSubmitting: true);
    try {
      await _api.saveDraft(state.vehicle);
      return true;
    } catch (_) {
      return false;
    } finally {
      state = state.copyWith(isSubmitting: false);
    }
  }
}

final vehicleApplyProvider = StateNotifierProvider.autoDispose
    .family<VehicleApplyController, VehicleApplyState, String?>((ref, editId) {
      return VehicleApplyController(ref.read(vehicleApiProvider));
    });
