import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/sick_leave_patient_model.dart';
import 'package:doctor_app_flutter/core/model/sick_leave_patient_request_model.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';
import 'package:doctor_app_flutter/core/viewModel/leave_rechdule_response.dart';
import 'package:doctor_app_flutter/models/sickleave/add_sickleave_request.dart';
import 'package:doctor_app_flutter/models/sickleave/extend_sick_leave_request.dart';
import 'package:doctor_app_flutter/models/sickleave/get_all_sickleave_response.dart';
import 'package:doctor_app_flutter/util/dr_app_toast_msg.dart';
// import 'package:doctor_app_flutter/models/dashboard/dashboard_model.dart';

class SickLeaveService extends BaseService {
  Map get sickLeavestatisitics => _statistics;
  Map _statistics = {};
  List get getOffTimeList => offTime;
  List offTime = [];
  List get getReasons => reasonse;
  List reasonse = [];
  List<GetAllSickLeaveResponse> get getAllSickLeave => _getAllsickLeave;
  List<GetAllSickLeaveResponse> _getAllsickLeave = [];

  List get coveringDoctorsList => _coveringDoctors;
  List _coveringDoctors = [];

  List<GetRescheduleLeavesResponse> get getAllRescheduleLeave =>
      _getReScheduleLeave;
  List<GetRescheduleLeavesResponse> _getReScheduleLeave = [];
  dynamic get postReschedule => _postReschedule;
  dynamic _postReschedule;

  dynamic get sickLeaveResponse => _sickLeaveResponse;
  dynamic _sickLeaveResponse;

  List<SickLeavePatientModel> get getAllSickLeavePatient =>
      _getAllsickLeavePatient;
  List<SickLeavePatientModel> _getAllsickLeavePatient = [];

  SickLeavePatientRequestModel _sickLeavePatientRequestModel =
      SickLeavePatientRequestModel();

  Future getStatistics(appoNo, patientMRN) async {
    hasError = false;
    await baseAppClient.post(
      GET_SICKLEAVE_STATISTIC,
      onSuccess: (dynamic response, int statusCode) {
        print(response);
        _statistics = {};
        _statistics = response['SickLeaveStatistics'];
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {"PatientMRN": patientMRN, "AppointmentNo": appoNo},
    );
  }

  Future addSickLeave(AddSickLeaveRequest addSickLeaveRequest) async {
    // addSickLeaveRequest.appointmentNo = '2016054661';
    // addSickLeaveRequest.patientMRN = '3120746';
    hasError = false;
    await baseAppClient.post(
      ADD_SICK_LEAVE,
      onSuccess: (dynamic response, int statusCode) {
        _sickLeaveResponse = response;
        return Future.value(response);
      },
      onFailure: (String error, int statusCode) {
        DrAppToastMsg.showErrorToast(error);
        // hasError = true;
        // super.error = error;
      },
      body: addSickLeaveRequest.toJson(),
    );
  }

  Future extendSickLeave(GetAllSickLeaveResponse request) async {
    var extendSickLeaveRequest = ExtendSickLeaveRequest();
    extendSickLeaveRequest.patientMRN =
        request.patientMRN.toString(); //'3120746';
    extendSickLeaveRequest.previousRequestNo = request.requestNo.toString();
    extendSickLeaveRequest.noOfDays = request.noOfDays.toString();
    extendSickLeaveRequest.remarks = request.remarks;
    hasError = false;
    await baseAppClient.post(
      EXTEND_SICK_LEAVE,
      onSuccess: (dynamic response, int statusCode) {
        _sickLeaveResponse = response;
        return Future.value(response);
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: extendSickLeaveRequest.toJson(),
    );
  }

  Future getSickLeave(patientMRN) async {
    hasError = false;
    await baseAppClient.postPatient(
      GET_SICK_LEAVE,
      onSuccess: (dynamic response, int statusCode) {
        Future.value(response);
        _getAllsickLeavePatient.clear();
        response['List_SickLeave'].forEach((v) {
          _getAllsickLeavePatient.add(SickLeavePatientModel.fromJson(v));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {'PatientMRN': patientMRN, "Status": 0},
    );
  }

  Future getSickLeavePatient(patientMRN) async {
    _sickLeavePatientRequestModel = SickLeavePatientRequestModel(
        patientID: patientMRN, patientTypeID: 2, patientType: 1);
    hasError = false;
    await baseAppClient.postPatient(
      GET_SICK_LEAVE_PATIENT,
      onSuccess: (dynamic response, int statusCode) {
        Future.value(response);
        _getAllsickLeavePatient.clear();
        response['List_SickLeave'].forEach((v) {
          _getAllsickLeavePatient.add(SickLeavePatientModel.fromJson(v));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: _sickLeavePatientRequestModel.toJson(),
    );
  }

  Future getRescheduleLeave() async {
    hasError = false;
    await baseAppClient.post(
      GET_RESCHEDULE_LEAVE,
      onSuccess: (dynamic response, int statusCode) {
        Future.value(response);
        _getReScheduleLeave.clear();
        response['requisitionList']['entityList'].forEach((v) {
          _getReScheduleLeave.add(GetRescheduleLeavesResponse.fromJson(v));
        });
        _getReScheduleLeave.sort((a, b) {
          var adate = a.dateTimeFrom; //before -> var adate = a.date;
          var bdate = b.dateTimeFrom; //var bdate = b.date;
          return -adate.compareTo(bdate);
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {'ClinicID': 1},
    );
  }

  Future getOffTime() async {
    hasError = false;

    await baseAppClient.post(
      GET_OFFTIME,
      onSuccess: (dynamic response, int statusCode) {
        offTime = [];
        response['MasterLookUpList']['entityList'].forEach((item) => {
              //if (item['code'] == '1' || item['code'] == '2')
              // {
              offTime.add(item)
              //}//
            });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {"MasterInput": 2013},
    );
  }

  Future getReasonsByID({id}) async {
    hasError = false;

    await baseAppClient.post(
      GET_OFFTIME,
      onSuccess: (dynamic response, int statusCode) {
        reasonse = [];
        reasonse = response['MasterLookUpList']['entityList'];
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {"MasterInput": id ?? 18},
    );
  }

  Future getCoveringDoctors() async {
    hasError = false;

    await baseAppClient.post(
      GET_COVERING_DOCTORS,
      onSuccess: (dynamic response, int statusCode) {
        _coveringDoctors = [];
        _coveringDoctors = response['coveringDoctorList']['entityList'];
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {"ClinicID": 1},
    );
  }

  Future addReschedule(request) async {
    hasError = false;

    await baseAppClient.post(
      ADD_RESCHDEULE,
      onSuccess: (dynamic response, int statusCode) {
        // return Future.value(response);
        _postReschedule = response;
      },
      onFailure: (String error, int statusCode) {
        _postReschedule = null;
        hasError = true;
        super.error = error;
      },
      body: request,
    );
  }

  updateReschedule(request) async {
    hasError = false;

    await baseAppClient.post(
      UPDATE_RESCHDEULE,
      onSuccess: (dynamic response, int statusCode) {
        _postReschedule = response;
        // return Future.value(response);
      },
      onFailure: (String error, int statusCode) {
        _postReschedule = null;
        hasError = true;
        super.error = error;
      },
      body: request,
    );
  }
}
