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/core/model/livecare/end_call_req.dart';
import 'package:doctor_app_flutter/core/model/livecare/get_panding_req_list.dart';
import 'package:doctor_app_flutter/core/model/livecare/get_pending_res_list.dart';
import 'package:doctor_app_flutter/core/model/livecare/start_call_req.dart';
import 'package:doctor_app_flutter/core/model/livecare/start_call_res.dart';
import 'package:doctor_app_flutter/core/model/livecare/transfer_to_admin.dart';
import 'package:doctor_app_flutter/utils/dr_app_shared_pref.dart';
import 'package:flutter/cupertino.dart';

//TODO: change it when Live care return back.
class LiveCareViewModel with ChangeNotifier {
  DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

  List<LiveCarePendingListResponse> liveCarePendingList = [];
  StartCallRes? inCallResponse;
  var transferToAdmin = {};
  var endCallResponse = {};
  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';
  BaseAppClient baseAppClient = BaseAppClient();

  LiveCarePendingListRequest _pendingRequestModel =
      LiveCarePendingListRequest();

  Future<List> getpendingList() async {
    var profile = await sharedPref.getObj(DOCTOR_PROFILE);
    _pendingRequestModel.projectID = await sharedPref.getInt(PROJECT_ID);
    _pendingRequestModel.doctorID = profile['DoctorID'];
    _pendingRequestModel.sErServiceID = "1,3,7";
    _pendingRequestModel.sourceID = 1;
    _pendingRequestModel.patientData = PatientData(isOutKSA: false);
    resetDefaultValues();
    // dynamic localRes;
    await baseAppClient.post(GET_LIVECARE_PENDINGLIST,
        onSuccess: (response, statusCode) async {
      isFinished = true;
      liveCarePendingList = [];
      response['List_PendingPatientList'].forEach((v) {
        liveCarePendingList.add(new LiveCarePendingListResponse.fromJson(v));
      });
    }, onFailure: (String error, int statusCode) {
      isFinished = true;
      throw error;
    }, body: _pendingRequestModel.toJson());
    return Future.value(liveCarePendingList);
  }

  Future<StartCallRes> startCall(request, bool isReCall) async {
    var profile = await sharedPref.getObj(DOCTOR_PROFILE);
    resetDefaultValues();
    /* the request model is not same hence added manually */
    var newRequest = new StartCallReq();
    newRequest.clinicId = profile["ClinicID"];
    newRequest.vCID = request.vCID; //["VC_ID"];
    newRequest.isrecall = isReCall;
    newRequest.doctorId = profile["DoctorID"];
    newRequest.isOutKsa = request.isOutKSA; //["IsOutKSA"];
    newRequest.projectName = profile["ProjectName"];
    newRequest.docotrName = profile["DoctorName"];
    newRequest.clincName = profile["ClinicDescription"];
    newRequest.docSpec = profile["DoctorTitleForProfile"];
    newRequest.generalid = 'Cs2020@2016\$2958';
    isFinished = false;
    await baseAppClient.post(START_LIVE_CARE_CALL,
        onSuccess: (response, statusCode) async {
      isFinished = true;
      inCallResponse = StartCallRes.fromJson(response);
    }, onFailure: (String error, int statusCode) {
      isFinished = true;
      throw error;
    }, body: newRequest.toJson());
    return Future.value(inCallResponse);
  }

  transfterToAdmin(request, notes) async {
    var profile = await sharedPref.getObj(DOCTOR_PROFILE);
    var newRequest = new TransferToAdminReq();
    newRequest.doctorId = profile["DoctorID"];
    newRequest.isOutKsa = request.isOutKSA;
    newRequest.generalid = 'Cs2020@2016\$2958';
    newRequest.vCID = request.vCID; //["VC_ID"];
    newRequest.notes = await baseAppClient.post(TRANSFERT_TO_ADMIN,
        onSuccess: (response, statusCode) async {
      isFinished = true;
      transferToAdmin = response;
    }, onFailure: (String error, int statusCode) {
      isFinished = true;
      throw error;
    }, body: newRequest.toJson());
    return Future.value(inCallResponse);
  }

  endCall(request, isPaitent, doctorID) async {
    var newRequest = new EndCallReq();
    newRequest.doctorId = doctorID; //profile["DoctorID"];
    newRequest.generalid = 'Cs2020@2016\$2958';
    newRequest.vCID = request.vCID; //["VC_ID"];
    newRequest.isDestroy = isPaitent;
    await baseAppClient.post(END_CALL, onSuccess: (response, statusCode) async {
      isFinished = true;
      endCallResponse = response;
    }, onFailure: (String error, int statusCode) {
      isFinished = true;
      throw error;
    }, body: newRequest.toJson());
    return Future.value(inCallResponse);
  }

  endCallWithCharge(vcID, doctorID) async {
    var newRequest = new EndCallReq();
    newRequest.vCID = vcID;
    newRequest.doctorId = doctorID;
    newRequest.generalid = 'Cs2020@2016\$2958';
    await baseAppClient.post(END_CALL_WITH_CHARGE,
        onSuccess: (response, statusCode) async {
      isFinished = true;
      endCallResponse = response;
    }, onFailure: (String error, int statusCode) {
      isFinished = true;
      throw error;
    }, body: newRequest.toJson());
    return Future.value(inCallResponse);
  }

  resetDefaultValues() {
    isFinished = false;
    hasError = false;
    errorMsg = '';
    notifyListeners();
  }
}
