using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace HMIWeb
{
    // Important: Allows JavaScript to see this class
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class MachineBridge
    {
        // Reference to the main form to update logic
        private MainForm _host;

        public MachineBridge(MainForm host)
        {
            _host = host;
        }

        // --- Methods called from React (App.tsx) ---

        public void MoveAxis(string axis, double value)
        {
            // In real app, call DLL here: Motion.Move(axis, value)
            Console.WriteLine($"[C#] Moving {axis} to {value}");

            // For simulation, update host state directly
            _host.SetTargetPosition(axis, value);
        }

        public void SetIO(int id, bool isInput, bool state)
        {
            Console.WriteLine($"[C#] Set IO {id} to {state}");
            _host.SetOutput(id, state);
        }

        public void SystemControl(string command)
        {
            Console.WriteLine($"[C#] CMD: {command}");
            _host.HandleCommand(command);
        }

        public void LoadRecipe(string recipeId)
        {
            Console.WriteLine($"[C#] Loading Recipe: {recipeId}");
        }

        public string GetConfig()
        {
            // Generate 20 Mock Settings
            var settings = new System.Collections.Generic.List<object>();
            
            // Core Settings
            settings.Add(new { Key = "Site Name", Value = "Smart Factory A-1", Group = "System Information", Type = "String", Description = "The display name of the factory site." });
            settings.Add(new { Key = "Line ID", Value = "L-2024-001", Group = "System Information", Type = "String", Description = "Unique identifier for this production line." });
            settings.Add(new { Key = "API Endpoint", Value = "https://api.factory.local/v1", Group = "Network Configuration", Type = "String", Description = "Base URL for the backend API." });
            settings.Add(new { Key = "Support Contact", Value = "010-1234-5678", Group = "System Information", Type = "String", Description = "Emergency contact number for maintenance." });
            settings.Add(new { Key = "Debug Mode", Value = "false", Group = "System Information", Type = "Boolean", Description = "Enable detailed logging for debugging." });
            settings.Add(new { Key = "Max Speed", Value = "1500", Group = "Motion Control", Type = "Number", Description = "Maximum velocity in mm/s." });
            settings.Add(new { Key = "Acceleration", Value = "500", Group = "Motion Control", Type = "Number", Description = "Acceleration ramp in mm/s²." });

            // Generated Settings
            for (int i = 1; i <= 5; i++)
            {
                settings.Add(new { 
                    Key = $"Sensor_{i}_Threshold", 
                    Value = (i * 10).ToString(), 
                    Group = "Sensor Calibration",
                    Type = "Number",
                    Description = $"Trigger threshold for Sensor {i}."
                });
            }
            
            for (int i = 1; i <= 3; i++)
            {
                settings.Add(new { 
                    Key = $"Safety_Zone_{i}", 
                    Value = "true", 
                    Group = "Safety Settings",
                    Type = "Boolean",
                    Description = $"Enable monitoring for Safety Zone {i}."
                });
            }

            Console.WriteLine("get config (20 items)");
            return Newtonsoft.Json.JsonConvert.SerializeObject(settings);
        }

        public void SaveConfig(string configJson)
        {
            Console.WriteLine($"[Backend] SAVE CONFIG REQUEST RECEIVED");
            Console.WriteLine($"[Backend] Data: {configJson}");
            // In a real app, we would save this to a file or database
        }
    }
}
