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

// StatsGetPollStatsRequest represents TL type `stats.getPollStats#c27dfa68`.
//
// See https://core.telegram.org/method/stats.getPollStats for reference.
type StatsGetPollStatsRequest struct {
	// Flags field of StatsGetPollStatsRequest.
	Flags bin.Fields
	// Dark field of StatsGetPollStatsRequest.
	Dark bool
	// Peer field of StatsGetPollStatsRequest.
	Peer InputPeerClass
	// MsgID field of StatsGetPollStatsRequest.
	MsgID int
}

// StatsGetPollStatsRequestTypeID is TL type id of StatsGetPollStatsRequest.
const StatsGetPollStatsRequestTypeID = 0xc27dfa68

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

func (g *StatsGetPollStatsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Dark == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.MsgID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StatsGetPollStatsRequest from given interface.
func (g *StatsGetPollStatsRequest) FillFrom(from interface {
	GetDark() (value bool)
	GetPeer() (value InputPeerClass)
	GetMsgID() (value int)
}) {
	g.Dark = from.GetDark()
	g.Peer = from.GetPeer()
	g.MsgID = from.GetMsgID()
}

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

// TypeName returns name of type in TL schema.
func (*StatsGetPollStatsRequest) TypeName() string {
	return "stats.getPollStats"
}

// TypeInfo returns info about TL type.
func (g *StatsGetPollStatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stats.getPollStats",
		ID:   StatsGetPollStatsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Dark",
			SchemaName: "dark",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *StatsGetPollStatsRequest) SetFlags() {
	if !(g.Dark == false) {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *StatsGetPollStatsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stats.getPollStats#c27dfa68 as nil")
	}
	b.PutID(StatsGetPollStatsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StatsGetPollStatsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stats.getPollStats#c27dfa68 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stats.getPollStats#c27dfa68: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode stats.getPollStats#c27dfa68: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stats.getPollStats#c27dfa68: field peer: %w", err)
	}
	b.PutInt(g.MsgID)
	return nil
}

// Decode implements bin.Decoder.
func (g *StatsGetPollStatsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stats.getPollStats#c27dfa68 to nil")
	}
	if err := b.ConsumeID(StatsGetPollStatsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stats.getPollStats#c27dfa68: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *StatsGetPollStatsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stats.getPollStats#c27dfa68 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stats.getPollStats#c27dfa68: field flags: %w", err)
		}
	}
	g.Dark = g.Flags.Has(0)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stats.getPollStats#c27dfa68: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stats.getPollStats#c27dfa68: field msg_id: %w", err)
		}
		g.MsgID = value
	}
	return nil
}

// SetDark sets value of Dark conditional field.
func (g *StatsGetPollStatsRequest) SetDark(value bool) {
	if value {
		g.Flags.Set(0)
		g.Dark = true
	} else {
		g.Flags.Unset(0)
		g.Dark = false
	}
}

// GetDark returns value of Dark conditional field.
func (g *StatsGetPollStatsRequest) GetDark() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

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

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

// StatsGetPollStats invokes method stats.getPollStats#c27dfa68 returning error if any.
//
// See https://core.telegram.org/method/stats.getPollStats for reference.
func (c *Client) StatsGetPollStats(ctx context.Context, request *StatsGetPollStatsRequest) (*StatsPollStats, error) {
	var result StatsPollStats

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