import 'dart:typed_data';

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

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;

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

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

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

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

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

  Future<int> createLakumAccount(String name, String phone) async {
    setState(ViewState.Busy);
    await _lacumService.createLakumAccount(name, phone, lacumInformation.yahalaAccountNo, lacumInformation.identificationNo, null);
    if (_lacumService.hasError) {
      error = _lacumService.error;
      setState(ViewState.Error);
      return 404;
    } else {
      setState(ViewState.Idle);
      return 200;
    }
  }

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

  Uint8List convertBase64ToBarCodeImage() {
    try {
      final _byteImage = convert.base64Decode(
          lacumGroupInformation.lakumInquiryInformationObjVersion.barCode);
      return _byteImage;
    } catch (e) {
      print(e);
      return null;
    }
  }

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

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