using Adplay.Behaviours;
using Adplay.View;

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;
namespace Adplay.Test
{
    public class SampleTest : MonoBehaviour
    {
        [SerializeField] private Transform canvas;
        [SerializeField] private AdPlayTemplate templatePrefab;
        [SerializeField] private GameObject infoGameObject, sampleTestGameObject;
        [SerializeField] private Transform buttonParent;


        private void OnEnable()
        {
            AdPlayManager.OnGetAdsComplete += OnGetAdsComplete;
        }

        private void OnDisable()
        {
            AdPlayManager.OnGetAdsComplete -= OnGetAdsComplete;
        }

        private void OnGetAdsComplete()
        {
            infoGameObject.SetActive(false);
            sampleTestGameObject.SetActive(true);
        }

        public async void ShowPopUpButton()
        {
            var buttonTemplate = Instantiate(templatePrefab, canvas);

            var result = await AdPlayManager.GetPopUpButton(buttonTemplate, async () =>
            {
                var template = Instantiate(templatePrefab, canvas);
                var result = await AdPlayManager.GetPopUp(template);
                if (!result.IsSuccess)
                {
                    Debug.Log("No pop up");
                }
            });
            if (!result.IsSuccess)
                Debug.Log("No pop up button");
        }

        public async void ShowSplash()
        {
            var template = Instantiate(templatePrefab, canvas);

            var result = await AdPlayManager.GetSplash(template);
            if (!result.IsSuccess)
            {
                Debug.Log("No splash");
            }
        }

        public async void ShowBanner()
        {
            var template = Instantiate(templatePrefab, canvas);

            var result = await AdPlayManager.GetBanner(template);
            if (!result.IsSuccess)
            {
                Debug.Log("No banner");
            }
        }

        public async void ShowPopUp()
        {
            var template = Instantiate(templatePrefab, canvas);

            var result = await AdPlayManager.GetPopUp(template);
            if (!result.IsSuccess)
            {
                Debug.Log("No pop up");
            }
        }

        public async void ShowInterstitialSound()
        {
            var result = await AdPlayManager.GetInterstitialSound();
            if (!result.IsSuccess)
            {
                Debug.Log("No interstitial sound");
            }
        }

    }

#if UNITY_EDITOR

    [CustomEditor(typeof(SampleTest))]
    public class SampleTestEditor : Editor
    {
        public override async void OnInspectorGUI()
        {
            // Draw the default inspector (your SerializedFields)
            DrawDefaultInspector();

            // Reference to the target script
            SampleTest sample = (SampleTest)target;

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("AdPlay Actions", EditorStyles.boldLabel);

            // Button for Show Pop Up Button
            if (GUILayout.Button("Show Pop Up Button"))
            {
                sample.ShowPopUpButton();
            }

            // Button for Show Splash
            if (GUILayout.Button("Show Splash"))
            {
                sample.ShowSplash();
            }

            // Button for Show Banner
            if (GUILayout.Button("Show Banner"))
            {
                sample.ShowBanner();
            }

            // Button for Show PopUp
            if (GUILayout.Button("Show PopUp"))
            {
                sample.ShowPopUp();
            }

            // Button for Hear Audio
            if (GUILayout.Button("Hear Audio"))
            {
                sample.ShowInterstitialSound();
            }


            GUILayout.Space(10);
            if (GUILayout.Button("Initialize SDK"))
            {
                await AdPlayManager.Initialize();
            }
        }
    }
#endif
}
