#if UNITY_ANDROID
/*
* PubMatic Inc. ("PubMatic") CONFIDENTIAL
* Unpublished Copyright (c) 2006-2022 PubMatic, All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of PubMatic. The intellectual and technical concepts contained
* herein are proprietary to PubMatic and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained
* from PubMatic.  Access to the source code contained herein is hereby forbidden to anyone except current PubMatic employees, managers or contractors who have executed
* Confidentiality and Non-disclosure agreements explicitly covering such access or to such other persons whom are directly authorized by PubMatic to access the source code and are subject to confidentiality and nondisclosure obligations with respect to the source code.
*
* The copyright notice above does not evidence any actual or intended publication or disclosure  of  this source code, which includes
* information that is confidential and/or proprietary, and is a trade secret, of  PubMatic.   ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
* OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT  THE EXPRESS WRITTEN CONSENT OF PUBMATIC IS STRICTLY PROHIBITED, AND IN VIOLATION OF APPLICABLE
* LAWS AND INTERNATIONAL TREATIES.  THE RECEIPT OR POSSESSION OF  THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY ANY RIGHTS
* TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
*/
using System;
using OpenWrapSDK.Common;
using UnityEngine;
namespace OpenWrapSDK.Android
{
    /// 
    /// Android client of Interstitial ad which communicates with java native code
    /// 
    internal class POBInterstitialClient : AndroidJavaProxy, IPOBInterstitialClient
    {
        #region Private variables
        private readonly string Tag = "POBInterstitialClient";
        private readonly AndroidJavaObject androidInterstitialAd;
        #endregion
        /// 
        /// Constructor
        /// 
        /// OpenWrap's publisher id
        /// OpenWrap's profile id
        /// OpenWrap's ad unit id
        #region Constructors/Destructor
        internal POBInterstitialClient(string owPublisherId, int owProfileId, string owAdUnitId) : base(POBConstants.POBInterstitialAdCallbackInterfaceName)
        {
            AndroidJavaObject activityContext = POBAndroidUtils.getActivity();
            this.androidInterstitialAd = new AndroidJavaObject(POBConstants.POBInterstitialClassName, activityContext, owPublisherId, owProfileId, owAdUnitId, this);
            // Initialize the event dispatcher 
            POBEventsDispatcher.Initialize();
        }
        /// 
        /// Destructor
        /// 
        ~POBInterstitialClient()
        {
            Destroy();
        }
        #endregion
        #region IPOBInterstitialClient APIs
        /// 
        /// Callback method notifies that an ad has been received successfully.
        /// 
        public event EventHandler OnAdLoaded;
        /// 
        /// Callback method notifies an error encountered while loading or rendering an ad.
        /// 
        public event EventHandler OnAdFailedToLoad;
        /// 
        /// Callback method notifies an error encountered while showing an ad.
        /// 
        public event EventHandler OnAdFailedToShow;
        /// 
        /// Callback method notifies that a user interaction will open another app, leaving the current app.
        /// 
        public event EventHandler OnAppLeaving;
        /// 
        /// Callback method notifies that the interstitial ad will be presented as a modal on tops
        /// 
        public event EventHandler OnAdOpened;
        /// 
        /// Callback method notifies that the interstitial ad has been animated off the screen.
        /// 
        public event EventHandler OnAdClosed;
        /// 
        /// Callback method notifies that the interstitial ad has been clicked
        /// 
        public event EventHandler OnAdClicked;
        /// 
        /// Callback method notifies that the interstitial ad has expired
        /// 
        public event EventHandler OnAdExpired;
        /// 
        /// Callback method notifies that the interstitial video playback have completed.
        /// 
        public event EventHandler OnVideoPlaybackCompleted;
        /// 
        /// Method to get the interstitial bid
        /// 
        public IPOBBid GetBid()
        {
            return new POBBidClient(this.androidInterstitialAd.Call("getBid"));
        }
        /// 
        /// Method to get the interstitial impression
        /// 
        /// Instance of type IPOBImpression
        public IPOBImpression GetImpression()
        {
            return new POBImpressionClient(this.androidInterstitialAd.Call("getImpression"));
        }
        /// 
        /// Method to get the interstitial request
        /// 
        /// Instance of type IPOBRequest
        public IPOBRequest GetRequest()
        {
            return new POBRequestClient(this.androidInterstitialAd.Call("getAdRequest"));
        }
        /// 
        /// Method to check if ad is ready
        /// 
        /// is ready status
        public bool IsReady()
        {
            POBLog.Info(Tag, POBLogStrings.ClientIsReadyLog);
            return this.androidInterstitialAd.Call("isReady");
        }
        /// 
        /// Method to load interstitial ad 
        /// 
        public void LoadAd()
        {
            POBLog.Info(Tag, POBLogStrings.ClientLoadAdLog);
            this.androidInterstitialAd.Call("loadAd");
        }
        /// 
        /// Method to show interstitial ad
        /// 
        public void ShowAd()
        {
            POBLog.Info(Tag, POBLogStrings.ClientShowAdLog);
            this.androidInterstitialAd.Call("show");
        }
        /// 
        ///  Method to Destroy interstitial ad
        /// 
        public void Destroy()
        {
            POBLog.Info(Tag, POBLogStrings.ClientDestroyLog);
            this.androidInterstitialAd.Call("destroy");
        }
        #endregion
        #region Callbacks from Unity
        /// 
        /// Notifies the listener that an ad has been successfully loaded and rendered.
        /// 
        public void onAdLoaded()
        {
            if (OnAdLoaded != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdLoaded(this, EventArgs.Empty);
                });
                
            }
        }
        /// 
        /// Notifies the listener of an error encountered while loading an ad.
        /// 
        /// POBError instance
        public void onAdFailedToLoad(AndroidJavaObject error)
        {
            if (OnAdFailedToLoad != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdFailedToLoad(this, POBAndroidUtils.ConvertToPOBErrorEventArgs(error));
                });
                
            }
        }
        /// 
        /// Notifies the listener of an error encountered while rendering an ad.
        /// 
        /// POBError instance
        public void onAdFailedToShow(AndroidJavaObject error)
        {
            if (OnAdFailedToShow != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdFailedToShow(this, POBAndroidUtils.ConvertToPOBErrorEventArgs(error));
                });
                
            }
        }
        /// 
        /// Notifies the listener whenever current app goes in the background due to user click
        /// 
        public void onAppLeaving()
        {
            if (OnAppLeaving != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAppLeaving(this, EventArgs.Empty);
                });
                
            }
        }
        /// 
        /// Notifies that the OpenWrap view will open an ad on top of the current view.
        /// 
        public void onAdOpened()
        {
            if (OnAdOpened != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdOpened(this, EventArgs.Empty);
                });
                
            }
        }
        /// 
        /// Notifies that the OpenWrap view has closed the ad on top of the current view.
        /// 
        public void onAdClosed()
        {
            if (OnAdClosed != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdClosed(this, EventArgs.Empty);
                });
                
            }
        }
        /// 
        /// Notifies that the user has clicked the ad view.
        /// 
        public void onAdClicked()
        {
            if (OnAdClicked != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdClicked(this, EventArgs.Empty);
                });
                
            }
        }
        /// 
        /// Notifies the listener that an ad has been expired
        /// 
        public void onAdExpired()
        {
            if (OnAdExpired != null)
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnAdExpired(this, EventArgs.Empty);
                });
                
            }
        }
        /// 
        /// Notifies the listener that the playback of the video ad has been completed.
        /// 
        public void onVideoPlaybackCompleted()
        {
            if (OnVideoPlaybackCompleted != null) 
            {
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnVideoPlaybackCompleted(this, EventArgs.Empty);
                });
                
            }
        }
#endregion
    }
}
#endif