using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Linq; // Added for resource name lookup
using System.Threading.Tasks;
using Microsoft.Web.WebView2.Core;


#if HAS_UNO
using Uno.UI.NativeElementHosting;
#endif

namespace Zuken.UI.GdViewerControl.Views.Control3D
{
        public partial class BabylonJSControl : ContentPresenter
        {
#if HAS_UNO
                private BrowserHtmlElement? _element { get; set; }
#endif

                public BabylonJSControl()
                {
                        //this.DefaultStyleKey = typeof(BabylonJSControl);
                        Background = new SolidColorBrush(Colors.Red);

#if HAS_UNO

                        InitializeForWasm();
#else

                        //_ = InitializeForWindowsAsync();
#endif
                }

#if HAS_UNO

                private void InitializeForWasm()
                {
                        try
                        {
                                // For WASM, create a canvas element directly
                                _element = BrowserHtmlElement.CreateHtmlElement("renderCanvas", "canvas");
                                _element.SetCssStyle(
                                        ("width", "100%"),
                                        ("height", "100%"),
                                        ("touch-action", "none")
                                );
                                Content = _element;
                        
                                var js = @"const canvas = document.getElementById(""renderCanvas""); // Get the canvas element
        const engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine

        const createScene = function () {
            // This creates a basic Babylon Scene object (non-mesh)
            const scene = new BABYLON.Scene(engine);

            // This creates and positions a free camera (non-mesh)
            const camera = new BABYLON.FreeCamera(""camera1"", new BABYLON.Vector3(0, 5, -10), scene);

            // This targets the camera to scene origin
            camera.setTarget(BABYLON.Vector3.Zero());

            // This attaches the camera to the canvas
            camera.attachControl(canvas, true);

            // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
            const light = new BABYLON.HemisphericLight(""light"", new BABYLON.Vector3(0, 1, 0), scene);

            // Default intensity is 1. Let's dim the light a small amount
            light.intensity = 0.7;

            // Our built-in 'sphere' shape.
            const sphere = BABYLON.MeshBuilder.CreateSphere(""sphere"", {diameter: 2, segments: 32}, scene);

            // Move the sphere upward 1/2 its height
            sphere.position.y = 1;

            // Our built-in 'ground' shape.
            const ground = BABYLON.MeshBuilder.CreateGround(""ground"", {width: 6, height: 6}, scene);

            return scene;
        };

        const scene = createScene(); //Call the createScene function

        // Register a render loop to repeatedly render the scene
        engine.runRenderLoop(function () {
                scene.render();
        });

        // Watch for browser/canvas resize events
        window.addEventListener(""resize"", function () {
                engine.resize();
        });
";

                                // Execute the JavaScript after a short delay to ensure the canvas is ready
                                _element.ExecuteJavascript(js);
                        }
                        catch (Exception ex)
                        {
                                //Content = new TextBlock
                                //{
                                //        Text = $"Error loading BabylonJS: {ex.Message}",
                                //        HorizontalAlignment = HorizontalAlignment.Center,
                                //        VerticalAlignment = VerticalAlignment.Center,
                                //        Foreground = new SolidColorBrush(Colors.Red)
                                //};

                                Console.WriteLine($"Error loading BabylonJS: {ex.Message}");
                        }
                }
#endif

        }
}
