#if !UNITY_IOS
// Copyright (C) 2015 Google, Inc.
//
// 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.

using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.Scripting;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

namespace GoogleMobileAds
{
    [Preserve]
    public class GoogleMobileAdsClientFactory : IClientFactory
    {
        private static bool? _nextGenEnabled;

        public IAppStateEventClient BuildAppStateEventClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                return new GoogleMobileAds.Android.AppStateEventClient();
            }
            throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
            " on non-Android runtime");
        }

        public IAppOpenAdClient BuildAppOpenAdClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return new GoogleMobileAds.Android.NextGenAppOpenAdClient();
                }
                return new GoogleMobileAds.Android.AppOpenAdClient();
            }
            throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
            " on non-Android runtime");
        }

        public IBannerClient BuildBannerClient() {
          if (Application.platform == RuntimePlatform.Android) {
            if (IsNextGenEnabled())
            {
                    return new GoogleMobileAds.Android.NextGenBannerAdClient();
            }
            return new GoogleMobileAds.Android.BannerClient();
          }
          throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                              " on non-Android runtime");
        }

        public IAdManagerBannerClient BuildAdManagerBannerClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return new GoogleMobileAds.Android.NextGenBannerAdClient();
                }
                return new GoogleMobileAds.Android.AdManagerBannerClient();
            }
            throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                                " on non-Android runtime");
        }

        public IInterstitialClient BuildInterstitialClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return new GoogleMobileAds.Android.NextGenInterstitialAdClient();
                }
                return new GoogleMobileAds.Android.InterstitialClient();
            }
          throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                              " on non-Android runtime");
        }

        public IAdManagerInterstitialClient BuildAdManagerInterstitialClient() {
          if (Application.platform == RuntimePlatform.Android) {
            if (IsNextGenEnabled())
            {
                return new GoogleMobileAds.Android.NextGenInterstitialAdClient();
            }
            return new GoogleMobileAds.Android.AdManagerInterstitialClient();
          }
          throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                              " on non-Android runtime");
        }

        public IRewardedAdClient BuildRewardedAdClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return new GoogleMobileAds.Android.NextGenRewardedAdClient();
                }
                return new GoogleMobileAds.Android.RewardedAdClient();
            }
            throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                              " on non-Android runtime");
        }

        public IRewardedInterstitialAdClient BuildRewardedInterstitialAdClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return new GoogleMobileAds.Android.NextGenRewardedInterstitialAdClient();
                }
                return new GoogleMobileAds.Android.RewardedInterstitialAdClient();
            }
            throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                                " on non-Android runtime");
        }

        public INativeOverlayAdClient BuildNativeOverlayAdClient()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return new GoogleMobileAds.Android.NextGenNativeOverlayAdClient();
                }
                return new GoogleMobileAds.Android.NativeOverlayAdClient();
            }
            throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
            " on non-Android runtime");
        }

        public IApplicationPreferencesClient ApplicationPreferencesInstance() {
          if (Application.platform == RuntimePlatform.Android) {
            return new GoogleMobileAds.Android.ApplicationPreferencesClient();
          }
          throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                              " on non-Android runtime");
        }

        public IMobileAdsClient MobileAdsInstance()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                if (IsNextGenEnabled())
                {
                    return GoogleMobileAds.Android.NextGenMobileAdsClient.Instance;
                }
                return GoogleMobileAds.Android.MobileAdsClient.Instance;
          }
          throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                              " on non-Android runtime");
        }

        private bool IsNextGenEnabled()
        {
            if (_nextGenEnabled.HasValue)
            {
                return _nextGenEnabled.Value;
            }
            try
            {
                var _ =
                    new AndroidJavaClass(GoogleMobileAds.Android.NextGenUtils.MobileAdsClassName);
                _nextGenEnabled = true;
            }
            catch (AndroidJavaException)
            {
                _nextGenEnabled = false;
            }
            return _nextGenEnabled.Value;
        }

#if GMA_PREVIEW_FEATURES

        public IAppOpenAdPreloaderClient BuildAppOpenAdPreloaderClient()
        {
            if (Application.platform != RuntimePlatform.Android)
            {
                throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                                    " on non-Android runtime");
            }
            if (IsNextGenEnabled())
            {
                return new GoogleMobileAds.Android.NextGenAppOpenAdPreloaderClient();
            }
            return new GoogleMobileAds.Android.AppOpenAdPreloaderClient();
        }

        public IInterstitialAdPreloaderClient BuildInterstitialAdPreloaderClient()
        {
            if (Application.platform != RuntimePlatform.Android)
            {
                throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                                    " on non-Android runtime");
            }
            if (IsNextGenEnabled())
            {
                return new GoogleMobileAds.Android.NextGenInterstitialAdPreloaderClient();
            }
            return new GoogleMobileAds.Android.InterstitialAdPreloaderClient();
        }

        public IRewardedAdPreloaderClient BuildRewardedAdPreloaderClient()
        {
            if (Application.platform != RuntimePlatform.Android)
            {
                throw new InvalidOperationException(@"Called " + MethodBase.GetCurrentMethod().Name +
                                                    " on non-Android runtime");
            }
            if (IsNextGenEnabled())
            {
                return new GoogleMobileAds.Android.NextGenRewardedAdPreloaderClient();
            }
            return new GoogleMobileAds.Android.RewardedAdPreloaderClient();
        }

#endif
    }
}
#endif
