using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using TSMaster;

namespace CAN驱动
{
    public struct can_network
    {
        [JsonInclude]
        public string network_name;
        [JsonInclude]
        public can_node[] can_nodes;

    }
    public struct can_node
    {
        [JsonInclude]
        public string node_name;
        [JsonInclude]
        public can_message[] tx_can_Messages;
        [JsonInclude]
        public can_message[] rx_can_Messages;
    }
    public struct can_message
    {
        [JsonInclude]
        public string message_name;
        [JsonInclude]
        public string[] signals;
    }

    public class _Msg_
    {
        public int msg_type { get; set; }
        public int msg_id { get; set; }
        public int msg_cyclic { get; set; }
        public byte msg_dlc { get; set; }
        public string msg_name { get; set; }
        public string[] signal_Name { get; set; }
        public int msg_brs { get; set; }
        public TLIBCANFD ACANFD;
    }
    public enum Msg_Type
    {
        CAN = 0,
        CANFD = 1,
    }
    /// <summary>
    /// DBC 文件信息类型枚举，用于指定 tsdb_get_can_db_info 查询的内容类型。
    /// </summary>
    public enum DBCINFO
    {
        /// <summary>网络名称</summary>
        NETWORK_Name = 0,

        /// <summary>CAN 报文总数</summary>
        CAN_MSG_COUNT = 11,
        /// <summary>CAN FD 报文总数</summary>
        CANFD_MSG_COUNT = 12,
        /// <summary>节点总数</summary>
        NODE_COUNT = 14,

        /// <summary>信号名称</summary>
        SIGNAL_NAME = 33,

        /// <summary>CAN 报文类型</summary>
        CAN_MSG_TYPE = 40,
        /// <summary>CAN 报文 DLC（数据长度码）</summary>
        CAN_MSG_DLC = 41,
        /// <summary>CAN 报文 ID</summary>
        CAN_MSG_ID = 42,
        /// <summary>CAN 报文周期</summary>
        CAN_MSG_CYCLIC = 43,
        /// <summary>CAN 报文名称</summary>
        CAN_MSG_NAME = 44,
        /// <summary>CAN 报文信号数量</summary>
        CAN_MSG_SIGNAL_COUNT = 47,
        /// <summary>CAN 报文信号名称索引</summary>
        CAN_MSG_SIGNAL_NAME_INDEX = 48,

        /// <summary>CAN FD 报文类型</summary>
        CANFD_MSG_TYPE = 60,
        /// <summary>CAN FD 报文 DLC（数据长度码）</summary>
        CANFD_MSG_DLC = 61,
        /// <summary>CAN FD 报文 ID</summary>
        CANFD_MSG_ID = 62,
        /// <summary>CAN FD 报文周期</summary>
        CANFD_MSG_CYCLIC = 63,
        /// <summary>CAN FD 报文名称</summary>
        CANFD_MSG_NAME = 64,
        /// <summary>CAN FD 报文信号数量</summary>
        CANFD_MSG_SIGNAL_COUNT = 67,
        /// <summary>CAN FD 报文信号名称索引</summary>
        CANFD_MSG_SIGNAL_NAME_INDEX = 68,

        /// <summary>节点名称</summary>
        NODE_Name = 101,
        /// <summary>节点发送 CAN 报文数量</summary>
        NODE_TX_CAN_COUNT = 103,
        /// <summary>节点发送 CAN 报文索引</summary>
        NODE_TX_CAN_MESSAGE_INDEX = 104,
        /// <summary>节点接收 CAN 报文数量</summary>
        NODE_RX_CAN_COUNT = 105,
        /// <summary>节点接收 CAN 报文索引</summary>
        NODE_RX_CAN_MESSAGE_INDEX = 106,
        /// <summary>节点发送 CAN FD 报文数量</summary>
        NODE_TX_CANFD_COUNT = 107,
        /// <summary>节点发送 CAN FD 报文索引</summary>
        NODE_TX_CANFD_MESSAGE_INDEX = 108,
        /// <summary>节点接收 CAN FD 报文数量</summary>
        NODE_RX_CANFD_COUNT = 109,
        /// <summary>节点接收 CAN FD 报文索引</summary>
        NODE_RX_CANFD_MESSAGE_INDEX = 110,
    }

    public class DBCParse
    {
        public static List<List<_Msg_>> MsgDatabase { get; set; } =
            [.. Enumerable.Range(0, 12).Select(_ => new List<_Msg_>())];
        public static can_network can_Network;
        public static string tsdb_get_can_db_info(uint ADatabaseId, int AType, int AIndex, int ASubIndex)
        {
            nint tmpInt = new nint();
            int ret = TsMasterApi.tsdb_get_can_db_info(ADatabaseId, AType, AIndex, ASubIndex, ref tmpInt);
            if (ret == 0)
            {
                return Marshal.PtrToStringAnsi(tmpInt) ?? "";
            }
            else
                return "";
        }

        public static void parse(uint Aid, int channel)
        {
            int CANcount = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_COUNT, 0, 0));
            for (int i = 0; i < CANcount; i++)
            {
                _Msg_ temp = new _Msg_();
                temp.msg_type = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_TYPE, i, 0));
                temp.msg_dlc = byte.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_DLC, i, 0));
                temp.msg_id = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_ID, i, 0));
                temp.msg_name = tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_NAME, i, 0);
                temp.msg_cyclic = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_CYCLIC, i, 0));
                temp.signal_Name = new string[int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_SIGNAL_COUNT, i, 0))];
                for (int signalcount = 0; signalcount < temp.signal_Name.Length; signalcount++)
                {
                    temp.signal_Name[signalcount] = tsdb_get_can_db_info(Aid, (int)DBCINFO.SIGNAL_NAME, int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CAN_MSG_SIGNAL_NAME_INDEX, i, signalcount)), 0);
                }
                temp.msg_brs = 0;
                temp.ACANFD = new TLIBCANFD(APP_CHANNEL.CHN1,
                    temp.msg_id,
                    true,
                    temp.msg_type == 1 || temp.msg_type == 4 ? true : false,
                    false, temp.msg_dlc,
                    temp.msg_type < 2 ? false : true,
                    false);
                MsgDatabase[channel].Add(temp);

            }

            CANcount = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_COUNT, 0, 0));
            for (int i = 0; i < CANcount; i++)
            {
                _Msg_ temp = new _Msg_();
                temp.msg_type = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_TYPE, i, 0));
                temp.msg_dlc = byte.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_DLC, i, 0));
                temp.msg_id = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_ID, i, 0));
                temp.msg_name = tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_NAME, i, 0);
                temp.msg_cyclic = int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_CYCLIC, i, 0));
                temp.signal_Name = new string[int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_SIGNAL_COUNT, i, 0))];
                for (int signalcount = 0; signalcount < temp.signal_Name.Length; signalcount++)
                {
                    temp.signal_Name[signalcount] = tsdb_get_can_db_info(Aid, (int)DBCINFO.SIGNAL_NAME, int.Parse(tsdb_get_can_db_info(Aid, (int)DBCINFO.CANFD_MSG_SIGNAL_NAME_INDEX, i, signalcount)), 0);
                }
                temp.msg_brs = int.Parse(tsdb_get_can_db_info(Aid, 69, i, 0));
                temp.ACANFD = new TLIBCANFD(APP_CHANNEL.CHN1, temp.msg_id, true, temp.msg_type == 1 || temp.msg_type == 4 ? true : false, false, temp.msg_dlc, temp.msg_type < 2 ? false : true, temp.msg_brs == 1 ? true : false);
                MsgDatabase[channel].Add(temp);
            }
            MsgDatabase[channel].Sort((a, b) => a.msg_id.CompareTo(b.msg_id));
        }

        public static void rbs_parse(uint AID, int channel)
        {
            can_Network = new can_network();
            can_Network.network_name = tsdb_get_can_db_info(AID, (int)DBCINFO.NETWORK_Name, 0, 0);
            int Node_count = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_COUNT, 0, 0));
            can_Network.can_nodes = new can_node[Node_count];
            for (int i = 0; i < Node_count; i++)
            {
                can_Network.can_nodes[i].node_name = tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_Name, i, 0);
                int tx_can_count = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_TX_CAN_COUNT, i, 0));
                int tx_canfd_count = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_TX_CANFD_COUNT, i, 0));

                int rx_can_count = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_RX_CAN_COUNT, i, 0));
                int rx_canfd_count = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_RX_CANFD_COUNT, i, 0));

                //获取tx_can结构体
                can_Network.can_nodes[i].tx_can_Messages = new can_message[tx_can_count + tx_canfd_count];

                can_Network.can_nodes[i].rx_can_Messages = new can_message[rx_can_count + rx_canfd_count];
                //获取报文名以及信号名
                //1 get can_message idnex
                //2 get can message name 
                //3 get signal count
                //4 get signal name
                int tx_index = -1;
                for (int tx_can = 0; tx_can < tx_can_count; tx_can++)
                {
                    int can_message_idnex = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_TX_CAN_MESSAGE_INDEX, i, tx_can));
                    can_Network.can_nodes[i].tx_can_Messages[tx_can].message_name = tsdb_get_can_db_info(AID, (int)DBCINFO.CAN_MSG_NAME, can_message_idnex, 0);

                    can_Network.can_nodes[i].tx_can_Messages[tx_can].signals = new string[int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CAN_MSG_SIGNAL_COUNT, can_message_idnex, 0))];
                    for (int signalcount = 0; signalcount < can_Network.can_nodes[i].tx_can_Messages[tx_can].signals.Length; signalcount++)
                    {
                        can_Network.can_nodes[i].tx_can_Messages[tx_can].signals[signalcount] = tsdb_get_can_db_info(AID, (int)DBCINFO.SIGNAL_NAME, int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CAN_MSG_SIGNAL_NAME_INDEX, can_message_idnex, signalcount)), 0);
                    }

                    tx_index = tx_can;
                }
                for (int tx_can = 0; tx_can < tx_canfd_count; tx_can++, tx_index++)
                {
                    int can_message_idnex = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_TX_CANFD_MESSAGE_INDEX, i, tx_can));
                    can_Network.can_nodes[i].tx_can_Messages[tx_index + 1].message_name = tsdb_get_can_db_info(AID, (int)DBCINFO.CANFD_MSG_NAME, can_message_idnex, 0);

                    can_Network.can_nodes[i].tx_can_Messages[tx_index + 1].signals = new string[int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CANFD_MSG_SIGNAL_COUNT, can_message_idnex, 0))];
                    for (int signalcount = 0; signalcount < can_Network.can_nodes[i].tx_can_Messages[tx_index + 1].signals.Length; signalcount++)
                    {
                        can_Network.can_nodes[i].tx_can_Messages[tx_index + 1].signals[signalcount] = tsdb_get_can_db_info(AID, (int)DBCINFO.SIGNAL_NAME, int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CANFD_MSG_SIGNAL_NAME_INDEX, can_message_idnex, signalcount)), 0);
                    }
                }

                int rx_index = -1;
                for (int tx_can = 0; tx_can < rx_can_count; tx_can++)
                {
                    int can_message_idnex = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_RX_CAN_MESSAGE_INDEX, i, tx_can));
                    can_Network.can_nodes[i].rx_can_Messages[tx_can].message_name = tsdb_get_can_db_info(AID, (int)DBCINFO.CAN_MSG_NAME, can_message_idnex, 0);

                    can_Network.can_nodes[i].rx_can_Messages[tx_can].signals = new string[int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CAN_MSG_SIGNAL_COUNT, can_message_idnex, 0))];
                    for (int signalcount = 0; signalcount < can_Network.can_nodes[i].rx_can_Messages[tx_can].signals.Length; signalcount++)
                    {
                        can_Network.can_nodes[i].rx_can_Messages[tx_can].signals[signalcount] = tsdb_get_can_db_info(AID, (int)DBCINFO.SIGNAL_NAME, int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CAN_MSG_SIGNAL_NAME_INDEX, can_message_idnex, signalcount)), 0);
                    }

                    rx_index = tx_can;
                }
                for (int tx_can = 0; tx_can < rx_canfd_count; tx_can++, rx_index++)
                {
                    int can_message_idnex = int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.NODE_RX_CANFD_MESSAGE_INDEX, i, tx_can));
                    can_Network.can_nodes[i].rx_can_Messages[rx_index + 1].message_name = tsdb_get_can_db_info(AID, (int)DBCINFO.CANFD_MSG_NAME, can_message_idnex, 0);

                    can_Network.can_nodes[i].rx_can_Messages[rx_index + 1].signals = new string[int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CANFD_MSG_SIGNAL_COUNT, can_message_idnex, 0))];
                    for (int signalcount = 0; signalcount < can_Network.can_nodes[i].rx_can_Messages[rx_index + 1].signals.Length; signalcount++)
                    {
                        can_Network.can_nodes[i].rx_can_Messages[rx_index + 1].signals[signalcount] = tsdb_get_can_db_info(AID, (int)DBCINFO.SIGNAL_NAME, int.Parse(tsdb_get_can_db_info(AID, (int)DBCINFO.CANFD_MSG_SIGNAL_NAME_INDEX, can_message_idnex, signalcount)), 0);
                    }
                }
            }
        }
    }


}
