import 'dart:convert';

import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/models/livecare/get_panding_req_list.dart';
import 'package:doctor_app_flutter/models/livecare/get_pending_res_list.dart';
import 'package:doctor_app_flutter/models/livecare/start_call_req.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:flutter/cupertino.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';

class LiveCareProvider with ChangeNotifier {
  DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

  List<LiveCarePendingListResponse> liveCarePendingList = [];
  var inCallResponse = {};
  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';

  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<Map> 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["VC_ID"];
    newRequest.isrecall = isReCall;
    newRequest.doctorId = profile["DoctorID"];
    newRequest.isOutKsa = request["IsOutKSA"];
    newRequest.projectName = profile["ProjectName"];
    newRequest.docotrName = profile["DoctorName"];
    newRequest.clincName = profile["ClinicDescription"];
    newRequest.clincName = profile["ClinicDescription"];
    newRequest.docSpec = profile["DoctorTitleForProfile"];
    newRequest.generalid = 'Cs2020@2016\$2958';
    isFinished = false;
    await BaseAppClient.post(START_LIVECARE_CALL,
        onSuccess: (response, statusCode) async {
      isFinished = true;
      inCallResponse = response;
    }, onFailure: (String error, int statusCode) {
      isFinished = true;
      throw error;
    }, body: newRequest.toJson());
    return Future.value(inCallResponse);
  }

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