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

// ChannelsSetMainProfileTabRequest represents TL type `channels.setMainProfileTab#3583fcb1`.
// Changes the main profile tab of a channel, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#tabs
//
// See https://core.telegram.org/method/channels.setMainProfileTab for reference.
type ChannelsSetMainProfileTabRequest struct {
	// The channel.
	Channel InputChannelClass
	// The tab to set as main tab.
	Tab ProfileTabClass
}

// ChannelsSetMainProfileTabRequestTypeID is TL type id of ChannelsSetMainProfileTabRequest.
const ChannelsSetMainProfileTabRequestTypeID = 0x3583fcb1

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

func (s *ChannelsSetMainProfileTabRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Channel == nil) {
		return false
	}
	if !(s.Tab == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsSetMainProfileTabRequest from given interface.
func (s *ChannelsSetMainProfileTabRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
	GetTab() (value ProfileTabClass)
}) {
	s.Channel = from.GetChannel()
	s.Tab = from.GetTab()
}

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

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

// TypeInfo returns info about TL type.
func (s *ChannelsSetMainProfileTabRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.setMainProfileTab",
		ID:   ChannelsSetMainProfileTabRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "Tab",
			SchemaName: "tab",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *ChannelsSetMainProfileTabRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode channels.setMainProfileTab#3583fcb1 as nil")
	}
	b.PutID(ChannelsSetMainProfileTabRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *ChannelsSetMainProfileTabRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode channels.setMainProfileTab#3583fcb1 as nil")
	}
	if s.Channel == nil {
		return fmt.Errorf("unable to encode channels.setMainProfileTab#3583fcb1: field channel is nil")
	}
	if err := s.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.setMainProfileTab#3583fcb1: field channel: %w", err)
	}
	if s.Tab == nil {
		return fmt.Errorf("unable to encode channels.setMainProfileTab#3583fcb1: field tab is nil")
	}
	if err := s.Tab.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.setMainProfileTab#3583fcb1: field tab: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *ChannelsSetMainProfileTabRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode channels.setMainProfileTab#3583fcb1 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.setMainProfileTab#3583fcb1: field channel: %w", err)
		}
		s.Channel = value
	}
	{
		value, err := DecodeProfileTab(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.setMainProfileTab#3583fcb1: field tab: %w", err)
		}
		s.Tab = value
	}
	return nil
}

// GetChannel returns value of Channel field.
func (s *ChannelsSetMainProfileTabRequest) GetChannel() (value InputChannelClass) {
	if s == nil {
		return
	}
	return s.Channel
}

// GetTab returns value of Tab field.
func (s *ChannelsSetMainProfileTabRequest) GetTab() (value ProfileTabClass) {
	if s == nil {
		return
	}
	return s.Tab
}

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (s *ChannelsSetMainProfileTabRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return s.Channel.AsNotEmpty()
}

// ChannelsSetMainProfileTab invokes method channels.setMainProfileTab#3583fcb1 returning error if any.
// Changes the main profile tab of a channel, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#tabs
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//
// See https://core.telegram.org/method/channels.setMainProfileTab for reference.
func (c *Client) ChannelsSetMainProfileTab(ctx context.Context, request *ChannelsSetMainProfileTabRequest) (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
}
