package tdesktop

import (
	

	
	
)

// MTPConfigEnvironment is enum of config environment.
type MTPConfigEnvironment int32

func ( MTPConfigEnvironment) () bool {
	return  == 0 ||  == 1
}

// String implements fmt.Stringer.
func ( MTPConfigEnvironment) () string {
	switch  {
	case 0:
		return "production"
	case 1:
		return "test"
	default:
		return "unknown"
	}
}

// Test denotes that environment is test.
func ( MTPConfigEnvironment) () bool {
	return  == 1
}

// MTPConfig is a Telegram Desktop storage structure which stores MTProto config info.
//
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/mtproto/mtproto_config.h.
type MTPConfig struct {
	Environment              MTPConfigEnvironment
	DCOptions                MTPDCOptions
	ChatSizeMax              int32  // default: 200
	MegagroupSizeMax         int32  // default: 10000
	ForwardedCountMax        int32  // default: 100
	OnlineUpdatePeriod       int32  // default: 120000
	OfflineBlurTimeout       int32  // default: 5000
	OfflineIdleTimeout       int32  // default: 30000
	OnlineFocusTimeout       int32  // default: 1000
	OnlineCloudTimeout       int32  // default: 300000
	NotifyCloudDelay         int32  // default: 30000
	NotifyDefaultDelay       int32  // default: 1500
	SavedGifsLimit           int32  // default: 200
	EditTimeLimit            int32  // default: 172800
	RevokeTimeLimit          int32  // default: 172800
	RevokePrivateTimeLimit   int32  // default: 172800
	RevokePrivateInbox       bool   // default: false
	StickersRecentLimit      int32  // default: 30
	StickersFavedLimit       int32  // default: 5
	PinnedDialogsCountMax    int32  // default: 5
	PinnedDialogsInFolderMax int32  // default: 100
	InternalLinksDomain      string // default: "https://t.me/"
	ChannelsReadMediaPeriod  int32  // default: 86400 * 7
	CallReceiveTimeoutMs     int32  // default: 20000
	CallRingTimeoutMs        int32  // default: 90000
	CallConnectTimeoutMs     int32  // default: 30000
	CallPacketTimeoutMs      int32  // default: 10000
	WebFileDCID              int32  // default: 4
	TxtDomainString          string // default: ""
	PhoneCallsEnabled        bool   // default: true
	BlockedMode              bool   // default: false
	CaptionLengthMax         int32  // default: 1024
}

// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/storage/storage_account.cpp#L938.
func ( *tdesktopFile,  crypto.Key) (MTPConfig, error) {
	,  := .readArray()
	if  != nil {
		return MTPConfig{}, errors.Wrap(, "read encrypted data")
	}

	,  := decryptLocal(, )
	if  != nil {
		return MTPConfig{}, errors.Wrap(, "decrypt data")
	}
	// Skip decrypted data length (uint32).
	 = [4:]
	 := qtReader{buf: bin.Buffer{Buf: }}

	,  := .subArray()
	if  != nil {
		return MTPConfig{}, errors.Wrap(, "read config array")
	}

	var  MTPConfig
	if  := .deserialize(&);  != nil {
		return MTPConfig{}, errors.Wrap(, "deserialize MTPConfig")
	}
	return , 
}

func ( *MTPConfig) ( *qtReader) error {
	,  := .readInt32()
	if  != nil {
		return errors.Wrap(, "read version")
	}
	if  != kVersion {
		return errors.Errorf("wrong version (expected %d, got %d)", kVersion, )
	}

	,  := .readInt32()
	if  != nil {
		return errors.Wrap(, "read environment")
	}
	.Environment = MTPConfigEnvironment()
	if !.Environment.valid() {
		return errors.Errorf("invalid environment %d", )
	}

	{
		,  := .subArray()
		if  != nil {
			return 
		}
		if  := .DCOptions.deserialize(&);  != nil {
			return errors.Wrap(, "read DC options")
		}
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	,  := .readUint32()
	if  != nil {
		return errors.Wrap(, "read revokePrivateInbox")
	}
	.RevokePrivateInbox =  == 1

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

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

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

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

	,  := .readString()
	if  != nil {
		return errors.Wrap(, "read internalLinksDomain")
	}
	.InternalLinksDomain = 

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

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

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

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

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

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

	,  := .readString()
	if  != nil {
		return errors.Wrap(, "read txtDomainString")
	}
	.TxtDomainString = 

	,  := .readUint32()
	if  != nil {
		return errors.Wrap(, "read phoneCallsEnabled")
	}
	.PhoneCallsEnabled =  == 1

	,  := .readUint32()
	if  != nil {
		return errors.Wrap(, "read blockedMode")
	}
	.BlockedMode =  == 1

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

	return nil
}