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

// AccountSaveMusicRequest represents TL type `account.saveMusic#b26732a9`.
// Adds or removes a song from the current user's profile see here »¹ for more info on
// the music tab of the profile page.
//
// Links:
//  1. https://core.telegram.org/api/profile#music
//
// See https://core.telegram.org/method/account.saveMusic for reference.
type AccountSaveMusicRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, removes the song.
	Unsave bool
	// The song to add or remove; can be an already added song when reordering songs with
	// after_id. Adding an already added song will never re-add it, only move it to the top
	// of the song list (or after the song passed in after_id).
	ID InputDocumentClass
	// If set, the song will be added after the passed song (must be already pinned on the
	// profile).
	//
	// Use SetAfterID and GetAfterID helpers.
	AfterID InputDocumentClass
}

// AccountSaveMusicRequestTypeID is TL type id of AccountSaveMusicRequest.
const AccountSaveMusicRequestTypeID = 0xb26732a9

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

func (s *AccountSaveMusicRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Unsave == false) {
		return false
	}
	if !(s.ID == nil) {
		return false
	}
	if !(s.AfterID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountSaveMusicRequest from given interface.
func (s *AccountSaveMusicRequest) FillFrom(from interface {
	GetUnsave() (value bool)
	GetID() (value InputDocumentClass)
	GetAfterID() (value InputDocumentClass, ok bool)
}) {
	s.Unsave = from.GetUnsave()
	s.ID = from.GetID()
	if val, ok := from.GetAfterID(); ok {
		s.AfterID = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *AccountSaveMusicRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.saveMusic",
		ID:   AccountSaveMusicRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Unsave",
			SchemaName: "unsave",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "AfterID",
			SchemaName: "after_id",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *AccountSaveMusicRequest) SetFlags() {
	if !(s.Unsave == false) {
		s.Flags.Set(0)
	}
	if !(s.AfterID == nil) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *AccountSaveMusicRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.saveMusic#b26732a9 as nil")
	}
	b.PutID(AccountSaveMusicRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *AccountSaveMusicRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.saveMusic#b26732a9 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.saveMusic#b26732a9: field flags: %w", err)
	}
	if s.ID == nil {
		return fmt.Errorf("unable to encode account.saveMusic#b26732a9: field id is nil")
	}
	if err := s.ID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.saveMusic#b26732a9: field id: %w", err)
	}
	if s.Flags.Has(1) {
		if s.AfterID == nil {
			return fmt.Errorf("unable to encode account.saveMusic#b26732a9: field after_id is nil")
		}
		if err := s.AfterID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.saveMusic#b26732a9: field after_id: %w", err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *AccountSaveMusicRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode account.saveMusic#b26732a9 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.saveMusic#b26732a9: field flags: %w", err)
		}
	}
	s.Unsave = s.Flags.Has(0)
	{
		value, err := DecodeInputDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.saveMusic#b26732a9: field id: %w", err)
		}
		s.ID = value
	}
	if s.Flags.Has(1) {
		value, err := DecodeInputDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.saveMusic#b26732a9: field after_id: %w", err)
		}
		s.AfterID = value
	}
	return nil
}

// SetUnsave sets value of Unsave conditional field.
func (s *AccountSaveMusicRequest) SetUnsave(value bool) {
	if value {
		s.Flags.Set(0)
		s.Unsave = true
	} else {
		s.Flags.Unset(0)
		s.Unsave = false
	}
}

// GetUnsave returns value of Unsave conditional field.
func (s *AccountSaveMusicRequest) GetUnsave() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetID returns value of ID field.
func (s *AccountSaveMusicRequest) GetID() (value InputDocumentClass) {
	if s == nil {
		return
	}
	return s.ID
}

// SetAfterID sets value of AfterID conditional field.
func (s *AccountSaveMusicRequest) SetAfterID(value InputDocumentClass) {
	s.Flags.Set(1)
	s.AfterID = value
}

// GetAfterID returns value of AfterID conditional field and
// boolean which is true if field was set.
func (s *AccountSaveMusicRequest) GetAfterID() (value InputDocumentClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.AfterID, true
}

// GetIDAsNotEmpty returns mapped value of ID field.
func (s *AccountSaveMusicRequest) GetIDAsNotEmpty() (*InputDocument, bool) {
	return s.ID.AsNotEmpty()
}

// GetAfterIDAsNotEmpty returns mapped value of AfterID conditional field and
// boolean which is true if field was set.
func (s *AccountSaveMusicRequest) GetAfterIDAsNotEmpty() (*InputDocument, bool) {
	if value, ok := s.GetAfterID(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}

// AccountSaveMusic invokes method account.saveMusic#b26732a9 returning error if any.
// Adds or removes a song from the current user's profile see here »¹ for more info on
// the music tab of the profile page.
//
// Links:
//  1. https://core.telegram.org/api/profile#music
//
// Possible errors:
//
//	400 DOCUMENT_INVALID: The specified document is invalid.
//
// See https://core.telegram.org/method/account.saveMusic for reference.
func (c *Client) AccountSaveMusic(ctx context.Context, request *AccountSaveMusicRequest) (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
}
