import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/rate/appointment_details.dart';
import 'package:diplomaticquarterapp/core/model/rate/appoitment_rated.dart';
import 'package:diplomaticquarterapp/core/service/appointment_rate_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import '../../locator.dart';

class AppointmentRateViewModel extends BaseViewModel {
  AppointmentRateService _appointmentRateService =
      locator<AppointmentRateService>();
  bool isHaveAppointmentNotRate = false;

  AppointmentDetails get appointmentDetails => _appointmentRateService.appointmentDetails;


  Future getIsLastAppointmentRatedList() async {
    isHaveAppointmentNotRate = false;
    setState(ViewState.Busy);
    await _appointmentRateService.getIsLastAppointmentRatedList();
    if (_appointmentRateService.hasError) {
      error = _appointmentRateService.error;
      setState(ViewState.Error);
    } else {
      if(_appointmentRateService.appointmentRatedList.length>0)
      await getAppointmentDetails();
    }
  }

  getAppointmentDetails() async {
    await _appointmentRateService.getAppointmentDetails();
    if (_appointmentRateService.hasError) {
      error = _appointmentRateService.error;
      setState(ViewState.Error);
    } else {
      if (_appointmentRateService.appointmentDetails != null)
        isHaveAppointmentNotRate = true;
      setState(ViewState.Idle);
    }
  }

  Future sendAppointmentRate(int rate, int appointmentNo, int projectID,
      int doctorID, int clinicID, String note) async {
    setState(ViewState.BusyLocal);
    await _appointmentRateService.sendAppointmentRate(
        rate, appointmentNo, projectID, doctorID, clinicID, note);
    if (_appointmentRateService.hasError) {
      error = _appointmentRateService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
      _appointmentRateService.deleteAllAppAppointmentRate();
    }
  }

  AppoitmentRated get lastAppointmentRated =>
      _appointmentRateService.lastAppointmentRated;

  deleteAppointmentRated(AppoitmentRated appointmentRated) =>
      _appointmentRateService.deleteAppointmentRated(appointmentRated);

  deleteAppAppointmentRate() =>
      _appointmentRateService.deleteAllAppAppointmentRate();

  setIsRated(bool isRated) {
    this.isHaveAppointmentNotRate = isRated;
    notifyListeners();
  }
}
