#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 RewardedAd which communicates with java native code
    /// 
    internal class POBRewardedAdClient: AndroidJavaProxy, IPOBRewardedAdClient
    {
        #region Private variables
        private readonly string Tag = "POBRewardedAdClient";
        private readonly AndroidJavaObject androidRewardedAd;
        #endregion
        #region Constructors/Destructor
        /// Private constructor to avoid direct instance creation, To create instance of
        /// POBRewardedAdClient use POBRewardedAdClient.GetRewardedAdClient() method.
        /// 
        ///  Valid instance of AndroidJavaObject which is proxy object of
        /// com.pubmatic.unity.openwrapsdk.POBUnityRewardedAd 
        private POBRewardedAdClient(AndroidJavaObject androidRewardedAd) : base(POBConstants.POBRewardedAdCallbackInterfaceName)
        {           
            this.androidRewardedAd = androidRewardedAd;
            // Set listener to receive Rewarded Ad event calls
            this.androidRewardedAd.Call("setListener", this);
            // Initialize the event dispatcher
            POBEventsDispatcher.Initialize();
        }
        /// 
        /// Destructor
        /// 
        ~POBRewardedAdClient()
        {
            Destroy();
        }
        #endregion
        /// Returns POBRewardedAdClient instance if all the params are valid else it would return
        /// null
        /// 
        /// 
        /// 
        /// 
        /// 
        public static POBRewardedAdClient GetRewardedAdClient(string publisherId, int profileId, string adUnitId)
        {
            // Get activity instance
            AndroidJavaObject activity = POBAndroidUtils.getActivity();
            // Android Java class for RewardedAd ad
            AndroidJavaClass androidRewardedAdClass = new AndroidJavaClass(POBConstants.POBRewardedAdClassName);
            // Get valid instance of RewardedAd using getInstance native method.
            AndroidJavaObject androidRewardedAd = androidRewardedAdClass.CallStatic("getInstance",
                activity, publisherId, profileId, adUnitId);
            if (androidRewardedAd != null){
                return new POBRewardedAdClient(androidRewardedAd);
            }
            return null;
        }
        /// 
        /// 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 (for example, App Store), leaving the current app.
        /// 
        public event EventHandler OnAppLeaving;
        /// 
        /// Callback method notifies that the rewarded ad will be presented as a modal on top of the current view controller
        /// 
        public event EventHandler OnAdOpened;
        /// 
        /// Callback method notifies that the rewarded ad has been animated off the screen.
        /// 
        public event EventHandler OnAdClosed;
        /// 
        /// Callback method notifies that the rewarded ad has been clicked
        /// 
        public event EventHandler OnAdClicked;
        /// 
        /// Callback method notifies that the rewarded ad has expired
        /// 
        public event EventHandler OnAdExpired;
        /// 
        /// Callback method notifies when an Ad has completed the minimum required viewing, and user should be rewarded
        /// 
        public event EventHandler OnReceiveReward;
        #region IPOBRewardedAdClient APIs
        /// 
        /// Getter for OpenWrap rewarded ad POBBid
        /// 
        /// Instance of POBBid
        public IPOBBid GetBid()
        {
            return new POBBidClient(this.androidRewardedAd.Call("getBid"));
        }
        /// 
        /// To get the rewarded ad impression
        /// 
        /// Instance of type IPOBImpression
        public IPOBImpression GetImpression()
        {
            return new POBImpressionClient(this.androidRewardedAd.Call("getImpression"));
        }
        /// 
        /// To get the rewarded ad request
        /// 
        /// Instance of type IPOBRequest
        public IPOBRequest GetRequest()
        {
            return new POBRequestClient(this.androidRewardedAd.Call("getAdRequest"));
        }
        /// 
        /// To check if ad is ready to show
        /// 
        /// is ready status
        public bool IsReady()
        {
            POBLog.Info(Tag, POBLogStrings.ClientIsReadyLog);
            return this.androidRewardedAd.Call("isReady");
        }
        /// 
        /// To load rewarded ad 
        /// 
        public void LoadAd()
        {
            POBLog.Info(Tag, POBLogStrings.ClientLoadAdLog);
            this.androidRewardedAd.Call("loadAd");
        }
        /// 
        /// To show rewarded ad
        /// 
        public void ShowAd()
        {
            POBLog.Info(Tag, POBLogStrings.ClientShowAdLog);
            this.androidRewardedAd.Call("show");
        }
        /// 
        /// Method to set skip alert info
        /// 
        /// skip alert title
        /// skip alert message
        /// close button title for skip alert
        /// resume button title for skip alert
        public void SetSkipAlertInfo(string title, string message, string closeTitle, string resumeTitle)
        {
            this.androidRewardedAd.Call("setSkipAlertDialogInfo", title, message, resumeTitle, closeTitle);
        }
        /// 
        ///  To clean up rewarded ad
        /// 
        public void Destroy()
        {
            POBLog.Info(Tag, POBLogStrings.ClientDestroyLog);
            this.androidRewardedAd.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 when an Ad has completed the minimum required viewing, and user should be rewarded.
        /// 
        /// AndroidJavaObject of POBReward
        public void onReceiveReward(AndroidJavaObject reward)
        {
            if (OnReceiveReward != null)
            {
                POBRewardEventArgs rewardEventArgs = POBAndroidUtils.GetPOBRewardEventArgs(reward);
                POBEventsDispatcher.ScheduleInUpdate(() => {
                    OnReceiveReward(this, rewardEventArgs);
                });
                
                
            }
        }
        #endregion
    }
}
#endif