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

// MessagesToggleSuggestedPostApprovalRequest represents TL type `messages.toggleSuggestedPostApproval#8107455c`.
// Approve or reject a suggested post »¹.
//
// Links:
//  1. https://core.telegram.org/api/suggested-posts
//
// See https://core.telegram.org/method/messages.toggleSuggestedPostApproval for reference.
type MessagesToggleSuggestedPostApprovalRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Reject the suggested post.
	Reject bool
	// Both for users and channels, must contain the ID of the direct messages monoforum »¹
	// (for channels, the topic ID is extracted automatically from the msg_id).
	//
	// Links:
	//  1) https://core.telegram.org/api/monoforum
	Peer InputPeerClass
	// ID of the suggestion message.
	MsgID int
	// Custom scheduling date.
	//
	// Use SetScheduleDate and GetScheduleDate helpers.
	ScheduleDate int
	// Optional comment for rejections (can only be used if reject is set).
	//
	// Use SetRejectComment and GetRejectComment helpers.
	RejectComment string
}

// MessagesToggleSuggestedPostApprovalRequestTypeID is TL type id of MessagesToggleSuggestedPostApprovalRequest.
const MessagesToggleSuggestedPostApprovalRequestTypeID = 0x8107455c

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

func (t *MessagesToggleSuggestedPostApprovalRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Flags.Zero()) {
		return false
	}
	if !(t.Reject == false) {
		return false
	}
	if !(t.Peer == nil) {
		return false
	}
	if !(t.MsgID == 0) {
		return false
	}
	if !(t.ScheduleDate == 0) {
		return false
	}
	if !(t.RejectComment == "") {
		return false
	}

	return true
}

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

// FillFrom fills MessagesToggleSuggestedPostApprovalRequest from given interface.
func (t *MessagesToggleSuggestedPostApprovalRequest) FillFrom(from interface {
	GetReject() (value bool)
	GetPeer() (value InputPeerClass)
	GetMsgID() (value int)
	GetScheduleDate() (value int, ok bool)
	GetRejectComment() (value string, ok bool)
}) {
	t.Reject = from.GetReject()
	t.Peer = from.GetPeer()
	t.MsgID = from.GetMsgID()
	if val, ok := from.GetScheduleDate(); ok {
		t.ScheduleDate = val
	}

	if val, ok := from.GetRejectComment(); ok {
		t.RejectComment = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*MessagesToggleSuggestedPostApprovalRequest) TypeName() string {
	return "messages.toggleSuggestedPostApproval"
}

// TypeInfo returns info about TL type.
func (t *MessagesToggleSuggestedPostApprovalRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.toggleSuggestedPostApproval",
		ID:   MessagesToggleSuggestedPostApprovalRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Reject",
			SchemaName: "reject",
			Null:       !t.Flags.Has(1),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
		},
		{
			Name:       "ScheduleDate",
			SchemaName: "schedule_date",
			Null:       !t.Flags.Has(0),
		},
		{
			Name:       "RejectComment",
			SchemaName: "reject_comment",
			Null:       !t.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (t *MessagesToggleSuggestedPostApprovalRequest) SetFlags() {
	if !(t.Reject == false) {
		t.Flags.Set(1)
	}
	if !(t.ScheduleDate == 0) {
		t.Flags.Set(0)
	}
	if !(t.RejectComment == "") {
		t.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (t *MessagesToggleSuggestedPostApprovalRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode messages.toggleSuggestedPostApproval#8107455c as nil")
	}
	b.PutID(MessagesToggleSuggestedPostApprovalRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *MessagesToggleSuggestedPostApprovalRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode messages.toggleSuggestedPostApproval#8107455c as nil")
	}
	t.SetFlags()
	if err := t.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.toggleSuggestedPostApproval#8107455c: field flags: %w", err)
	}
	if t.Peer == nil {
		return fmt.Errorf("unable to encode messages.toggleSuggestedPostApproval#8107455c: field peer is nil")
	}
	if err := t.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.toggleSuggestedPostApproval#8107455c: field peer: %w", err)
	}
	b.PutInt(t.MsgID)
	if t.Flags.Has(0) {
		b.PutInt(t.ScheduleDate)
	}
	if t.Flags.Has(2) {
		b.PutString(t.RejectComment)
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *MessagesToggleSuggestedPostApprovalRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode messages.toggleSuggestedPostApproval#8107455c to nil")
	}
	if err := b.ConsumeID(MessagesToggleSuggestedPostApprovalRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.toggleSuggestedPostApproval#8107455c: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *MessagesToggleSuggestedPostApprovalRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode messages.toggleSuggestedPostApproval#8107455c to nil")
	}
	{
		if err := t.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.toggleSuggestedPostApproval#8107455c: field flags: %w", err)
		}
	}
	t.Reject = t.Flags.Has(1)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.toggleSuggestedPostApproval#8107455c: field peer: %w", err)
		}
		t.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.toggleSuggestedPostApproval#8107455c: field msg_id: %w", err)
		}
		t.MsgID = value
	}
	if t.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.toggleSuggestedPostApproval#8107455c: field schedule_date: %w", err)
		}
		t.ScheduleDate = value
	}
	if t.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.toggleSuggestedPostApproval#8107455c: field reject_comment: %w", err)
		}
		t.RejectComment = value
	}
	return nil
}

// SetReject sets value of Reject conditional field.
func (t *MessagesToggleSuggestedPostApprovalRequest) SetReject(value bool) {
	if value {
		t.Flags.Set(1)
		t.Reject = true
	} else {
		t.Flags.Unset(1)
		t.Reject = false
	}
}

// GetReject returns value of Reject conditional field.
func (t *MessagesToggleSuggestedPostApprovalRequest) GetReject() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(1)
}

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

// GetMsgID returns value of MsgID field.
func (t *MessagesToggleSuggestedPostApprovalRequest) GetMsgID() (value int) {
	if t == nil {
		return
	}
	return t.MsgID
}

// SetScheduleDate sets value of ScheduleDate conditional field.
func (t *MessagesToggleSuggestedPostApprovalRequest) SetScheduleDate(value int) {
	t.Flags.Set(0)
	t.ScheduleDate = value
}

// GetScheduleDate returns value of ScheduleDate conditional field and
// boolean which is true if field was set.
func (t *MessagesToggleSuggestedPostApprovalRequest) GetScheduleDate() (value int, ok bool) {
	if t == nil {
		return
	}
	if !t.Flags.Has(0) {
		return value, false
	}
	return t.ScheduleDate, true
}

// SetRejectComment sets value of RejectComment conditional field.
func (t *MessagesToggleSuggestedPostApprovalRequest) SetRejectComment(value string) {
	t.Flags.Set(2)
	t.RejectComment = value
}

// GetRejectComment returns value of RejectComment conditional field and
// boolean which is true if field was set.
func (t *MessagesToggleSuggestedPostApprovalRequest) GetRejectComment() (value string, ok bool) {
	if t == nil {
		return
	}
	if !t.Flags.Has(2) {
		return value, false
	}
	return t.RejectComment, true
}

// MessagesToggleSuggestedPostApproval invokes method messages.toggleSuggestedPostApproval#8107455c returning error if any.
// Approve or reject a suggested post »¹.
//
// Links:
//  1. https://core.telegram.org/api/suggested-posts
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.toggleSuggestedPostApproval for reference.
func (c *Client) MessagesToggleSuggestedPostApproval(ctx context.Context, request *MessagesToggleSuggestedPostApprovalRequest) (UpdatesClass, error) {
	var result UpdatesBox

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