// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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#3b6ddad2`.
// 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
	Voters int
}

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

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

	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)
}) {
	p.Chosen = from.GetChosen()
	p.Correct = from.GetCorrect()
	p.Option = from.GetOption()
	p.Voters = from.GetVoters()
}

// 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",
		},
	}
	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)
	}
}

// Encode implements bin.Encoder.
func (p *PollAnswerVoters) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pollAnswerVoters#3b6ddad2 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#3b6ddad2 as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode pollAnswerVoters#3b6ddad2: field flags: %w", err)
	}
	b.PutBytes(p.Option)
	b.PutInt(p.Voters)
	return nil
}

// Decode implements bin.Decoder.
func (p *PollAnswerVoters) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pollAnswerVoters#3b6ddad2 to nil")
	}
	if err := b.ConsumeID(PollAnswerVotersTypeID); err != nil {
		return fmt.Errorf("unable to decode pollAnswerVoters#3b6ddad2: %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#3b6ddad2 to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode pollAnswerVoters#3b6ddad2: 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#3b6ddad2: field option: %w", err)
		}
		p.Option = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode pollAnswerVoters#3b6ddad2: field voters: %w", err)
		}
		p.Voters = 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
}

// GetVoters returns value of Voters field.
func (p *PollAnswerVoters) GetVoters() (value int) {
	if p == nil {
		return
	}
	return p.Voters
}
