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

// AccountSetMainProfileTabRequest represents TL type `account.setMainProfileTab#5dee78b0`.
// Changes the main profile tab of the current user, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#tabs
//
// See https://core.telegram.org/method/account.setMainProfileTab for reference.
type AccountSetMainProfileTabRequest struct {
	// The tab to set as main tab.
	Tab ProfileTabClass
}

// AccountSetMainProfileTabRequestTypeID is TL type id of AccountSetMainProfileTabRequest.
const AccountSetMainProfileTabRequestTypeID = 0x5dee78b0

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

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

	return true
}

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

// FillFrom fills AccountSetMainProfileTabRequest from given interface.
func (s *AccountSetMainProfileTabRequest) FillFrom(from interface {
	GetTab() (value ProfileTabClass)
}) {
	s.Tab = from.GetTab()
}

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

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

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

// Encode implements bin.Encoder.
func (s *AccountSetMainProfileTabRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.setMainProfileTab#5dee78b0 as nil")
	}
	b.PutID(AccountSetMainProfileTabRequestTypeID)
	return s.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (s *AccountSetMainProfileTabRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode account.setMainProfileTab#5dee78b0 to nil")
	}
	{
		value, err := DecodeProfileTab(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.setMainProfileTab#5dee78b0: field tab: %w", err)
		}
		s.Tab = value
	}
	return nil
}

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

// AccountSetMainProfileTab invokes method account.setMainProfileTab#5dee78b0 returning error if any.
// Changes the main profile tab of the current user, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/profile#tabs
//
// See https://core.telegram.org/method/account.setMainProfileTab for reference.
func (c *Client) AccountSetMainProfileTab(ctx context.Context, tab ProfileTabClass) (bool, error) {
	var result BoolBox

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