using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Ietf.Rfc2460
{
    internal class Ipv6Header : Validatable
    {
        public Ipv6Header(byte[] buffer)
        {
            if (buffer.Length < 40)
            {
                Valid = false;
                return;
            }
            MemoryStream ms = new MemoryStream(buffer, false);
            byte readUInt8 = ms.ReadUInt8();
            Version = (readUInt8 & 0xf0) >> 4;
            TrafficClass = (readUInt8 & 0x0f);
            TrafficClass <<= 4;

            readUInt8 = ms.ReadUInt8();
            TrafficClass += (readUInt8 & 0xf0) >> 4;
            FlowLabel = (readUInt8 & 0x0f);

            FlowLabel += ms.ReadUInt16BE();

            PayloadLength = ms.ReadUInt16BE();
            NextHeader = ms.ReadUInt8();
            HopLimit = ms.ReadUInt8();
            SourceAddress = new IPAddress(ms.ReadBytes(16));
            DestinationAddress = new IPAddress(ms.ReadBytes(16));
            Valid = true;
        }

        public IPAddress DestinationAddress { get; set; }

        public IPAddress SourceAddress { get; set; }

        public byte HopLimit { get; set; }

        public byte NextHeader { get; set; }

        public ushort PayloadLength { get; set; }

        public int Version { get; }
        
        public int TrafficClass { get; }

        public int FlowLabel { get; }
    }
}
