﻿using UnityEngine;
using InMobiAds.Api;
using System;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public class InmobiIntegration : MonoBehaviour
{
    public InputField placementId;

    public InputField imLogs;

    private BannerAd bannerAd;

    public Toggle enableAutoRefreshForBanner;


    private InterstitialAd interstitialAd;
    private RewardedVideoAd rewardedVideoAd;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void CloseVC()
    {
        SceneManager.LoadScene("DemoAppStart");
    }

    public void OpenUnifiedID()
    {
        SceneManager.LoadScene("UnifiedID");
    }

    public void InitializeInMobiAds()
    {
        UpdateLogs("initializeInMobiAds ");

        InMobiPlugin inmobiPlugin = new InMobiPlugin();

        inmobiPlugin.OnInitializationCompleted += this.HandleInitializationStatus;
        Dictionary<string, object> consentObject = new Dictionary<string, object>();
        consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_AVAILABLE, true);
        consentObject.Add(InMobiPlugin.IM_GDPR_CONSENT_IAB, "IAB_STRING");

        inmobiPlugin.Init("e60f046776294b1296928987facfcb2f", consentObject);
        inmobiPlugin.SetLogLevel("Debug");
        inmobiPlugin.SetAge(23);
    }

    public void HandleInitializationStatus (object sender, SdkInitializationStatusArgs args) {
        UpdateLogs("SDK Initialization status code : " + args.status.Code + " and reason : " + args.status.Reason);
    }

    public void requestAerservBanner()
    {
        UpdateLogs("New banner has been requested ");

        String plId;

        if (placementId.text != "")
        {
            plId = placementId.text;
        }
        else
        {
#if UNITY_ANDROID
		 plId = "1467162141987";
#elif UNITY_IPHONE
            plId = "1464947431995";
#endif
        }

        Debug.Log(plId + "placement id");


        // Create a 320x50 banner at the top of the screen.
        this.bannerAd = new BannerAd(plId, 320, 50, (int)InMobiAdPosition.TopCenter);


        // Register for ad events.
        this.bannerAd.OnAdLoadSucceeded += this.HandleOnAdLoadSucceeded;
        this.bannerAd.OnAdLoadFailed += this.HandleAdLoadFailed;
        this.bannerAd.OnAdDisplayed += this.HandleAdDisplayed;
        this.bannerAd.OnAdDismissed += this.HandleAdDismissed;
        this.bannerAd.OnAdInteraction += this.HandleAdInteraction;
        this.bannerAd.OnUserLeftApplication += this.HandleUserLeftApplication;
        this.bannerAd.SetEnableAutoRefresh(enableAutoRefreshForBanner.isOn);
        this.bannerAd.SetKeywords("KeyWord1,KeyWord2");
        this.bannerAd.SetRefreshInterval(30);
        // Load a banner ad.
        this.bannerAd.LoadAd();
    }

    public void DesTroyBanner()
    {
        UpdateLogs("Banner has been destroyed");
        this.bannerAd.DestroyAd();
    }



    public void RequestInterstitial()
    {
        UpdateLogs("New Interstitial has been requested ");
        // These ad units are configured to always serve test ads.


        String plId;

        if (placementId.text != "")
        {
            plId = placementId.text;
        }
        else
        {
#if UNITY_ANDROID
		plId = "1469137441636";
#elif UNITY_IPHONE
            plId = "1467548435003";
#endif
        }


        // Create an interstitial.
        this.interstitialAd = new InterstitialAd(plId);

        // Register for ad events.
        this.interstitialAd.OnAdLoadSucceeded += this.HandleOnAdLoadSucceeded;
        this.interstitialAd.OnAdLoadFailed += this.HandleAdLoadFailed;
        this.interstitialAd.OnAdDisplayed += this.HandleAdDisplayed;
        this.interstitialAd.OnAdDismissed += this.HandleAdDismissed;
        this.interstitialAd.OnAdInteraction += this.HandleAdInteraction;
        this.interstitialAd.OnUserLeftApplication += this.HandleUserLeftApplication;

        this.interstitialAd.OnAdReceived += this.HandleAdReceived;
        this.interstitialAd.OnAdDisplayFailed += this.HandleAdDisplayFailed;
        this.interstitialAd.OnAdWillDisplay += this.HandleAdWillDisplay;

        // Load an interstitial ad.
        this.interstitialAd.LoadAd();
    }


    public void RequestRewardBasedVideo()
    {
        UpdateLogs("New RequestRewardBasedVideo has been requested ");

        String plId;

        if (placementId.text != "")
        {
            plId = placementId.text;
        }
        else
        {
#if UNITY_ANDROID
		plId = "1465237901291";
#elif UNITY_IPHONE
            plId = "1465883204802";
#endif
        }

        this.rewardedVideoAd = new RewardedVideoAd(plId);

        // Register for ad events.
        this.rewardedVideoAd.OnAdLoadSucceeded += this.HandleOnAdLoadSucceeded;
        this.rewardedVideoAd.OnAdLoadFailed += this.HandleAdLoadFailed;
        this.rewardedVideoAd.OnAdDisplayed += this.HandleAdDisplayed;
        this.rewardedVideoAd.OnAdDismissed += this.HandleAdDismissed;
        this.rewardedVideoAd.OnAdInteraction += this.HandleAdInteraction;
        this.rewardedVideoAd.OnUserLeftApplication += this.HandleUserLeftApplication;

        this.rewardedVideoAd.OnAdReceived += this.HandleAdReceived;
        this.rewardedVideoAd.OnAdDisplayFailed += this.HandleAdDisplayFailed;
        this.rewardedVideoAd.OnAdWillDisplay += this.HandleAdWillDisplay;

        this.rewardedVideoAd.OnAdRewardActionCompleted += this.HandleRewardActionCompleted;

        this.rewardedVideoAd.LoadAd();
    }

    public void ShowInterstitial()
    {
        if (this.interstitialAd.isReady())
        {
            UpdateLogs("InterstitialAd is ready to show");
            this.interstitialAd.Show();
        }
        else
        {
            UpdateLogs("InterstitialAd is not ready to show");
        }
    }

    public void ShowRewardBasedVideo()
    {
        if (this.rewardedVideoAd.isReady())
        {
            UpdateLogs("rewardedVideoAd is ready to show");
            this.rewardedVideoAd.Show();
        }
        else
        {
            UpdateLogs("rewardedVideoAd is not ready to show");
        }
    }



    #region callback handlers

    public void HandleOnAdLoadSucceeded(object sender, EventArgs args)
    {

        UpdateLogs("AdLoadSucceeded");
    }

    public void HandleAdLoadFailed(object sender, AdLoadFailedEventArgs args)
    {
        UpdateLogs("AdLoadFailed" + args.Error);
    }

    public void HandleAdDisplayed(object sender, EventArgs args)
    {
        UpdateLogs("AdDisplayed");
    }

    public void HandleAdDismissed(object sender, EventArgs args)
    {
        UpdateLogs("AdDismissed");
    }

    public void HandleAdInteraction(object sender, AdInteractionEventArgs args)
    {
        UpdateLogs("AdInteraction");
    }

    public void HandleUserLeftApplication(object sender, EventArgs args)
    {
        UpdateLogs("UserLeftApplication");
    }

    #endregion

    #region Interstitial specific callback handlers

    public void HandleAdReceived(object sender, EventArgs args)
    {
        UpdateLogs("AdReceived");
    }

    public void HandleAdWillDisplay(object sender, EventArgs args)
    {
        UpdateLogs("AdWillDisplay");
    }

    public void HandleAdDisplayFailed(object sender, EventArgs args)
    {
        UpdateLogs("AdDisplayFailed");
    }



    #endregion


    #region RewardBasedVideo callback handlers


    public void HandleRewardActionCompleted(object sender, AdRewardActionCompletedEventArgs args)
    {
        UpdateLogs("RewardActionCompleted" + args.Rewards);
    }

    #endregion

    private void UpdateLogs(String logs)
    {
        UnityMainThreadHelper.GetInstance().PostTask(()=> {
            MonoBehaviour.print("Event logged: " + logs);
            String updated = imLogs.text + " \n " + logs;
            imLogs.text = updated;
        });
    }

    private void ClearLogs() {
        String updated = "";
        imLogs.text = updated;
    }

    public void toggleAutoRefresh()
    {
        this.bannerAd.SetEnableAutoRefresh(enableAutoRefreshForBanner.isOn);
    }

}
