package tdesktop

import (
	

	
)

// MTPDCOption is a Telegram Desktop storage structure which stores DC info.
//
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/mtproto/mtproto_dc_options.h.
type MTPDCOption struct {
	ID     int32
	Flags  bin.Fields
	Port   int32
	IP     string
	Secret []byte
}

// IPv6 denotes that the specified IP is an IPv6 address.
func ( MTPDCOption) () bool {
	return .Flags.Has(0)
}

// MediaOnly denotes that this DC should only be used to download or upload files.
func ( MTPDCOption) () bool {
	return .Flags.Has(1)
}

// TCPOOnly denotes that this DC only supports connection with transport obfuscation.
func ( MTPDCOption) () bool {
	return .Flags.Has(2)
}

// CDN denotes that this is a CDN DC.
func ( MTPDCOption) () bool {
	return .Flags.Has(3)
}

// Static denotes that this IP should be used when connecting through a proxy.
func ( MTPDCOption) () bool {
	return .Flags.Has(4)
}

func ( *MTPDCOption) ( *qtReader,  int32) error {
	,  := .readInt32()
	if  != nil {
		return errors.Wrap(, "read id")
	}
	.ID = 

	,  := .readUint32()
	if  != nil {
		return errors.Wrap(, "read flags")
	}
	.Flags = bin.Fields()

	,  := .readInt32()
	if  != nil {
		return errors.Wrap(, "read port")
	}
	.Port = 

	const  = 45
	,  := .readString()
	if  != nil {
		return errors.Wrap(, "read ip")
	}
	if  := len();  >  {
		return errors.Errorf("too big IP string (%d > %d)", , )
	}
	.IP = 

	if  > 0 {
		const  = 32
		,  := .readBytes()
		if  != nil {
			return errors.Wrap(, "read secret")
		}
		if  := len();  >  {
			return errors.Errorf("too big DC secret (%d > %d)", , )
		}
		.Secret = 
	}

	return nil
}

// MTPDCOptions is a Telegram Desktop storage structure which stores DCs info.
//
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/mtproto/mtproto_dc_options.cpp#L479.
type MTPDCOptions struct {
	Options []MTPDCOption
}

func ( *MTPDCOptions) ( *qtReader) error {
	,  := .readInt32()
	if  != nil {
		return errors.Wrap(, "read version")
	}

	var  int32
	if  < 0 {
		 = -
	}

	var  int32
	if  > 0 {
		,  := .readInt32()
		if  != nil {
			return errors.Wrap(, "read count")
		}
		 = 
	} else {
		 = 
	}

	for  := 0;  < int(); ++ {
		var  MTPDCOption
		if  := .deserialize(, );  != nil {
			return errors.Errorf("read option %d: %w", , )
		}
		.Options = append(.Options, )
	}

	// TODO(tdakkota): Read CDN keys.
	return nil
}