import 'package:doctor_app_flutter/core/enum/patient_type.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/model/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/service/patient/out_patient_service.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/util/date-utils.dart';

import '../../locator.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/model/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/service/patientInPatientService.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';

import '../../locator.dart';
import 'base_view_model.dart';

class PatientSearchViewModel extends BaseViewModel{
  OutPatientService _outPatientService = locator<OutPatientService>();

  List<PatiantInformtion> get patientList => _outPatientService.patientList;

  List<PatiantInformtion> filterData = [];

  searchData(String str) {
    var strExist = str.length > 0 ? true : false;
    if (strExist) {
      filterData = [];
      for (var i = 0; i < _outPatientService.patientList.length; i++) {
        String firstName = _outPatientService.patientList[i].firstName.toUpperCase();
        String lastName = _outPatientService.patientList[i].lastName.toUpperCase();
        String mobile = _outPatientService.patientList[i].mobileNumber.toUpperCase();
        String patientID = _outPatientService.patientList[i].patientId.toString();

        if (firstName.contains(str.toUpperCase()) ||
            lastName.contains(str.toUpperCase()) ||
            mobile.contains(str) ||
            patientID.contains(str)) {
          filterData.add(_outPatientService.patientList[i]);
        }
      }
      notifyListeners();
    } else {
      filterData = _outPatientService.patientList;
      notifyListeners();
    }
  }

  getOutPatient(PatientSearchRequestModel patientSearchRequestModel , {bool isLocalBusy = false}) async {
    if(isLocalBusy) {
      setState(ViewState.BusyLocal);
    } else {
      setState(ViewState.Busy);
    }

    await _outPatientService.getOutPatient(
        patientSearchRequestModel);
    if (_outPatientService.hasError) {
      error = _outPatientService.error;
      if(isLocalBusy) {
        setState(ViewState.ErrorLocal);
      } else {
        setState(ViewState.Error);
      }
      setState(ViewState.Error);
    } else {
      filterData = _outPatientService.patientList;
      setState(ViewState.Idle);
    }
  }

  getPatientFileInformation(PatientSearchRequestModel patientSearchRequestModel, {bool isLocalBusy = false}) async {
    setState(ViewState.Busy);
    await _outPatientService.getPatientFileInformation(
        patientSearchRequestModel);
    if (_outPatientService.hasError) {
      error = _outPatientService.error;
      setState(ViewState.Error);
    } else {
      filterData = _outPatientService.patientList;
      setState(ViewState.Idle);
    }
  }



  getPatientBasedOnDate (
      {item, PatientSearchRequestModel patientSearchRequestModel, PatientType selectedPatientType,
        bool isSearchWithKeyInfo })async {
    String dateTo;
    String dateFrom;
    if (item == 'Tomorrow') {
      dateTo = DateUtils.convertDateToFormat(
          DateTime(DateTime.now().year, DateTime.now().month,
              DateTime.now().day + 1),
          'yyyy-MM-dd');
      dateFrom = DateUtils.convertDateToFormat(
          DateTime(DateTime.now().year, DateTime.now().month,
              DateTime.now().day + 1),
          'yyyy-MM-dd');
    } else if (item == 'Next Week') {


      dateTo = DateUtils.convertDateToFormat(
          DateTime(DateTime.now().year, DateTime.now().month,
              DateTime.now().day + 6),
          'yyyy-MM-dd');

      dateFrom = DateUtils.convertDateToFormat(
          DateTime(DateTime.now().year, DateTime.now().month,
              DateTime.now().day + 1),
          'yyyy-MM-dd');
    } else {
      dateFrom = DateUtils.convertDateToFormat(
          DateTime(
              DateTime.now().year, DateTime.now().month, DateTime.now().day),
          'yyyy-MM-dd');
      dateTo= DateUtils.convertDateToFormat(
          DateTime(
              DateTime.now().year, DateTime.now().month, DateTime.now().day),
          'yyyy-MM-dd');
    }
    PatientSearchRequestModel currentModel = PatientSearchRequestModel();
    currentModel.patientID =  patientSearchRequestModel.patientID;
    currentModel.firstName =  patientSearchRequestModel.firstName;
    currentModel.lastName =  patientSearchRequestModel.lastName;
    currentModel.middleName = patientSearchRequestModel.middleName;
    currentModel.from = dateFrom;
    currentModel.to = dateTo;


    if(isSearchWithKeyInfo) {
      await getPatientFileInformation(currentModel);
    } else {
        await getOutPatient(currentModel, isLocalBusy: true);
      }
    filterData = _outPatientService.patientList;

  }



  PatientInPatientService _inPatientService =
  locator<PatientInPatientService>();

  List<PatiantInformtion> get _inPatientList => _inPatientService.inPatientList;
  List<PatiantInformtion> filteredInPatientItems = List();

  Future getInPatientList(PatientSearchRequestModel requestModel,
      {bool isMyInpatient = false, bool isFirstTime = true}) async {
    if (isFirstTime)
      setState(ViewState.Busy);
    else
      setState(ViewState.BusyLocal);
    await _inPatientService.getInPatientList(requestModel, isMyInpatient);
    if (_inPatientService.hasError) {
      error = _inPatientService.error;
      setState(ViewState.Error);
    } else {
      filteredInPatientItems.clear();
      if (_inPatientList.length > 0)
        filteredInPatientItems.addAll(_inPatientList);
      setState(ViewState.Idle);
    }
  }

  void filterSearchResults(String query) {
    var strExist = query.length > 0 ? true : false;
    if (strExist) {
      filteredInPatientItems = [];
      for (var i = 0; i < _inPatientList.length; i++) {
        String firstName = _inPatientList[i].firstName.toUpperCase();
        String lastName = _inPatientList[i].lastName.toUpperCase();
        String mobile = _inPatientList[i].mobileNumber.toUpperCase();
        String patientID = _inPatientList[i].patientId.toString();

        if (firstName.contains(query.toUpperCase()) ||
            lastName.contains(query.toUpperCase()) ||
            mobile.contains(query) ||
            patientID.contains(query)) {
          filteredInPatientItems.add(_inPatientList[i]);
        }
      }
      notifyListeners();
    } else {
      if (_inPatientList.length > 0)
        filteredInPatientItems.clear();
        filteredInPatientItems.addAll(_inPatientList);
      notifyListeners();
    }
  }


}
