using System.Net.NetworkInformation;
using skyscraper5.Skyscraper.IO;

namespace skyscraper8.Ieee802_1AB.Model;

public class LldpPort
{
    public LldpPort(byte[] buffer)
    {
        MemoryStream ms = new MemoryStream(buffer);
        Subtype = ms.ReadUInt8();
        switch (Subtype)
        {
            case 3:
	            MacAddress = new PhysicalAddress(ms.ReadBytes(6));
	            break;
            case 5:
	            PortId = ms.ReadUTF8FixedLength((int)ms.GetAvailableBytes());
	            break;
            default:
	            throw new NotImplementedException(String.Format("Unknown Port Subtype {0}. Please share a sample of this stream.", Subtype));
		}
    }

    public PhysicalAddress MacAddress { get; set; }
    public string PortId { get; set; }
    public byte Subtype { get; private set; }

    protected bool Equals(LldpPort other)
    {
	    switch (Subtype)
	    {
		    case 3:
			    return MacAddress.Equals(other.MacAddress) && Subtype == other.Subtype;
		    case 5:
				return PortId == other.PortId && Subtype == other.Subtype;
		    default:
			    throw new NotImplementedException(String.Format("Unknown Port Subtype {0}. Please share a sample of this stream.", Subtype));
	    }
    }

    public override bool Equals(object? obj)
    {
	    if (ReferenceEquals(null, obj)) 
		    return false;
	    if (ReferenceEquals(this, obj)) 
		    return true;
	    if (obj.GetType() != this.GetType()) 
		    return false;
	    return Equals((LldpPort)obj);
    }

    public override int GetHashCode()
    {
	    switch (Subtype)
	    {
		    case 3:
			    return HashCode.Combine(PortId, MacAddress);
		    case 5:
				return HashCode.Combine(PortId, Subtype);
			default:
			    throw new NotImplementedException(String.Format("Unknown Port Subtype {0}. Please share a sample of this stream.", Subtype));
	    }

    }

    public override string ToString()
    {
	    switch (Subtype)
	    {
			case 3:
				return MacAddress.ToString();
            case 5:
	            return PortId;
		    default:
			    return String.Format("Port Subtype {0}", Subtype);
	    }
    }
}