﻿#if UNITY_ANDROID
using System.Collections;
using System.Collections.Generic;

using Bayat.SaveSystem.Storage;

using GooglePlayGames;
using GooglePlayGames.BasicApi;

using UnityEngine;
using UnityEngine.UI;

namespace Bayat.SaveSystem.Examples
{

    public class GooglePlayLoginExample : MonoBehaviour
    {

        [SerializeField]
        protected Selectable[] controls;

        [SerializeField]
        protected Texture2D pngImage;
        [SerializeField]
        protected string description = "Demo description for Saved Game Metadata";

        private void Start()
        {
            this.controls = FindObjectsOfType<Selectable>();
            SetControlsActive(false);
            Login();
        }

        public void Login()
        {
            Debug.Log("Logging in");

            // Enable debug mode
            PlayGamesPlatform.DebugLogEnabled = true;

            // Initialize Play Games Platform
            //var builder = new PlayGamesClientConfiguration.Builder();
            //builder.EnableSavedGames();
            //builder.AddOauthScope("profile");
            //var config = builder.Build();
            //PlayGamesPlatform.InitializeInstance(config);

            // Authenticate the user
            PlayGamesPlatform.Instance.Authenticate((status) =>
            {
                if (status == SignInStatus.Success)
                {
                    Debug.Log("Logged in using Google Play successfully!");

                    // Change the storage to Google Play Saved Game Storage
                    SaveSystemSettings settings = SaveSystemSettings.DefaultSettings;
                    var googleStorage = (GooglePlaySavedGameStorage)StorageFactory.FromConnectionString("google-play-games://");
                    settings.Storage = googleStorage;

                    // Capture a screenshot and use it as the cover image for the saved game if there are no images specified
                    if (this.pngImage == null)
                    {
                        this.pngImage = ScreenCapture.CaptureScreenshotAsTexture();
                    }
                    googleStorage.MetadataPngImage = this.pngImage;
                    SetControlsActive(true);
                }
                else
                {
                    Debug.LogError("Failed to login with Google Play");
                    Debug.LogError(status);
                    SetControlsActive(false);
                }
            });
        }

        public void SetControlsActive(bool active)
        {
            foreach (Selectable control in this.controls)
            {
                control.interactable = active;
            }
        }

    }

}
#endif