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

// PollAnswerVoters represents TL type `pollAnswerVoters#3645230a`.
// A poll answer, and how users voted on it
//
// See https://core.telegram.org/constructor/pollAnswerVoters for reference.
type PollAnswerVoters struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether we have chosen this answer
	Chosen bool
	// For quizzes, whether the option we have chosen is correct
	Correct bool
	// The param that has to be passed to messages.sendVote¹.
	//
	// Links:
	//  1) https://core.telegram.org/method/messages.sendVote
	Option []byte
	// How many users voted for this option
	//
	// Use SetVoters and GetVoters helpers.
	Voters int
	// RecentVoters field of PollAnswerVoters.
	//
	// Use SetRecentVoters and GetRecentVoters helpers.
	RecentVoters []PeerClass
}

// PollAnswerVotersTypeID is TL type id of PollAnswerVoters.
const PollAnswerVotersTypeID = 0x3645230a

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

func (p *PollAnswerVoters) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Flags.Zero()) {
		return false
	}
	if !(p.Chosen == false) {
		return false
	}
	if !(p.Correct == false) {
		return false
	}
	if !(p.Option == nil) {
		return false
	}
	if !(p.Voters == 0) {
		return false
	}
	if !(p.RecentVoters == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PollAnswerVoters from given interface.
func (p *PollAnswerVoters) FillFrom(from interface {
	GetChosen() (value bool)
	GetCorrect() (value bool)
	GetOption() (value []byte)
	GetVoters() (value int, ok bool)
	GetRecentVoters() (value []PeerClass, ok bool)
}) {
	p.Chosen = from.GetChosen()
	p.Correct = from.GetCorrect()
	p.Option = from.GetOption()
	if val, ok := from.GetVoters(); ok {
		p.Voters = val
	}

	if val, ok := from.GetRecentVoters(); ok {
		p.RecentVoters = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*PollAnswerVoters) TypeName() string {
	return "pollAnswerVoters"
}

// TypeInfo returns info about TL type.
func (p *PollAnswerVoters) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "pollAnswerVoters",
		ID:   PollAnswerVotersTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Chosen",
			SchemaName: "chosen",
			Null:       !p.Flags.Has(0),
		},
		{
			Name:       "Correct",
			SchemaName: "correct",
			Null:       !p.Flags.Has(1),
		},
		{
			Name:       "Option",
			SchemaName: "option",
		},
		{
			Name:       "Voters",
			SchemaName: "voters",
			Null:       !p.Flags.Has(2),
		},
		{
			Name:       "RecentVoters",
			SchemaName: "recent_voters",
			Null:       !p.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (p *PollAnswerVoters) SetFlags() {
	if !(p.Chosen == false) {
		p.Flags.Set(0)
	}
	if !(p.Correct == false) {
		p.Flags.Set(1)
	}
	if !(p.Voters == 0) {
		p.Flags.Set(2)
	}
	if !(p.RecentVoters == nil) {
		p.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (p *PollAnswerVoters) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pollAnswerVoters#3645230a as nil")
	}
	b.PutID(PollAnswerVotersTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PollAnswerVoters) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pollAnswerVoters#3645230a as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode pollAnswerVoters#3645230a: field flags: %w", err)
	}
	b.PutBytes(p.Option)
	if p.Flags.Has(2) {
		b.PutInt(p.Voters)
	}
	if p.Flags.Has(2) {
		b.PutVectorHeader(len(p.RecentVoters))
		for idx, v := range p.RecentVoters {
			if v == nil {
				return fmt.Errorf("unable to encode pollAnswerVoters#3645230a: field recent_voters element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode pollAnswerVoters#3645230a: field recent_voters element with index %d: %w", idx, err)
			}
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PollAnswerVoters) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pollAnswerVoters#3645230a to nil")
	}
	if err := b.ConsumeID(PollAnswerVotersTypeID); err != nil {
		return fmt.Errorf("unable to decode pollAnswerVoters#3645230a: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PollAnswerVoters) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pollAnswerVoters#3645230a to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode pollAnswerVoters#3645230a: field flags: %w", err)
		}
	}
	p.Chosen = p.Flags.Has(0)
	p.Correct = p.Flags.Has(1)
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode pollAnswerVoters#3645230a: field option: %w", err)
		}
		p.Option = value
	}
	if p.Flags.Has(2) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode pollAnswerVoters#3645230a: field voters: %w", err)
		}
		p.Voters = value
	}
	if p.Flags.Has(2) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode pollAnswerVoters#3645230a: field recent_voters: %w", err)
		}

		if headerLen > 0 {
			p.RecentVoters = make([]PeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodePeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode pollAnswerVoters#3645230a: field recent_voters: %w", err)
			}
			p.RecentVoters = append(p.RecentVoters, value)
		}
	}
	return nil
}

// SetChosen sets value of Chosen conditional field.
func (p *PollAnswerVoters) SetChosen(value bool) {
	if value {
		p.Flags.Set(0)
		p.Chosen = true
	} else {
		p.Flags.Unset(0)
		p.Chosen = false
	}
}

// GetChosen returns value of Chosen conditional field.
func (p *PollAnswerVoters) GetChosen() (value bool) {
	if p == nil {
		return
	}
	return p.Flags.Has(0)
}

// SetCorrect sets value of Correct conditional field.
func (p *PollAnswerVoters) SetCorrect(value bool) {
	if value {
		p.Flags.Set(1)
		p.Correct = true
	} else {
		p.Flags.Unset(1)
		p.Correct = false
	}
}

// GetCorrect returns value of Correct conditional field.
func (p *PollAnswerVoters) GetCorrect() (value bool) {
	if p == nil {
		return
	}
	return p.Flags.Has(1)
}

// GetOption returns value of Option field.
func (p *PollAnswerVoters) GetOption() (value []byte) {
	if p == nil {
		return
	}
	return p.Option
}

// SetVoters sets value of Voters conditional field.
func (p *PollAnswerVoters) SetVoters(value int) {
	p.Flags.Set(2)
	p.Voters = value
}

// GetVoters returns value of Voters conditional field and
// boolean which is true if field was set.
func (p *PollAnswerVoters) GetVoters() (value int, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(2) {
		return value, false
	}
	return p.Voters, true
}

// SetRecentVoters sets value of RecentVoters conditional field.
func (p *PollAnswerVoters) SetRecentVoters(value []PeerClass) {
	p.Flags.Set(2)
	p.RecentVoters = value
}

// GetRecentVoters returns value of RecentVoters conditional field and
// boolean which is true if field was set.
func (p *PollAnswerVoters) GetRecentVoters() (value []PeerClass, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(2) {
		return value, false
	}
	return p.RecentVoters, true
}

// MapRecentVoters returns field RecentVoters wrapped in PeerClassArray helper.
func (p *PollAnswerVoters) MapRecentVoters() (value PeerClassArray, ok bool) {
	if !p.Flags.Has(2) {
		return value, false
	}
	return PeerClassArray(p.RecentVoters), true
}
