// ------------------------------------------------------------------------------
// DPF.iOS.MFMailCompose Component
//
// Dadeh Pardazane Faragir ( DPF ) Co.
//
// Web: http://www.dpfaragir.com
//
// Developed By: Babak Yaghoobi
//
// Email #1: yaghoobi@dpfaragir.com
// Email #2: b_yaghobi@yahoo.com
//
// ------------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ------------------------------------------------------------------------------
unit DPF.iOS.MFMailCompose;

interface

{$I DPF.iOS.Defs.inc}

uses
  System.SysUtils,
  System.Classes,
  System.Types,
  System.UITypes,
  System.Variants,

  System.TypInfo,
  DPF.iOS.BaseControl,
  DPF.iOS.UIImageView,
{$IFDEF IOS}
  Macapi.ObjectiveC,
  Macapi.ObjCRuntime,
  IOSapi.CocoaTypes,
  IOSapi.Foundation,
  IOSapi.Uikit,
  IOSapi.CoreGraphics,
  IOSapi.CoreImage,
  FMX.Platform.iOS,
  DPF.iOS.Common,
  DPF.iOS.MFMailComposeViewController,
{$ENDIF}
  FMX.Layouts, FMX.Memo,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls;

type

  TDPFMailCompose = class;

  TDPFOnSendStatus = procedure( Sender: TObject; Result: Integer ) of object;
{$IFDEF IOS}

  // ------------------------------------------------------------------------------
  TMFMailComposeViewControllerDelegate = class( TOCLocal, MFMailComposeViewControllerDelegate )
  private
    FDPFMailCompose: TDPFMailCompose;
  public
    constructor Create( DPFMailCompose: TDPFMailCompose );
    procedure mailComposeController( controller: MFMailComposeViewController; didFinishWithResult: MFMailComposeResult; error: NSError ); cdecl;
  end;
{$ENDIF}

  // ------------------------------------------------------------------------------
  [ComponentPlatformsAttribute( PidWin32 or PidWin64 or pidLinux32 or pidAndroid or PidOSX32 or PidiOSDevice or PidiOSSimulator )]
  TDPFMailCompose = class( TComponent )
  private
    FOnSendStatus: TDPFOnSendStatus;
{$IFDEF IOS}
    FMainWindow                         : UIWindow;
    FMailCompose                        : MFMailComposeViewController;
    FMFMailComposeViewControllerDelegate: TMFMailComposeViewControllerDelegate;
    RecipientsArr                       : NSMutableArray;
    CCRecipientsArr                     : NSMutableArray;
    BCCRecipientsArr                    : NSMutableArray;
{$ENDIF}
    function GetDPFVersion: string;
  protected
  public
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
    function MailCompose( Subject: string; MessageBody: string; MessageBodyisHtml: Boolean; Recipients: array of string; CCRecipients: array of string; BCCRecipients: array of string; AttachedFiles: array of string; ImageView: TDPFImageView = nil; ImageViewName:String='DPF-Photo.png' ): Boolean;
  published
    property OnSendStatus: TDPFOnSendStatus read FOnSendStatus write FOnSendStatus;
    property Version     : string read GetDPFVersion;
  end;

  // ------------------------------------------------------------------------------

implementation

// ------------------------------------------------------------------------------
{ TDPFMailCompose }
// ------------------------------------------------------------------------------
constructor TDPFMailCompose.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
end;

// ------------------------------------------------------------------------------
destructor TDPFMailCompose.Destroy;
begin
{$IFDEF IOS}
  if Assigned( RecipientsArr ) then
    RecipientsArr.release;

  if assigned( CCRecipientsArr ) then
    CCRecipientsArr.release;

  if Assigned( BCCRecipientsArr ) then
    BCCRecipientsArr.release;

  if Assigned( FMFMailComposeViewControllerDelegate ) then
    FMFMailComposeViewControllerDelegate.DisposeOf;

  if Assigned( FMailCompose ) then
    FMailCompose.release;
{$ENDIF}
  inherited;
end;

function TDPFMailCompose.GetDPFVersion: string;
begin
  Result := GetDPFVersion;
end;

// ------------------------------------------------------------------------------
function TDPFMailCompose.MailCompose( Subject: string; MessageBody: string; MessageBodyisHtml: Boolean; Recipients: array of string; CCRecipients: array of string; BCCRecipients: array of string; AttachedFiles: array of string; ImageView: TDPFImageView = nil; ImageViewName:String='DPF-Photo.png' ): Boolean;
{$IFNDEF IOS}
begin
  result := false;
end;
{$ELSE}

var
  I        : Integer;
  AttFile  : NSData;
  imageData: NSData;
begin
  RecipientsArr                        := TNSMutableArray.Create;
  CCRecipientsArr                      := TNSMutableArray.Create;
  BCCRecipientsArr                     := TNSMutableArray.Create;
  FMFMailComposeViewControllerDelegate := TMFMailComposeViewControllerDelegate.Create( Self );

  FMailCompose := TMFMailComposeViewController.Create;
  FMailCompose.setMailComposeDelegate( FMFMailComposeViewControllerDelegate );

  Result := { FMailCompose.canSendMail } True;
  if not Result then
  begin
    Exit;
  end;

  FMailCompose.setSubject( NSStr( Subject ) );
  FMailCompose.setMessageBody( NSStr( MessageBody ), MessageBodyisHtml );

  // Recipients
  if Length( Recipients ) > 0 then
  begin
    RecipientsArr := TNSMutableArray.Create;
    for I         := 0 to high( Recipients ) do
      RecipientsArr.addObject( ( NSStr( Recipients[I] ) as ILocalObject ).GetObjectID );
    FMailCompose.setToRecipients( RecipientsArr );
  end;

  // CC Recipients
  if Length( CCRecipients ) > 0 then
  begin
    CCRecipientsArr := TNSMutableArray.Create;
    for I           := 0 to high( CCRecipients ) do
      CCRecipientsArr.addObject( ( NSStr( CCRecipients[I] ) as ILocalObject ).GetObjectID );
    FMailCompose.setCcRecipients( RecipientsArr );
  end;

  // BCC Recipients
  if Length( BCCRecipients ) > 0 then
  begin
    for I := 0 to high( BCCRecipients ) do
      BCCRecipientsArr.addObject( ( NSStr( BCCRecipients[I] ) as ILocalObject ).GetObjectID );
    FMailCompose.setBccRecipients( BCCRecipientsArr );
  end;

  // Attached Files

  if Assigned( ImageView ) then
  begin
    imageData := TNSData.Wrap( UIImagePNGRepresentation( ( ImageView.GetUIImage as ILocalObject ).GetObjectID ) );
    FMailCompose.addAttachmentData( imageData, NSStr( 'image/png' ), NSStr( ImageViewName ) );
  end;

  if Length( AttachedFiles ) > 0 then
  begin
    for I := 0 to high( AttachedFiles ) do
      if FileExists( AttachedFiles[I] ) then
      begin
        AttFile := TNSData.Wrap( TNSData.OCClass.dataWithContentsOfFile( NSStr( AttachedFiles[I] ) ) );
        FMailCompose.addAttachmentData( AttFile, NSStr( GetFileMIMEType( AttachedFiles[I] ) ), NSStr( ExtractFileName( AttachedFiles[I] ) ) );
      end;
  end;

  FMainWindow := GetSharedApplication.keyWindow;
  if Assigned( FMainWindow ) and Assigned( FMainWindow.rootViewController ) then
    FMainWindow.rootViewController.presentModalViewController( FMailCompose, True );

end;
// ------------------------------------------------------------------------------
{ TMFMailComposeViewControllerDelegate }

constructor TMFMailComposeViewControllerDelegate.Create( DPFMailCompose: TDPFMailCompose );
begin
  inherited Create;
  FDPFMailCompose := DPFMailCompose;
end;

// ------------------------------------------------------------------------------
procedure TMFMailComposeViewControllerDelegate.mailComposeController( controller: MFMailComposeViewController; didFinishWithResult: MFMailComposeResult; error: NSError );
begin

  case didFinishWithResult of
    MFMailComposeResultCancelled:
      ;
    MFMailComposeResultSaved:
      ;
    MFMailComposeResultSent:
      ;
    MFMailComposeResultFailed:
      ;
  end;
  FDPFMailCompose.FMainWindow.rootViewController.dismissModalViewControllerAnimated( True );
  if Assigned( FDPFMailCompose.FOnSendStatus ) then
    FDPFMailCompose.FOnSendStatus( FDPFMailCompose, didFinishWithResult );

  FDPFMailCompose.DisposeOf;
end;
{$ENDIF}

// ------------------------------------------------------------------------------
end.
