import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/procedure/Procedure_template_request_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/categories_procedure.dart';
import 'package:doctor_app_flutter/core/model/procedure/get_ordered_procedure_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/get_ordered_procedure_request_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/get_procedure_req_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/post_procedure_req_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_template_details_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_template_details_request_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_valadate_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_valadate_request_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/update_procedure_request_model.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';

class ProcedureService extends BaseService {
  List<GetOrderedProcedureModel> _procedureList = [];

  List<GetOrderedProcedureModel> get procedureList => _procedureList;
  List<ProcedureValadteModel> _valadteProcedureList = [];

  List<ProcedureValadteModel> get valadteProcedureList => _valadteProcedureList;
  List<CategoriseProcedureModel> _categoriesList = [];

  List<CategoriseProcedureModel> get categoriesList => _categoriesList;
  List<Procedures> procedureslist = [];
  List<dynamic> categoryList = [];

  // List<ProcedureTempleteModel> _templateList =[];
  // List<ProcedureTempleteModel> get templateList => _templateList;
  List<ProcedureTempleteDetailsModel> templateList = [];

  List<ProcedureTempleteDetailsModel> _templateDetailsList = [];

  List<ProcedureTempleteDetailsModel> get templateDetailsList => _templateDetailsList;

  GetOrderedProcedureRequestModel _getOrderedProcedureRequestModel = GetOrderedProcedureRequestModel();

  ProcedureTempleteRequestModel _procedureTempleteRequestModel = ProcedureTempleteRequestModel();
  ProcedureTempleteDetailsRequestModel _procedureTempleteDetailsRequestModel = ProcedureTempleteDetailsRequestModel();

  GetProcedureReqModel _getProcedureReqModel = GetProcedureReqModel();

  GetProcedureReqModel _getProcedureCategoriseReqModel = GetProcedureReqModel(
    clinicId: 0,
    pageSize: 100,
    pageIndex: 1,
    patientMRN: 0,
  );

  Future getProcedureTemplate({int? doctorId, int? projectId, int? clinicId, String? categoryID, bool? isLocalBusy}) async {
    _procedureTempleteRequestModel = ProcedureTempleteRequestModel(
      // tokenID: "@dm!n",
      patientID: 0,
      searchType: 1,
    );

    hasError = false;

    await baseAppClient.post(GET_TEMPLETE_LIST /*GET_PROCEDURE_TEMPLETE*/, onSuccess: (dynamic response, int statusCode) {
      templateList.clear();
      response['DAPP_TemplateGetList'].forEach((template) {
        ProcedureTempleteDetailsModel templateElement = ProcedureTempleteDetailsModel.fromJson(template);
        if (categoryID != null) {
          if (categoryID == templateElement.categoryID) {
            templateList.add(templateElement);
          }
        } else {
          templateList.add(templateElement);
        }
      });
      // response['HIS_ProcedureTemplateList'].forEach((template) {
      //   _templateList.add(ProcedureTempleteModel.fromJson(template));
      // });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _procedureTempleteRequestModel.toJson());
  }

  Future getProcedureTemplateDetails({int? doctorId, int? projectId, int? clinicId, int? templateId}) async {
    _procedureTempleteDetailsRequestModel = ProcedureTempleteDetailsRequestModel(templateID: templateId, searchType: 1, patientID: 0);
    hasError = false;
    //insuranceApprovalInPatient.clear();
    _templateDetailsList.clear();

    await baseAppClient.post(GET_PROCEDURE_TEMPLETE_DETAILS, onSuccess: (dynamic response, int statusCode) {
      //prescriptionsList.clear();
      response['HIS_ProcedureTemplateDetailsList'].forEach((template) {
        _templateDetailsList.add(ProcedureTempleteDetailsModel.fromJson(template));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _procedureTempleteDetailsRequestModel.toJson());
  }

  Future getProcedure({int? mrn, int? appointmentNo}) async {
    _getOrderedProcedureRequestModel = GetOrderedProcedureRequestModel(
      patientMRN: mrn,
    );
    hasError = false;
    _procedureList.clear();
    await baseAppClient.post(GET_PROCEDURE_LIST, onSuccess: (dynamic response, int statusCode) {
      _procedureList.add(GetOrderedProcedureModel.fromJson(response['OrderedProcedureList']));
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _getOrderedProcedureRequestModel.toJson());
  }

  Future getCategory() async {
    hasError = false;

    await baseAppClient.post(GET_LIST_CATEGORISE, onSuccess: (dynamic response, int statusCode) {
      categoryList = [];
      categoryList = response['listProcedureCategories']['entityList'];
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  Future getProcedureCategory({String? categoryName, String? categoryID, patientId}) async {
    _getProcedureCategoriseReqModel = GetProcedureReqModel(
      search: ["$categoryName"],
      patientMRN: patientId,
      pageIndex: 0,
      clinicId: 0,
      pageSize: 0,
      category: categoryID ?? null,
    );
    hasError = false;
    _categoriesList.clear();
    await baseAppClient.post(GET_CATEGORISE_PROCEDURE, onSuccess: (dynamic response, int statusCode) {
      _categoriesList.add(CategoriseProcedureModel.fromJson(response['ProcedureList']));
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _getProcedureCategoriseReqModel.toJson());
  }

  Future postProcedure(PostProcedureReqModel postProcedureReqModel) async {
    hasError = false;
    _procedureList.clear();
    await baseAppClient.post(POST_PROCEDURE_LIST, onSuccess: (dynamic response, int statusCode) {
      print("Success");
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: postProcedureReqModel.toJson());
  }

  Future updateProcedure(UpdateProcedureRequestModel updateProcedureRequestModel) async {
    hasError = false;
    _procedureList.clear();
    await baseAppClient.post(UPDATE_PROCEDURE, onSuccess: (dynamic response, int statusCode) {
      print("ACCEPTED");
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: updateProcedureRequestModel.toJson());
  }

  Future validationProcedure(ProcedureValidationRequestModel validationProcedureRequestModel) async {
    hasError = false;
    _valadteProcedureList.clear();
    await baseAppClient.post(GET_PROCEDURE_VALIDATION, onSuccess: (dynamic response, int statusCode) {
      _valadteProcedureList.add(
        ProcedureValadteModel.fromJson(response['ValidateProcedureList']),
      );
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: validationProcedureRequestModel.toJson());
  }
}
