// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// SecureValueHash represents TL type `secureValueHash#ed1ecdb0`.
// Secure value hash
//
// See https://core.telegram.org/constructor/secureValueHash for reference.
type SecureValueHash struct {
	// Secure value type
	Type SecureValueTypeClass
	// Hash
	Hash []byte
}

// SecureValueHashTypeID is TL type id of SecureValueHash.
const SecureValueHashTypeID = 0xed1ecdb0

// Ensuring interfaces in compile-time for SecureValueHash.
var (
	_ bin.Encoder     = &SecureValueHash{}
	_ bin.Decoder     = &SecureValueHash{}
	_ bin.BareEncoder = &SecureValueHash{}
	_ bin.BareDecoder = &SecureValueHash{}
)

func (s *SecureValueHash) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Type == nil) {
		return false
	}
	if !(s.Hash == nil) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (s *SecureValueHash) String() string {
	if s == nil {
		return "SecureValueHash(nil)"
	}
	type Alias SecureValueHash
	return fmt.Sprintf("SecureValueHash%+v", Alias(*s))
}

// FillFrom fills SecureValueHash from given interface.
func (s *SecureValueHash) FillFrom(from interface {
	GetType() (value SecureValueTypeClass)
	GetHash() (value []byte)
}) {
	s.Type = from.GetType()
	s.Hash = from.GetHash()
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*SecureValueHash) TypeID() uint32 {
	return SecureValueHashTypeID
}

// TypeName returns name of type in TL schema.
func (*SecureValueHash) TypeName() string {
	return "secureValueHash"
}

// TypeInfo returns info about TL type.
func (s *SecureValueHash) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "secureValueHash",
		ID:   SecureValueHashTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SecureValueHash) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode secureValueHash#ed1ecdb0 as nil")
	}
	b.PutID(SecureValueHashTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SecureValueHash) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode secureValueHash#ed1ecdb0 as nil")
	}
	if s.Type == nil {
		return fmt.Errorf("unable to encode secureValueHash#ed1ecdb0: field type is nil")
	}
	if err := s.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode secureValueHash#ed1ecdb0: field type: %w", err)
	}
	b.PutBytes(s.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (s *SecureValueHash) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode secureValueHash#ed1ecdb0 to nil")
	}
	if err := b.ConsumeID(SecureValueHashTypeID); err != nil {
		return fmt.Errorf("unable to decode secureValueHash#ed1ecdb0: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SecureValueHash) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode secureValueHash#ed1ecdb0 to nil")
	}
	{
		value, err := DecodeSecureValueType(b)
		if err != nil {
			return fmt.Errorf("unable to decode secureValueHash#ed1ecdb0: field type: %w", err)
		}
		s.Type = value
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode secureValueHash#ed1ecdb0: field hash: %w", err)
		}
		s.Hash = value
	}
	return nil
}

// GetType returns value of Type field.
func (s *SecureValueHash) GetType() (value SecureValueTypeClass) {
	if s == nil {
		return
	}
	return s.Type
}

// GetHash returns value of Hash field.
func (s *SecureValueHash) GetHash() (value []byte) {
	if s == nil {
		return
	}
	return s.Hash
}
