namespace MinicsConsole.Hubs.RT;

public class RTProvider_TLV(ConfigFileOperator configFileOperator, ILog log) : ITlvProvider
{
    void ITlvProvider.Connected(UniversalNetFrame451.IO.TcpConnection connection)
    {
        log.Info($"RT TLV Client Connected {connection.RemoteEndPoint.Address}:{connection.RemoteEndPoint.Port}");
    }

    void ITlvProvider.Disconnected(UniversalNetFrame451.IO.TcpConnection connection)
    {
        log.Info($"RT TLV Client DisConnected {connection.RemoteEndPoint.Address}:{connection.RemoteEndPoint.Port}");
    }

    void ITlvProvider.Received(TlvData data)
    {
        if (data.RawData is null || data.RawData.Length == 0)
        {
            //Log
            return;
        }

        switch (data.Tag)
        {
            case 1:
                string fileName = Encoding.UTF8.GetString(data.RawData);

                _ = configFileOperator.SetConfigFile(fileName) switch
                {
                    true => log.Info($"RT Select TempConfig File:{fileName}"),
                    false => log.Error($"RT Select TempConfig Failed! File:{fileName} "),
                };
                break;

            default:
                break;
        }
    }


    TlvData ITlvProvider.RequestReply(TlvData tlvData)
    {
        return tlvData;
    }
}
