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

// ChannelsGetSendAsRequest represents TL type `channels.getSendAs#e785a43f`.
// Obtains a list of peers that can be used to send messages in a specific group
//
// See https://core.telegram.org/method/channels.getSendAs for reference.
type ChannelsGetSendAsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, fetches the list of peers that can be used to send paid reactions¹ to
	// messages of a specific peer.
	//
	// Links:
	//  1) https://core.telegram.org/api/reactions#paid-reactions
	ForPaidReactions bool
	// ForLiveStories field of ChannelsGetSendAsRequest.
	ForLiveStories bool
	// The group where we intend to send messages
	Peer InputPeerClass
}

// ChannelsGetSendAsRequestTypeID is TL type id of ChannelsGetSendAsRequest.
const ChannelsGetSendAsRequestTypeID = 0xe785a43f

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

func (g *ChannelsGetSendAsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.ForPaidReactions == false) {
		return false
	}
	if !(g.ForLiveStories == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsGetSendAsRequest from given interface.
func (g *ChannelsGetSendAsRequest) FillFrom(from interface {
	GetForPaidReactions() (value bool)
	GetForLiveStories() (value bool)
	GetPeer() (value InputPeerClass)
}) {
	g.ForPaidReactions = from.GetForPaidReactions()
	g.ForLiveStories = from.GetForLiveStories()
	g.Peer = from.GetPeer()
}

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

// TypeName returns name of type in TL schema.
func (*ChannelsGetSendAsRequest) TypeName() string {
	return "channels.getSendAs"
}

// TypeInfo returns info about TL type.
func (g *ChannelsGetSendAsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.getSendAs",
		ID:   ChannelsGetSendAsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ForPaidReactions",
			SchemaName: "for_paid_reactions",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "ForLiveStories",
			SchemaName: "for_live_stories",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *ChannelsGetSendAsRequest) SetFlags() {
	if !(g.ForPaidReactions == false) {
		g.Flags.Set(0)
	}
	if !(g.ForLiveStories == false) {
		g.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (g *ChannelsGetSendAsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode channels.getSendAs#e785a43f as nil")
	}
	b.PutID(ChannelsGetSendAsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *ChannelsGetSendAsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode channels.getSendAs#e785a43f as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.getSendAs#e785a43f: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode channels.getSendAs#e785a43f: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.getSendAs#e785a43f: field peer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *ChannelsGetSendAsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode channels.getSendAs#e785a43f to nil")
	}
	if err := b.ConsumeID(ChannelsGetSendAsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.getSendAs#e785a43f: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *ChannelsGetSendAsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode channels.getSendAs#e785a43f to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode channels.getSendAs#e785a43f: field flags: %w", err)
		}
	}
	g.ForPaidReactions = g.Flags.Has(0)
	g.ForLiveStories = g.Flags.Has(1)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.getSendAs#e785a43f: field peer: %w", err)
		}
		g.Peer = value
	}
	return nil
}

// SetForPaidReactions sets value of ForPaidReactions conditional field.
func (g *ChannelsGetSendAsRequest) SetForPaidReactions(value bool) {
	if value {
		g.Flags.Set(0)
		g.ForPaidReactions = true
	} else {
		g.Flags.Unset(0)
		g.ForPaidReactions = false
	}
}

// GetForPaidReactions returns value of ForPaidReactions conditional field.
func (g *ChannelsGetSendAsRequest) GetForPaidReactions() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

// SetForLiveStories sets value of ForLiveStories conditional field.
func (g *ChannelsGetSendAsRequest) SetForLiveStories(value bool) {
	if value {
		g.Flags.Set(1)
		g.ForLiveStories = true
	} else {
		g.Flags.Unset(1)
		g.ForLiveStories = false
	}
}

// GetForLiveStories returns value of ForLiveStories conditional field.
func (g *ChannelsGetSendAsRequest) GetForLiveStories() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(1)
}

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

// ChannelsGetSendAs invokes method channels.getSendAs#e785a43f returning error if any.
// Obtains a list of peers that can be used to send messages in a specific group
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 CHAT_ID_INVALID: The provided chat id is invalid.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/channels.getSendAs for reference.
func (c *Client) ChannelsGetSendAs(ctx context.Context, request *ChannelsGetSendAsRequest) (*ChannelsSendAsPeers, error) {
	var result ChannelsSendAsPeers

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
