import 'dart:io';

import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/ask_doctor/AskDoctorReqTypes.dart';
import 'package:hmg_patient_app/core/model/ask_doctor/DoctorResponse.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';

class AskDoctorService extends BaseService {
  List<AskDoctorReqTypes> askDoctorReqTypes =[];
  List<DoctorResponse> doctorResponseList =[];

  Future getCallInfoHoursResult({int? projectId, int? doctorId}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['DoctorID'] = doctorId;
    body['ProjectID'] = projectId;

    dynamic localRes;

    await baseAppClient.post(GET_CALL_INFO_HOURS_RESULT, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

  Future isDoctorAvailable({int? projectId, int? doctorId, int? clinicId}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['DoctorID'] = doctorId;
    body['ProjectID'] = projectId;
    body['ClinicID'] = clinicId;

    dynamic localRes;

    await baseAppClient.post(IS_DOCTOR_AVAILABLE_BY_CALENDAR_SCHEDULE, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

  Future getCallRequestTypeLOV() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_CALL_REQUEST_TYPE_LOV, onSuccess: (dynamic response, int statusCode) {
      askDoctorReqTypes.clear();
      response['ListReqTypes'].forEach((reqType) {
        askDoctorReqTypes.add(AskDoctorReqTypes.fromJson(reqType));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getQuestionTypes() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_QUESTION_TYPES, onSuccess: (dynamic response, int statusCode) {
      askDoctorReqTypes.clear();
      response['QuestionsTypes'].forEach((reqType) {
        askDoctorReqTypes.add(AskDoctorReqTypes.fromJson(reqType));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getDoctorResponse() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['from'] = "${DateTime.now().year}-${DateTime.now().month}-${DateTime.now().day} 00:00:00";
    body['from'] = "${DateTime.now().year}-${DateTime.now().month}-${DateTime.now().day} ${DateTime.now().hour}:${DateTime.now().minute}:00";
    await baseAppClient.post(GET_DOCTOR_RESPONSE, onSuccess: (dynamic response, int statusCode) {
      doctorResponseList.clear();
      response['List_DoctorResponse'].forEach((reqType) {
        doctorResponseList.add(DoctorResponse.fromJson(reqType));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future updateReadStatus({int? transactionNo}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['TransactionNo'] = transactionNo;
    await baseAppClient.post(UPDATE_READ_STATUS, onSuccess: (dynamic response, int statusCode) {
      //TODO fix it
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future sendRequestLOV({DoctorList? doctorList, String? requestType, String? remark}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['ProjectID'] = doctorList!.projectID;
    body['SetupID'] = doctorList.setupID;
    body['DoctorID'] = doctorList.doctorID;
    body['PatientMobileNumber'] = user.mobileNumber;
    body['IsMessageSent'] = false;
    body['RequestDate'] = DateUtil.convertDateToString(DateTime.now());
    body['RequestTime'] = DateUtil.convertDateToString(DateTime.now());
    body['Remarks'] = remark;
    body['Status'] = 2; // 4 for testing only.."cancelled status insert" else should be changed to 1 in live version
    body['CreatedBy'] = 102;
    body['CreatedOn'] = DateUtil.convertDateToString(DateTime.now());
    body['EditedBy'] = 102;
    body['EditedOn'] = DateUtil.convertDateToString(DateTime.now());
    body['isDentalAllowedBackend'] = false;
    body['AppointmentNo'] = doctorList.appointmentNo;
    body['ClinicID'] = doctorList.clinicID;
    body['QuestionType'] = num.parse(requestType!);
    body['RequestType'] = num.parse(requestType);
    body['RequestTypeID'] = num.parse(requestType);

    await baseAppClient.post(INSERT_CALL_INFO, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future rateDoctorResponse({int? transactionNo, int? questionType, int? rate, String? notes, String? mobileNo, String? idNo, String? patientName, int? projectID, String? language}) async {
    hasError = false;
    dynamic localRes;
    Map<String, dynamic> body = Map();
    body['MobileNo'] = mobileNo;
    body['IdentificationNo'] = idNo;
    body['PatientName'] = patientName;
    body['IsUserLoggedIn'] = true;
    body['ProjectID'] = projectID;
    body['UILanguage'] = language;
    body['BrowserInfo'] = Platform.localHostname;
    body['COCTypeName'] = 3;
    body['FormTypeID'] = 3;
    body['DeviceInfo'] = Platform.localHostname;
    body['Resolution'] = "400x847";
    body['AppVersion'] = VERSION_ID;
    body['ForDemo'] = false;
    body['isVidaPlus'] = false;
    body['TransactionNo'] = transactionNo;
    body['QuestionType'] = questionType;
    body['Rate'] = rate;
    body['Notes'] = notes;
    await baseAppClient.post(RATE_DOCTOR_RESPONSE, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }

}
