using adas_core.Domain.Enums;
using MongoDB.Bson;
using Newtonsoft.Json;

namespace adas_core.Domain.Models.MongoModels;

/// <summary>
/// Represents a relay class that provides functionality for forwarding or passing through data, commands, or notifications between components.
/// </summary>
/// <remarks>
/// This class serves as an intermediary mechanism, commonly used to decouple producers and consumers of information.
/// </remarks>
public class Relay
{
    public ObjectId Id { get; set; }
    
    public RelayEnum.Type Type { get; set; } = RelayEnum.Type.Door;

    public string? Driver { get; init; }
    
    public string Ip { get; init; } = string.Empty;
    
    public int Port { get; init; }
    
    public int RelayNumber { get; init; } = 1;
    
    public string? RelayName { get; init; }

    public int Total { get; init; } = 1;
    
    public int RefreshTime { get; set; } = 60;
    
    public bool Open { get; set; }

    public RelayEnum.Status Status { get; set; } = RelayEnum.Status.NotInitialized;
    
    public string? Username { get; init; }
    
    public string? Password { get; init; }

    public RelayEnum.Mode Mode { get; set; } = RelayEnum.Mode.OpenedOnClosedOff;

    public int? RebootDelay { get; set; }

    public bool Cache { get; set; } = true;
    public bool InUse { get; private set; }
    
    public RelayEnum.Status? ManualRelayStatus { get; set; }

    /// <summary>
    /// Determines whether the current <see cref="Relay"/> instance is equal to another <see cref="Relay"/> by comparing all of their properties (Driver, Ip, Port, RelayNumber, RelayName, Total, Username, and Password).
    /// </summary>
    /// <param name="other">The other <see cref="Relay"/> instance to compare against the current one.</param>
    /// <returns><c>true</c> if all properties of both relays match; otherwise, <c>false</c>.</returns>
    public bool Equals(Relay other)
        {
            return Driver == other.Driver && Ip == other.Ip && Port == other.Port && RelayNumber == other.RelayNumber &&
                   RelayName == other.RelayName && Total == other.Total && Username == other.Username &&
                   Password == other.Password;
        }

    /// <summary>
    /// Returns a JSON string representation of the current object, serializing all its public properties via JsonConvert.
    /// </summary>
    /// <returns>A JSON-formatted string that represents the current object.</returns>
    public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
}