using System;
using System.Collections;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Gms.Ads;
using Android.Gms.Common;
using Android.Gms.Common.Apis;
using Android.Gms.Extensions;
using Android.Gms.Games;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Coroutine;
using GameAnalyticsSDK;
using GameAnalyticsSDK.Utilities;
using TouchyTickets;
using Uri = Android.Net.Uri;

namespace Android {
    public class AndroidPlatform : Platform {

        public const int GooglePlayLoginRequest = 9001;
        private readonly Activity activity;
        private readonly LinearLayout adLayout;
        public GoogleApiClient GoogleApi { get; private set; }

        public AndroidPlatform(Activity activity, LinearLayout adLayout) {
            this.activity = activity;
            this.adLayout = adLayout;
        }

        public override void SetupOnlineInteractions(Dictionary<string, object> analyticsJson) {
            // Analytics
            GameAnalytics.SetAutoDetectAppVersion(true);
            GameAnalytics.Initialize(this.activity, GA_MiniJSON.JsonEncode(new Hashtable(analyticsJson)));
            AndroidEnvironment.UnhandledExceptionRaiser += (o, args) => GameAnalytics.NewErrorEvent(GAErrorSeverity.Critical, args.Exception.ToString());

            // Ads
            var ad = new AdView(this.activity) {
                AdUnitId = "ca-app-pub-5754829579653773/7841535920",
                AdSize = AdSize.SmartBanner
            };
            ad.LoadAd(new AdRequest.Builder()
                .AddTestDevice("14B965C6457E17D2808061ADF7E34923")
                .Build());
            this.adLayout.AddView(ad);

            // Google Play game services
            this.GoogleApi = new GoogleApiClient.Builder(this.activity)
                .AddApi(GamesClass.API)
                .AddScope(GamesClass.ScopeGames)
                .AddOnConnectionFailedListener(res => {
                    if (res.HasResolution) {
                        res.StartResolutionForResult(this.activity, GooglePlayLoginRequest);
                    } else {
                        throw new GoogleApiClientConnectionException(res);
                    }
                })
                .Build();
            this.GoogleApi.Connect();
        }

        public override void AddResourceEvent(bool sink, string currency, float amount, string itemType, string itemId) {
            GameAnalytics.NewResourceEvent(sink ? GAResourceFlowType.Sink : GAResourceFlowType.Source, currency, amount, itemType, itemId);
        }

        public override void SetKeepScreenOn(bool keep) {
            if (keep) {
                this.activity.Window.AddFlags(WindowManagerFlags.KeepScreenOn);
            } else {
                this.activity.Window.ClearFlags(WindowManagerFlags.KeepScreenOn);
            }
        }

        public override void OpenRateLink() {
            this.activity.StartActivity(new Intent(Intent.ActionView, Uri.Parse("https://play.google.com/store/apps/details?id=de.ellpeck.touchytickets")));
        }

    }
}