package mtproxy

import (
	

	
)

// SecretType represents MTProxy secret type.
type SecretType int

const (
	// Simple is a basic MTProxy secret.
	Simple SecretType = iota + 1
	// Secured is dd-secret.
	Secured
	// TLS is fakeTLS MTProxy secret.
	// First byte should be ee.
	TLS
)

// Secret represents MTProxy secret.
type Secret struct {
	Secret    []byte
	Tag       byte
	CloakHost string
	Type      SecretType
}

// ExpectedCodec returns codec from secret tag if it exists.
func ( Secret) () ( codec.Codec,  bool) {
	switch .Tag {
	case codec.AbridgedClientStart[0]:
		 = codec.Abridged{}
	case codec.IntermediateClientStart[0]:
		 = codec.Intermediate{}
	case codec.PaddedIntermediateClientStart[0]:
		 = codec.PaddedIntermediate{}
	default:
		return nil, false
	}

	return , true
}

// ParseSecret checks and parses secret.
func ( []byte) (Secret, error) {
	 := Secret{
		Secret: ,
	}
	const  = 16

	switch {
	case len() == 1+:
		.Type = Secured

		.Tag = [0]
		 = [1:]
		.Secret = [:]
	case len() > :
		.Type = TLS

		.Tag = [0]
		 = [1:]
		.Secret = [:]
		.CloakHost = string([:])
	case len() == :
		.Type = Simple
	default:
		return Secret{}, errors.Errorf("invalid secret %q", string())
	}

	if .Type != Simple {
		switch .Tag {
		case codec.AbridgedClientStart[0],
			codec.IntermediateClientStart[0],
			codec.PaddedIntermediateClientStart[0]:
		default:
			return Secret{}, errors.Errorf("unknown tag %+x", .Tag)
		}
	}

	return , nil
}