// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// PollOption represents TL type `pollOption#63e97090`.
type PollOption struct {
	// Option text; 1-100 characters. Only custom emoji entities are allowed
	Text FormattedText
	// Number of voters for this option, available only for closed or voted polls
	VoterCount int32
	// The percentage of votes for this option; 0-100
	VotePercentage int32
	// True, if the option was chosen by the user
	IsChosen bool
	// True, if the option is being chosen by a pending setPollAnswer request
	IsBeingChosen bool
}

// PollOptionTypeID is TL type id of PollOption.
const PollOptionTypeID = 0x63e97090

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

func (p *PollOption) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Text.Zero()) {
		return false
	}
	if !(p.VoterCount == 0) {
		return false
	}
	if !(p.VotePercentage == 0) {
		return false
	}
	if !(p.IsChosen == false) {
		return false
	}
	if !(p.IsBeingChosen == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PollOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "pollOption",
		ID:   PollOptionTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "VoterCount",
			SchemaName: "voter_count",
		},
		{
			Name:       "VotePercentage",
			SchemaName: "vote_percentage",
		},
		{
			Name:       "IsChosen",
			SchemaName: "is_chosen",
		},
		{
			Name:       "IsBeingChosen",
			SchemaName: "is_being_chosen",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PollOption) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pollOption#63e97090 as nil")
	}
	b.PutID(PollOptionTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PollOption) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pollOption#63e97090 as nil")
	}
	if err := p.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode pollOption#63e97090: field text: %w", err)
	}
	b.PutInt32(p.VoterCount)
	b.PutInt32(p.VotePercentage)
	b.PutBool(p.IsChosen)
	b.PutBool(p.IsBeingChosen)
	return nil
}

// Decode implements bin.Decoder.
func (p *PollOption) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pollOption#63e97090 to nil")
	}
	if err := b.ConsumeID(PollOptionTypeID); err != nil {
		return fmt.Errorf("unable to decode pollOption#63e97090: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PollOption) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pollOption#63e97090 to nil")
	}
	{
		if err := p.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode pollOption#63e97090: field text: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode pollOption#63e97090: field voter_count: %w", err)
		}
		p.VoterCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode pollOption#63e97090: field vote_percentage: %w", err)
		}
		p.VotePercentage = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode pollOption#63e97090: field is_chosen: %w", err)
		}
		p.IsChosen = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode pollOption#63e97090: field is_being_chosen: %w", err)
		}
		p.IsBeingChosen = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PollOption) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode pollOption#63e97090 as nil")
	}
	b.ObjStart()
	b.PutID("pollOption")
	b.Comma()
	b.FieldStart("text")
	if err := p.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode pollOption#63e97090: field text: %w", err)
	}
	b.Comma()
	b.FieldStart("voter_count")
	b.PutInt32(p.VoterCount)
	b.Comma()
	b.FieldStart("vote_percentage")
	b.PutInt32(p.VotePercentage)
	b.Comma()
	b.FieldStart("is_chosen")
	b.PutBool(p.IsChosen)
	b.Comma()
	b.FieldStart("is_being_chosen")
	b.PutBool(p.IsBeingChosen)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PollOption) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode pollOption#63e97090 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("pollOption"); err != nil {
				return fmt.Errorf("unable to decode pollOption#63e97090: %w", err)
			}
		case "text":
			if err := p.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode pollOption#63e97090: field text: %w", err)
			}
		case "voter_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode pollOption#63e97090: field voter_count: %w", err)
			}
			p.VoterCount = value
		case "vote_percentage":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode pollOption#63e97090: field vote_percentage: %w", err)
			}
			p.VotePercentage = value
		case "is_chosen":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode pollOption#63e97090: field is_chosen: %w", err)
			}
			p.IsChosen = value
		case "is_being_chosen":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode pollOption#63e97090: field is_being_chosen: %w", err)
			}
			p.IsBeingChosen = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetText returns value of Text field.
func (p *PollOption) GetText() (value FormattedText) {
	if p == nil {
		return
	}
	return p.Text
}

// GetVoterCount returns value of VoterCount field.
func (p *PollOption) GetVoterCount() (value int32) {
	if p == nil {
		return
	}
	return p.VoterCount
}

// GetVotePercentage returns value of VotePercentage field.
func (p *PollOption) GetVotePercentage() (value int32) {
	if p == nil {
		return
	}
	return p.VotePercentage
}

// GetIsChosen returns value of IsChosen field.
func (p *PollOption) GetIsChosen() (value bool) {
	if p == nil {
		return
	}
	return p.IsChosen
}

// GetIsBeingChosen returns value of IsBeingChosen field.
func (p *PollOption) GetIsBeingChosen() (value bool) {
	if p == nil {
		return
	}
	return p.IsBeingChosen
}
