import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/pharmacies/LacumAccountInformation.dart';
import 'package:hmg_patient_app/core/service/parmacyModule/lacum-service.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';

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

class LacumViewModel extends BaseViewModel {
  LacumService _lacumService = locator<LacumService>();

  LacumAccountInformation? get lacumInformation => _lacumService.lacumInformation;

  LacumAccountInformation? get lacumGroupInformation => _lacumService.lacumGroupInformation;

  String get successMsg => _lacumService.successMsg;

  Future getLacumData(String identificationNo) async {
    await getLacumAccountData(identificationNo);
    getLacumGroupData(identificationNo);
  }

  Future getLacumAccountData(String identificationNo) async {
    setState(ViewState.Busy);
    await _lacumService.getLacumAccountInformation(identificationNo);
    if (_lacumService.hasError) {
      error = _lacumService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getLacumGroupData(String identificationNo) async {
    setState(ViewState.Busy);
    await _lacumService.getLacumGroupInformation(identificationNo);
    if (_lacumService.hasError) {
      error = _lacumService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future makeAccountActivate(String identificationNo) async {
    setState(ViewState.Busy);
    await _lacumService.makeAccountActivate();
    if (_lacumService.hasError) {
      error = _lacumService.error!;
      setState(ViewState.Error);
    } else {
      lacumGroupInformation?.lakumInquiryInformationObjVersion?.accountStatus = "Active";
      getLacumAccountData(identificationNo);
    }
  }

  Future makeAccountDeactivate(String identificationNo) async {
    setState(ViewState.Busy);
    await _lacumService.makeAccountDeactivate();
    if (_lacumService.hasError) {
      error = _lacumService.error!;
      setState(ViewState.Error);
    } else {
      lacumGroupInformation!.lakumInquiryInformationObjVersion!.accountStatus = "Cancelled";
      getLacumAccountData(identificationNo);
    }
  }

  Future createLakumAccount(String name, String phone) async {
    setState(ViewState.BusyLocal);
    await _lacumService.createLakumAccount(name, phone, lacumInformation!.yahalaAccountNo!, lacumInformation!.identificationNo!, null);
    if (_lacumService.hasError) {
      error = _lacumService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  String formatCreatedDateToString() {
    DateTime createdDate = DateUtil.convertStringToDate(lacumGroupInformation!.lakumInquiryInformationObjVersion!.createdDate!);
    return DateUtil.getFormattedDate(createdDate, "MMM dd,yyyy");
  }

  Uint8List convertBase64ToBarCodeImage() {
    try {
      final _byteImage = base64Decode(lacumGroupInformation!.lakumInquiryInformationObjVersion!.barCode!.split(',').last);
      return _byteImage;
    } catch (e) {
      print(e);
      return Uint8List(0);
    }
  }

  setLakumData(LacumAccountInformation lacumInformation, LacumAccountInformation lacumGroupInformation) {
    _lacumService.lacumInformation = lacumInformation;
    _lacumService.lacumGroupInformation = lacumGroupInformation;
  }

  clearLakumGroupData() {
    _lacumService.lacumGroupInformation = null;
  }
}
