// 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{}
)

// BotsSetCustomVerificationRequest represents TL type `bots.setCustomVerification#8b89dfbd`.
// Verify a user or chat on behalf of an organization »¹.
//
// Links:
//  1. https://core.telegram.org/api/bots/verification
//
// See https://core.telegram.org/method/bots.setCustomVerification for reference.
type BotsSetCustomVerificationRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, adds the verification; otherwise removes verification.
	Enabled bool
	// Must not be set if invoked by a bot, must be set to the ID of an owned bot if invoked
	// by a user.
	//
	// Use SetBot and GetBot helpers.
	Bot InputUserClass
	// The peer to verify
	Peer InputPeerClass
	// Custom description for the verification, the UTF-8 length limit for this field is
	// contained in bot_verification_description_length_limit »¹. If not set, Was verified
	// by organization "organization_name" will be used as description.
	//
	// Links:
	//  1) https://core.telegram.org/api/config#bot-verification-description-length-limit
	//
	// Use SetCustomDescription and GetCustomDescription helpers.
	CustomDescription string
}

// BotsSetCustomVerificationRequestTypeID is TL type id of BotsSetCustomVerificationRequest.
const BotsSetCustomVerificationRequestTypeID = 0x8b89dfbd

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

func (s *BotsSetCustomVerificationRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Enabled == false) {
		return false
	}
	if !(s.Bot == nil) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.CustomDescription == "") {
		return false
	}

	return true
}

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

// FillFrom fills BotsSetCustomVerificationRequest from given interface.
func (s *BotsSetCustomVerificationRequest) FillFrom(from interface {
	GetEnabled() (value bool)
	GetBot() (value InputUserClass, ok bool)
	GetPeer() (value InputPeerClass)
	GetCustomDescription() (value string, ok bool)
}) {
	s.Enabled = from.GetEnabled()
	if val, ok := from.GetBot(); ok {
		s.Bot = val
	}

	s.Peer = from.GetPeer()
	if val, ok := from.GetCustomDescription(); ok {
		s.CustomDescription = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*BotsSetCustomVerificationRequest) TypeName() string {
	return "bots.setCustomVerification"
}

// TypeInfo returns info about TL type.
func (s *BotsSetCustomVerificationRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.setCustomVerification",
		ID:   BotsSetCustomVerificationRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Enabled",
			SchemaName: "enabled",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "Bot",
			SchemaName: "bot",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "CustomDescription",
			SchemaName: "custom_description",
			Null:       !s.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *BotsSetCustomVerificationRequest) SetFlags() {
	if !(s.Enabled == false) {
		s.Flags.Set(1)
	}
	if !(s.Bot == nil) {
		s.Flags.Set(0)
	}
	if !(s.CustomDescription == "") {
		s.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (s *BotsSetCustomVerificationRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode bots.setCustomVerification#8b89dfbd as nil")
	}
	b.PutID(BotsSetCustomVerificationRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *BotsSetCustomVerificationRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode bots.setCustomVerification#8b89dfbd as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.setCustomVerification#8b89dfbd: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		if s.Bot == nil {
			return fmt.Errorf("unable to encode bots.setCustomVerification#8b89dfbd: field bot is nil")
		}
		if err := s.Bot.Encode(b); err != nil {
			return fmt.Errorf("unable to encode bots.setCustomVerification#8b89dfbd: field bot: %w", err)
		}
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode bots.setCustomVerification#8b89dfbd: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.setCustomVerification#8b89dfbd: field peer: %w", err)
	}
	if s.Flags.Has(2) {
		b.PutString(s.CustomDescription)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *BotsSetCustomVerificationRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode bots.setCustomVerification#8b89dfbd to nil")
	}
	if err := b.ConsumeID(BotsSetCustomVerificationRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.setCustomVerification#8b89dfbd: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *BotsSetCustomVerificationRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode bots.setCustomVerification#8b89dfbd to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.setCustomVerification#8b89dfbd: field flags: %w", err)
		}
	}
	s.Enabled = s.Flags.Has(1)
	if s.Flags.Has(0) {
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.setCustomVerification#8b89dfbd: field bot: %w", err)
		}
		s.Bot = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.setCustomVerification#8b89dfbd: field peer: %w", err)
		}
		s.Peer = value
	}
	if s.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.setCustomVerification#8b89dfbd: field custom_description: %w", err)
		}
		s.CustomDescription = value
	}
	return nil
}

// SetEnabled sets value of Enabled conditional field.
func (s *BotsSetCustomVerificationRequest) SetEnabled(value bool) {
	if value {
		s.Flags.Set(1)
		s.Enabled = true
	} else {
		s.Flags.Unset(1)
		s.Enabled = false
	}
}

// GetEnabled returns value of Enabled conditional field.
func (s *BotsSetCustomVerificationRequest) GetEnabled() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(1)
}

// SetBot sets value of Bot conditional field.
func (s *BotsSetCustomVerificationRequest) SetBot(value InputUserClass) {
	s.Flags.Set(0)
	s.Bot = value
}

// GetBot returns value of Bot conditional field and
// boolean which is true if field was set.
func (s *BotsSetCustomVerificationRequest) GetBot() (value InputUserClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Bot, true
}

// GetPeer returns value of Peer field.
func (s *BotsSetCustomVerificationRequest) GetPeer() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.Peer
}

// SetCustomDescription sets value of CustomDescription conditional field.
func (s *BotsSetCustomVerificationRequest) SetCustomDescription(value string) {
	s.Flags.Set(2)
	s.CustomDescription = value
}

// GetCustomDescription returns value of CustomDescription conditional field and
// boolean which is true if field was set.
func (s *BotsSetCustomVerificationRequest) GetCustomDescription() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.CustomDescription, true
}

// BotsSetCustomVerification invokes method bots.setCustomVerification#8b89dfbd returning error if any.
// Verify a user or chat on behalf of an organization »¹.
//
// Links:
//  1. https://core.telegram.org/api/bots/verification
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//	403 BOT_VERIFIER_FORBIDDEN: This bot cannot assign verification icons.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/bots.setCustomVerification for reference.
func (c *Client) BotsSetCustomVerification(ctx context.Context, request *BotsSetCustomVerificationRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
