using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class ReturnButtonSpawner : MonoBehaviour
{

    public static void SpawnReturnButton(Transform nearTarget, GameObject modelSelectorCanvas)
    {
        
        var returnCanvasGO = new GameObject("ReturnCanvas");
        var canvas = returnCanvasGO.AddComponent<Canvas>();
        canvas.renderMode = RenderMode.WorldSpace;
        returnCanvasGO.AddComponent<CanvasScaler>().uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
        returnCanvasGO.AddComponent<GraphicRaycaster>();
        returnCanvasGO.AddComponent<OVRRaycaster>();

        returnCanvasGO.transform.position = nearTarget.position + Vector3.right * 2;
        returnCanvasGO.transform.rotation = Quaternion.LookRotation(returnCanvasGO.transform.position - Camera.main.transform.position);

        var buttonGO = new GameObject("ReturnButton");
        buttonGO.transform.SetParent(returnCanvasGO.transform, false);

        var rt = buttonGO.AddComponent<RectTransform>();
        rt.sizeDelta = new Vector2(300, 100);
        rt.localScale = Vector3.one * 0.005f;

        var image = buttonGO.AddComponent<Image>();
        var backgroundSprite = Resources.Load<Sprite>("sprites/default");
        image.sprite = backgroundSprite ?? null;
        image.color = backgroundSprite ? Color.white : new Color(0.2f, 0.2f, 0.2f, 0.8f);

        var button = buttonGO.AddComponent<Button>();
        var collider = buttonGO.AddComponent<BoxCollider>();
        collider.size = new Vector3(300, 100, 1);

        // === Titel ===
        var titleGO = new GameObject("TitleText");
        titleGO.transform.SetParent(buttonGO.transform, false);
        var titleText = titleGO.AddComponent<TextMeshProUGUI>();
        titleText.text = "Zurück ins Menü";
        titleText.fontSize = 24;

        var titleRT = titleGO.GetComponent<RectTransform>();
        titleRT.anchorMin = new Vector2(0, 1);
        titleRT.anchorMax = new Vector2(1, 1);
        titleRT.pivot = new Vector2(0, 1);
        titleRT.anchoredPosition = new Vector2(60, -10);
        titleRT.sizeDelta = new Vector2(-70, 30);

        

        button.onClick.AddListener(() =>
        {
            modelSelectorCanvas.SetActive(true);
            
            // Altes BaseModel zerstören und ein neues leeres erstellen
            var modelManager = FindObjectOfType<ModelManager>();
            if (modelManager != null && modelManager.baseModel != null)
            {
                Destroy(modelManager.baseModel); // Altes BaseModel zerstören

                
                modelManager.baseModel = new GameObject("BaseModel");
                modelManager.baseModel.AddComponent<BaseModelBehaviour>();
                modelManager.baseModel.transform.position = new Vector3(0,0,1); // Optional: Position setzen
                Debug.Log("Neues leeres BaseModel erstellt.");
            }
            Destroy(returnCanvasGO);
            Debug.Log("Zurück ins Menü");
        });
    }
}

