import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/models/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';

class BaseService {
  String error;
  bool hasError = false;
  BaseAppClient baseAppClient = BaseAppClient();
  DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();
  DoctorProfileModel doctorProfile;

  List<PatiantInformtion> patientArrivalList = [];

//TODO add the user login model when we need it
  Future<DoctorProfileModel> getDoctorProfile() async {
    if (doctorProfile == null) {
      Map profile = await sharedPref.getObj(DOCTOR_PROFILE);
      if (profile != null) {
        doctorProfile = DoctorProfileModel.fromJson(profile);
        if (doctorProfile != null) {
          return doctorProfile;
        }
      }
      return null;
    } else {
      return doctorProfile;
    }
  }

  Future getPatientArrivalList(String date,{String fromDate, int patientMrn = -1, int appointmentNo = -1}) async{
    hasError = false;
    Map<String, dynamic> body = Map();
    body['From'] = fromDate == null ? date : fromDate;
    body['To'] = date;
    body['PageIndex'] = 0;
    body['PageSize'] = 0;
    if(patientMrn != -1){
      body['PatientMRN'] = patientMrn;
    }
    if(appointmentNo != -1){
      body['AppointmentNo'] = appointmentNo;
    }

    await baseAppClient.post(
      ARRIVED_PATIENT_URL,
      onSuccess: (dynamic response, int statusCode) {
        patientArrivalList.clear();

        if(response['patientArrivalList']['entityList'] != null){
          response['patientArrivalList']['entityList'].forEach((v) {
            PatiantInformtion item = PatiantInformtion.fromJson(v);
            patientArrivalList.add(item);
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        this.error = error;
      },
      body: body,
    );
  }

}
