using System;
using Newtonsoft.Json.Linq;
using CncSimulator.Device;

namespace CncSimulator.Generator
{
    /// <summary>
    /// FANUC品牌JSON数据生成器。
    /// 根据设备状态生成18个Tag的FANUC格式JSON。
    /// </summary>
    public class FanucDataGenerator : IBrandGenerator
    {
        private readonly Random _rng = new Random();

        /// <summary>品牌标识</summary>
        public string BrandKey => "fanuc";

        /// <summary>生成单个设备的完整JSON对象</summary>
        public JObject GenerateDevice(DeviceState state)
        {
            var device = new JObject
            {
                ["device"] = state.DeviceCode,
                ["desc"] = state.Desc,
                ["tags"] = GenerateTags(state)
            };
            return device;
        }

        /// <summary>生成18个Tag的JArray</summary>
        private JArray GenerateTags(DeviceState state)
        {
            var tags = new JArray();
            DateTime baseTime = DateTime.Now;

            // 每个tag的时间基准上随机偏移 -5~0 秒
            DateTime TagTime()
            {
                return baseTime.AddSeconds(-_rng.Next(0, 6));
            }

            // 数值型tag
            void AddNumericTag(string id, string desc, decimal value)
            {
                tags.Add(new JObject
                {
                    ["id"] = id,
                    ["desc"] = desc,
                    ["quality"] = "0",
                    ["value"] = value.ToString("0.00000"),
                    ["time"] = TagTime().ToString("yyyy-MM-dd HH:mm:ss")
                });
            }

            // 字符串型tag
            void AddStringTag(string id, string desc, string value)
            {
                tags.Add(new JObject
                {
                    ["id"] = id,
                    ["desc"] = desc,
                    ["quality"] = "0",
                    ["value"] = value,
                    ["time"] = TagTime().ToString("yyyy-MM-dd HH:mm:ss")
                });
            }

            AddNumericTag("_io_status", "设备状态", state.DeviceStatus);
            AddNumericTag("Tag2", "当前轴数", 4);
            AddStringTag("Tag5", "执行的NC主程序名", state.ProgramName);
            AddStringTag("Tag6", "执行的NC主程序号", "N0");
            AddStringTag("Tag7", "当前加工程序内容",
                string.IsNullOrEmpty(state.ProgramContent)
                    ? $"<{state.ProgramName}>\nG40G49G80\n( SIMULATOR )"
                    : state.ProgramContent);
            AddNumericTag("Tag8", "当前加工零件数", state.PartCount);
            AddNumericTag("Tag9", "运行状态", state.RunStatus);
            AddNumericTag("Tag11", "操作模式", state.OperateMode);
            AddNumericTag("Tag14", "当前主轴倍率", state.SpindleOverride);
            AddNumericTag("Tag17", "主轴设定速度", state.SpindleSpeedSet);
            AddNumericTag("Tag18", "进给设定速度", state.FeedSpeedSet);
            AddNumericTag("Tag19", "主轴实际速度", state.SpindleSpeedActual);
            AddNumericTag("Tag20", "进给实际转速", state.FeedSpeedActual);
            AddNumericTag("Tag21", "主轴负载", state.SpindleLoad);
            AddNumericTag("Tag22", "开机时间", state.PowerOnTime);
            AddNumericTag("Tag23", "运行时间", state.RunTime);
            AddNumericTag("Tag24", "切削时间", state.CuttingTime);
            AddNumericTag("Tag25", "循环时间", state.CycleTime);
            AddStringTag("Tag26", "加工状态", state.MachiningStatus);

            return tags;
        }
    }
}
