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

// MessagesMarkDialogUnreadRequest represents TL type `messages.markDialogUnread#8c5006f8`.
// Manually mark dialog as unread
//
// See https://core.telegram.org/method/messages.markDialogUnread for reference.
type MessagesMarkDialogUnreadRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Mark as unread/read
	Unread bool
	// If set, must be equal to the ID of a monoforum¹, and will affect the monoforum topic
	// passed in peer.
	//
	// Links:
	//  1) https://core.telegram.org/api/monoforum
	//
	// Use SetParentPeer and GetParentPeer helpers.
	ParentPeer InputPeerClass
	// Dialog
	Peer InputDialogPeerClass
}

// MessagesMarkDialogUnreadRequestTypeID is TL type id of MessagesMarkDialogUnreadRequest.
const MessagesMarkDialogUnreadRequestTypeID = 0x8c5006f8

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

func (m *MessagesMarkDialogUnreadRequest) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.Flags.Zero()) {
		return false
	}
	if !(m.Unread == false) {
		return false
	}
	if !(m.ParentPeer == nil) {
		return false
	}
	if !(m.Peer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesMarkDialogUnreadRequest from given interface.
func (m *MessagesMarkDialogUnreadRequest) FillFrom(from interface {
	GetUnread() (value bool)
	GetParentPeer() (value InputPeerClass, ok bool)
	GetPeer() (value InputDialogPeerClass)
}) {
	m.Unread = from.GetUnread()
	if val, ok := from.GetParentPeer(); ok {
		m.ParentPeer = val
	}

	m.Peer = from.GetPeer()
}

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

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

// TypeInfo returns info about TL type.
func (m *MessagesMarkDialogUnreadRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.markDialogUnread",
		ID:   MessagesMarkDialogUnreadRequestTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Unread",
			SchemaName: "unread",
			Null:       !m.Flags.Has(0),
		},
		{
			Name:       "ParentPeer",
			SchemaName: "parent_peer",
			Null:       !m.Flags.Has(1),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (m *MessagesMarkDialogUnreadRequest) SetFlags() {
	if !(m.Unread == false) {
		m.Flags.Set(0)
	}
	if !(m.ParentPeer == nil) {
		m.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (m *MessagesMarkDialogUnreadRequest) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messages.markDialogUnread#8c5006f8 as nil")
	}
	b.PutID(MessagesMarkDialogUnreadRequestTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessagesMarkDialogUnreadRequest) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messages.markDialogUnread#8c5006f8 as nil")
	}
	m.SetFlags()
	if err := m.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.markDialogUnread#8c5006f8: field flags: %w", err)
	}
	if m.Flags.Has(1) {
		if m.ParentPeer == nil {
			return fmt.Errorf("unable to encode messages.markDialogUnread#8c5006f8: field parent_peer is nil")
		}
		if err := m.ParentPeer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.markDialogUnread#8c5006f8: field parent_peer: %w", err)
		}
	}
	if m.Peer == nil {
		return fmt.Errorf("unable to encode messages.markDialogUnread#8c5006f8: field peer is nil")
	}
	if err := m.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.markDialogUnread#8c5006f8: field peer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MessagesMarkDialogUnreadRequest) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messages.markDialogUnread#8c5006f8 to nil")
	}
	if err := b.ConsumeID(MessagesMarkDialogUnreadRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.markDialogUnread#8c5006f8: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessagesMarkDialogUnreadRequest) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messages.markDialogUnread#8c5006f8 to nil")
	}
	{
		if err := m.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.markDialogUnread#8c5006f8: field flags: %w", err)
		}
	}
	m.Unread = m.Flags.Has(0)
	if m.Flags.Has(1) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.markDialogUnread#8c5006f8: field parent_peer: %w", err)
		}
		m.ParentPeer = value
	}
	{
		value, err := DecodeInputDialogPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.markDialogUnread#8c5006f8: field peer: %w", err)
		}
		m.Peer = value
	}
	return nil
}

// SetUnread sets value of Unread conditional field.
func (m *MessagesMarkDialogUnreadRequest) SetUnread(value bool) {
	if value {
		m.Flags.Set(0)
		m.Unread = true
	} else {
		m.Flags.Unset(0)
		m.Unread = false
	}
}

// GetUnread returns value of Unread conditional field.
func (m *MessagesMarkDialogUnreadRequest) GetUnread() (value bool) {
	if m == nil {
		return
	}
	return m.Flags.Has(0)
}

// SetParentPeer sets value of ParentPeer conditional field.
func (m *MessagesMarkDialogUnreadRequest) SetParentPeer(value InputPeerClass) {
	m.Flags.Set(1)
	m.ParentPeer = value
}

// GetParentPeer returns value of ParentPeer conditional field and
// boolean which is true if field was set.
func (m *MessagesMarkDialogUnreadRequest) GetParentPeer() (value InputPeerClass, ok bool) {
	if m == nil {
		return
	}
	if !m.Flags.Has(1) {
		return value, false
	}
	return m.ParentPeer, true
}

// GetPeer returns value of Peer field.
func (m *MessagesMarkDialogUnreadRequest) GetPeer() (value InputDialogPeerClass) {
	if m == nil {
		return
	}
	return m.Peer
}

// MessagesMarkDialogUnread invokes method messages.markDialogUnread#8c5006f8 returning error if any.
// Manually mark dialog as unread
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.markDialogUnread for reference.
func (c *Client) MessagesMarkDialogUnread(ctx context.Context, request *MessagesMarkDialogUnreadRequest) (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
}
