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 string SelectRecipe(string recipeId)
        {
            Console.WriteLine($"[C#] Selecting Recipe: {recipeId}");

            // In real app, load recipe settings here
            // For now, just return success
            try
            {
                // Simulate recipe loading logic
                _host.SetCurrentRecipe(recipeId);

                var response = new { success = true, message = "Recipe selected successfully", recipeId = recipeId };
                return JsonConvert.SerializeObject(response);
            }
            catch (Exception ex)
            {
                var response = new { success = false, message = ex.Message };
                return JsonConvert.SerializeObject(response);
            }
        }

        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
        }
        public string GetIOList()
        {
            var ioList = new System.Collections.Generic.List<object>();

            // Outputs (0-31)
            for (int i = 0; i < 32; i++)
            {
                string name = $"DOUT_{i:D2}";
                if (i == 0) name = "Tower Lamp Red";
                if (i == 1) name = "Tower Lamp Yel";
                if (i == 2) name = "Tower Lamp Grn";
                
                ioList.Add(new { id = i, name = name, type = "output", state = false });
            }

            // Inputs (0-31)
            for (int i = 0; i < 32; i++)
            {
                string name = $"DIN_{i:D2}";
                bool initialState = false;
                if (i == 0) name = "Front Door Sensor";
                if (i == 1) name = "Right Door Sensor";
                if (i == 2) name = "Left Door Sensor";
                if (i == 3) name = "Back Door Sensor";
                if (i == 4) { name = "Main Air Pressure"; initialState = true; }
                if (i == 5) { name = "Vacuum Generator"; initialState = true; }
                if (i == 6) { name = "Emergency Stop Loop"; initialState = true; }

                ioList.Add(new { id = i, name = name, type = "input", state = initialState });
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(ioList);
        }
        public string GetRecipeList()
        {
            var recipes = new System.Collections.Generic.List<object>();
            recipes.Add(new { id = "1", name = "Wafer_Proc_300_Au", lastModified = "2023-10-25" });
            recipes.Add(new { id = "2", name = "Wafer_Insp_200_Adv", lastModified = "2023-10-26" });
            recipes.Add(new { id = "3", name = "Glass_Gen5_Bonding", lastModified = "2023-10-27" });
            recipes.Add(new { id = "4", name = "Solar_Cell_Cut_A", lastModified = "2023-11-01" });
            recipes.Add(new { id = "5", name = "LED_Mount_HighSpeed", lastModified = "2023-11-15" });

            return Newtonsoft.Json.JsonConvert.SerializeObject(recipes);
        }

        public string CopyRecipe(string recipeId, string newName)
        {
            Console.WriteLine($"[C#] Copying Recipe: {recipeId} as {newName}");

            try
            {
                // In real app, copy recipe data from database/file
                // Generate new ID
                string newId = System.Guid.NewGuid().ToString().Substring(0, 8);
                string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd");

                var response = new {
                    success = true,
                    message = "Recipe copied successfully",
                    newRecipe = new {
                        id = newId,
                        name = newName,
                        lastModified = timestamp
                    }
                };
                return JsonConvert.SerializeObject(response);
            }
            catch (Exception ex)
            {
                var response = new { success = false, message = ex.Message };
                return JsonConvert.SerializeObject(response);
            }
        }

        public string DeleteRecipe(string recipeId)
        {
            Console.WriteLine($"[C#] Deleting Recipe: {recipeId}");

            try
            {
                // In real app, delete recipe from database/file
                // Check if recipe is in use
                if (recipeId == _host.GetCurrentRecipe())
                {
                    var response = new { success = false, message = "Cannot delete currently selected recipe" };
                    return JsonConvert.SerializeObject(response);
                }

                var response = new { success = true, message = "Recipe deleted successfully", recipeId = recipeId };
                return JsonConvert.SerializeObject(response);
            }
            catch (Exception ex)
            {
                var response = new { success = false, message = ex.Message };
                return JsonConvert.SerializeObject(response);
            }
        }
    }
}
