//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;

namespace Rebex.Samples
{
    /// <summary>
    /// Logger, which emits log entries with Tunnel(ID).
    /// </summary>
    public class TunnelLogWriter : LogWriterBase
    {
        readonly ILogWriter _logger;
        readonly int _id;
        readonly string _area;

        /// <summary>
        /// Initializes new tunnel logger.
        /// </summary>
        public TunnelLogWriter(ILogWriter logger, int tunnelId, string area = null)
        {
            _logger = logger;
            _id = tunnelId;
            _area = area;

            // set correct level
            if (_logger == null)
                Level = LogLevel.Off;
            else
                Level = _logger.Level;
        }

        /// <summary>
        /// Overrides default logic with specific Tunnel(ID) and AREA.
        /// </summary>
        public override void Write(LogLevel level, Type objectType, int objectId, string area, string message)
        {
            if (_logger != null)
            {
                _logger.Write(level, typeof(Tunnel), _id, _area ?? area, message);
            }
        }

        /// <summary>
        /// Overrides default logic with specific Tunnel(ID) and AREA.
        /// </summary>
        public override void Write(LogLevel level, Type objectType, int objectId, string area, string message, byte[] buffer, int offset, int length)
        {
            if (_logger != null)
            {
                _logger.Write(level, typeof(Tunnel), _id, _area ?? area, message, buffer, offset, length);
            }
        }

        /// <summary>
        /// Gets tunnel logger for specific AREA - typically IN or OUT.
        /// </summary>
        public TunnelLogWriter GetSpecific(string area)
        {
            return new TunnelLogWriter(_logger, _id, area);
        }
    }
}
