using Adplay.Behaviours;
using Adplay.View;

#if UNITY_EDITOR
using UnityEditor;
#endif

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

        [Space]
        [SerializeField] private GameObject runtimeButtonsRoot;
        [SerializeField] private Button splashBtn, bannerBtn, popupButtonBtn, voiceBtn;


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

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

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

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

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

            });
            if (!result.IsSuccess)
                Debug.Log("No pop up button permanent");
        }

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

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

            });
            if (!result.IsSuccess)
                Debug.Log("No pop up button permanent");
        }



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

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

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

            var result = await AdPlayManager.GetSplash(template, Model.CreativeShowType.WeightBased);
            if (!result.IsSuccess)
            {
                Debug.Log("No splash weight based");
            }
        }


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

            var result = await AdPlayManager.GetBanner(template, Model.CreativeShowType.Permanent, Model.BannerPlaceType.MainMenuSlot1);
            if (!result.IsSuccess)
            {
                Debug.Log("No banner Permanent");
            }
        }

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

            var result = await AdPlayManager.GetBanner(template, Model.CreativeShowType.WeightBased, Model.BannerPlaceType.MainMenuSlot1);
            if (!result.IsSuccess)
            {
                Debug.Log("No banner weight based");
            }
        }

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

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



        private void InitializeRuntimeButtons()
        {
            splashBtn.onClick.AddListener(ShowSplashPermanent);
            bannerBtn.onClick.AddListener(ShowPermanantBanner);
            popupButtonBtn.onClick.AddListener(ShowPopUpButtonPermanent);
            voiceBtn.onClick.AddListener(ShowPermanentInterstitialSound);
        }

    }

#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 Splash
            if (GUILayout.Button("Show Splash Permanent"))
            {
                sample.ShowSplashPermanent();
            }
            if (GUILayout.Button("Show Splash Weight Based"))
            {
                sample.ShowSplashWeightBased();
            }

            GUILayout.Space(EditorGUIUtility.singleLineHeight);
            // Button for Show Banner
            if (GUILayout.Button("Show Banner Permanent"))
            {
                sample.ShowPermanantBanner();
            }
            // Button for Show Banner
            if (GUILayout.Button("Show Banner Weight Based"))
            {
                sample.ShowWeightBasedBanner();
            }
            GUILayout.Space(EditorGUIUtility.singleLineHeight);

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

            // Button for Show PopUp
            if (GUILayout.Button("Show PopUp Button Weight Based"))
            {
                sample.ShowPopUpButtonWeightBased();
            }
            GUILayout.Space(EditorGUIUtility.singleLineHeight);


            // Button for Hear Audio
            if (GUILayout.Button("Permanent Interstitial Sound"))
            {
                sample.ShowPermanentInterstitialSound();
            }
            if (GUILayout.Button("Weight Based Interstitial Sound"))
            {
                sample.ShowWeightBasedInterstitialSound();
            }

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