using System;
using UnityEngine;

public sealed class AdManager : SingletonMonoBehaviour<AdManager> {

    //-- ironSource --//
#if UNITY_IOS
    private static readonly string IRONSOURCE_APP_KEY = "11bc8b5ad";
#else
    private static readonly string IRONSOURCE_APP_KEY = "11bc928bd";
#endif

    private Action<bool> _rewardAdCallback = null;
    private readonly int ASYNC_FRAME_LIMIT = 60;
    private int _async_frame_count = 0;
    private bool _rewardAdOpen = false;
    private bool _rewardAdClose = false;
    private bool _rewardAdFinish = false;
    private float _timeScale = 0.0f;



    void Awake(){
#if DEVELOPMENT_BUILD
        IronSource.Agent.validateIntegration ();
        IronSource.Agent.setAdaptersDebug(true);
#endif
        IronSource.Agent.init (IRONSOURCE_APP_KEY);
    }

    void Start(){
        LoadBannerAds();
        ShowBannerAd();
    }

    void Update (){
        if(_rewardAdOpen){
            _rewardAdOpen = false;
            pauseGame();
        }else if(_rewardAdClose){
            if(_rewardAdFinish){
                _rewardAdClose = false;
                this.RewardAdCallbackClose(_rewardAdFinish);
            }else if(ASYNC_FRAME_LIMIT > _async_frame_count){
                _async_frame_count++;
            }else{
                _rewardAdClose = false;
                this.RewardAdCallbackClose(_rewardAdFinish);
            }
        }
    }

    void OnApplicationPause(bool isPaused) {
        IronSource.Agent.onApplicationPause(isPaused);
    }

    //-- Banners Ads --//

    public void LoadBannerAds(){
        IronSource.Agent.loadBanner(IronSourceBannerSize.SMART, IronSourceBannerPosition.BOTTOM);

    }
    public void ShowBannerAd(){
        IronSource.Agent.displayBanner();
    }
    public void HideBanner(){
        IronSource.Agent.hideBanner();
    }

    public void InitializeRewardedAds(){
	    IronSourceEvents.onRewardedVideoAdOpenedEvent += RewardedVideoAdOpenedEvent;
    	IronSourceEvents.onRewardedVideoAdClosedEvent += RewardedVideoAdClosedEvent;
    	IronSourceEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvailabilityChangedEvent;
        IronSourceEvents.onRewardedVideoAdStartedEvent += RewardedVideoAdStartedEvent;
        IronSourceEvents.onRewardedVideoAdRewardedEvent += RewardedVideoAdRewardedEvent;
    	IronSourceEvents.onRewardedVideoAdShowFailedEvent += RewardedVideoAdShowFailedEvent;
    }
  	private void RewardedVideoAdOpenedEvent(){
        _rewardAdOpen = true;
    }
	private void RewardedVideoAdClosedEvent(){
        _rewardAdClose = true;
    }
	private void RewardedVideoAvailabilityChangedEvent(bool available){
	}
    private void RewardedVideoAdStartedEvent(){
    }
  	private void RewardedVideoAdRewardedEvent(IronSourcePlacement placement){
        _rewardAdFinish = true;
  	}
	private void RewardedVideoAdShowFailedEvent (IronSourceError error){
        _rewardAdClose = true;
  	}
    public bool IsLoadedRewardVideo(){
#if DEVELOPMENT_BUILD || UNITY_EDITOR
        return !UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugAlwaysVideoFailToLoad", false);
#endif
        return IronSource.Agent.isRewardedVideoAvailable();
    }
    public void ShowRewardVideo(Action<bool> callback){
#if UNITY_EDITOR
        callback(true);
#else
        _rewardAdCallback = callback;
        if(_rewardAdCallback == null){
            return;
        }else if(IsLoadedRewardVideo()){
            _rewardAdOpen = false;
            _rewardAdFinish = false;
            _rewardAdClose = false;
    		_async_frame_count = 0;
            IronSource.Agent.showRewardedVideo();
        }else{
            _rewardAdCallback(false);
        }
#endif
    }
    private void RewardAdCallbackClose(bool success){
        resumeGame();
        _rewardAdCallback(success);
    }

    private void pauseGame(){
        SoundManager.Instance.PauseBGM();
        SoundManager.Instance.StopSE();
        if(Time.timeScale > 0.0f){
            _timeScale = Time.timeScale;            
            // TimeManager.Instance.Stop();
        }
    }
    private void resumeGame(){
        SoundManager.Instance.ReplayBGM();
        if(_timeScale > 0.0f){
            Time.timeScale = _timeScale;
            // TimeManager.Instance.ChangeTimeScale(_timeScale);
            _timeScale = 0.0f;      
        }
    }

}