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

// ChannelsSetDiscussionGroupRequest represents TL type `channels.setDiscussionGroup#40582bb2`.
// Associate a group to a channel as discussion group¹ for that channel
//
// Links:
//  1. https://core.telegram.org/api/discussion
//
// See https://core.telegram.org/method/channels.setDiscussionGroup for reference.
type ChannelsSetDiscussionGroupRequest struct {
	// Channel
	Broadcast InputChannelClass
	// Discussion group¹ to associate to the channel
	//
	// Links:
	//  1) https://core.telegram.org/api/discussion
	Group InputChannelClass
}

// ChannelsSetDiscussionGroupRequestTypeID is TL type id of ChannelsSetDiscussionGroupRequest.
const ChannelsSetDiscussionGroupRequestTypeID = 0x40582bb2

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

func (s *ChannelsSetDiscussionGroupRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Broadcast == nil) {
		return false
	}
	if !(s.Group == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsSetDiscussionGroupRequest from given interface.
func (s *ChannelsSetDiscussionGroupRequest) FillFrom(from interface {
	GetBroadcast() (value InputChannelClass)
	GetGroup() (value InputChannelClass)
}) {
	s.Broadcast = from.GetBroadcast()
	s.Group = from.GetGroup()
}

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

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

// TypeInfo returns info about TL type.
func (s *ChannelsSetDiscussionGroupRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.setDiscussionGroup",
		ID:   ChannelsSetDiscussionGroupRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Broadcast",
			SchemaName: "broadcast",
		},
		{
			Name:       "Group",
			SchemaName: "group",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *ChannelsSetDiscussionGroupRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode channels.setDiscussionGroup#40582bb2 as nil")
	}
	b.PutID(ChannelsSetDiscussionGroupRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *ChannelsSetDiscussionGroupRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode channels.setDiscussionGroup#40582bb2 as nil")
	}
	if s.Broadcast == nil {
		return fmt.Errorf("unable to encode channels.setDiscussionGroup#40582bb2: field broadcast is nil")
	}
	if err := s.Broadcast.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.setDiscussionGroup#40582bb2: field broadcast: %w", err)
	}
	if s.Group == nil {
		return fmt.Errorf("unable to encode channels.setDiscussionGroup#40582bb2: field group is nil")
	}
	if err := s.Group.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.setDiscussionGroup#40582bb2: field group: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *ChannelsSetDiscussionGroupRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode channels.setDiscussionGroup#40582bb2 to nil")
	}
	if err := b.ConsumeID(ChannelsSetDiscussionGroupRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.setDiscussionGroup#40582bb2: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *ChannelsSetDiscussionGroupRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode channels.setDiscussionGroup#40582bb2 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.setDiscussionGroup#40582bb2: field broadcast: %w", err)
		}
		s.Broadcast = value
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.setDiscussionGroup#40582bb2: field group: %w", err)
		}
		s.Group = value
	}
	return nil
}

// GetBroadcast returns value of Broadcast field.
func (s *ChannelsSetDiscussionGroupRequest) GetBroadcast() (value InputChannelClass) {
	if s == nil {
		return
	}
	return s.Broadcast
}

// GetGroup returns value of Group field.
func (s *ChannelsSetDiscussionGroupRequest) GetGroup() (value InputChannelClass) {
	if s == nil {
		return
	}
	return s.Group
}

// GetBroadcastAsNotEmpty returns mapped value of Broadcast field.
func (s *ChannelsSetDiscussionGroupRequest) GetBroadcastAsNotEmpty() (NotEmptyInputChannel, bool) {
	return s.Broadcast.AsNotEmpty()
}

// GetGroupAsNotEmpty returns mapped value of Group field.
func (s *ChannelsSetDiscussionGroupRequest) GetGroupAsNotEmpty() (NotEmptyInputChannel, bool) {
	return s.Group.AsNotEmpty()
}

// ChannelsSetDiscussionGroup invokes method channels.setDiscussionGroup#40582bb2 returning error if any.
// Associate a group to a channel as discussion group¹ for that channel
//
// Links:
//  1. https://core.telegram.org/api/discussion
//
// Possible errors:
//
//	400 BROADCAST_ID_INVALID: Broadcast ID invalid.
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	403 CHAT_WRITE_FORBIDDEN: You can't write in this chat.
//	400 LINK_NOT_MODIFIED: Discussion link not modified.
//	400 MEGAGROUP_ID_INVALID: Invalid supergroup ID.
//	400 MEGAGROUP_PREHISTORY_HIDDEN: Group with hidden history for new members can't be set as discussion groups.
//
// See https://core.telegram.org/method/channels.setDiscussionGroup for reference.
func (c *Client) ChannelsSetDiscussionGroup(ctx context.Context, request *ChannelsSetDiscussionGroupRequest) (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
}
