//---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop

#include "Main.h"
#include <System.Permissions.hpp>
#include <Androidapi.Helpers.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner): TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
  BarcodeDecoder = new TBarcodeDecoder();

  DynamicArray<String> permissions;
  permissions.Length = 3;
  permissions[0] = JStringToString(TJManifest_permission::JavaClass->CAMERA);
  permissions[1] = JStringToString(TJManifest_permission::JavaClass->READ_EXTERNAL_STORAGE);
  permissions[2] = JStringToString(TJManifest_permission::JavaClass->WRITE_EXTERNAL_STORAGE);

  PermissionsService()->RequestPermissions(permissions,
    [this](const DynamicArray<String> APermissions, const DynamicArray<TPermissionStatus> AGrantResults)
    {
      ButtonTakePhoto->Enabled = (AGrantResults.Length == 3)
        && (AGrantResults[0] == TPermissionStatus::Granted)
        && (AGrantResults[1] == TPermissionStatus::Granted)
        && (AGrantResults[2] == TPermissionStatus::Granted);
    });
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormDestroy(TObject *Sender)
{
  delete BarcodeDecoder;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TakePhotoFromCameraActionDidCancelTaking()
{
  Memo->Lines->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TakePhotoFromCameraActionDidFinishTaking(TBitmap *Image)
{
  Memo->Lines->Clear();
  ImageViewer->Bitmap->Assign(Image);
  TDecodeResults Barcodes = BarcodeDecoder->Decode(Image);
  for (int I = 0; I < Barcodes.Length; ++I)
    Memo->Lines->Append(Barcodes[I].FormatName + ": " + Barcodes[I].Text);
}
//---------------------------------------------------------------------------

