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

// KeyboardButtonStyle represents TL type `keyboardButtonStyle#4fdd3430`.
//
// See https://core.telegram.org/constructor/keyboardButtonStyle for reference.
type KeyboardButtonStyle struct {
	// Flags field of KeyboardButtonStyle.
	Flags bin.Fields
	// BgPrimary field of KeyboardButtonStyle.
	BgPrimary bool
	// BgDanger field of KeyboardButtonStyle.
	BgDanger bool
	// BgSuccess field of KeyboardButtonStyle.
	BgSuccess bool
	// Icon field of KeyboardButtonStyle.
	//
	// Use SetIcon and GetIcon helpers.
	Icon int64
}

// KeyboardButtonStyleTypeID is TL type id of KeyboardButtonStyle.
const KeyboardButtonStyleTypeID = 0x4fdd3430

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

func (k *KeyboardButtonStyle) Zero() bool {
	if k == nil {
		return true
	}
	if !(k.Flags.Zero()) {
		return false
	}
	if !(k.BgPrimary == false) {
		return false
	}
	if !(k.BgDanger == false) {
		return false
	}
	if !(k.BgSuccess == false) {
		return false
	}
	if !(k.Icon == 0) {
		return false
	}

	return true
}

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

// FillFrom fills KeyboardButtonStyle from given interface.
func (k *KeyboardButtonStyle) FillFrom(from interface {
	GetBgPrimary() (value bool)
	GetBgDanger() (value bool)
	GetBgSuccess() (value bool)
	GetIcon() (value int64, ok bool)
}) {
	k.BgPrimary = from.GetBgPrimary()
	k.BgDanger = from.GetBgDanger()
	k.BgSuccess = from.GetBgSuccess()
	if val, ok := from.GetIcon(); ok {
		k.Icon = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (k *KeyboardButtonStyle) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "keyboardButtonStyle",
		ID:   KeyboardButtonStyleTypeID,
	}
	if k == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BgPrimary",
			SchemaName: "bg_primary",
			Null:       !k.Flags.Has(0),
		},
		{
			Name:       "BgDanger",
			SchemaName: "bg_danger",
			Null:       !k.Flags.Has(1),
		},
		{
			Name:       "BgSuccess",
			SchemaName: "bg_success",
			Null:       !k.Flags.Has(2),
		},
		{
			Name:       "Icon",
			SchemaName: "icon",
			Null:       !k.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (k *KeyboardButtonStyle) SetFlags() {
	if !(k.BgPrimary == false) {
		k.Flags.Set(0)
	}
	if !(k.BgDanger == false) {
		k.Flags.Set(1)
	}
	if !(k.BgSuccess == false) {
		k.Flags.Set(2)
	}
	if !(k.Icon == 0) {
		k.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (k *KeyboardButtonStyle) Encode(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButtonStyle#4fdd3430 as nil")
	}
	b.PutID(KeyboardButtonStyleTypeID)
	return k.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (k *KeyboardButtonStyle) EncodeBare(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButtonStyle#4fdd3430 as nil")
	}
	k.SetFlags()
	if err := k.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode keyboardButtonStyle#4fdd3430: field flags: %w", err)
	}
	if k.Flags.Has(3) {
		b.PutLong(k.Icon)
	}
	return nil
}

// Decode implements bin.Decoder.
func (k *KeyboardButtonStyle) Decode(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButtonStyle#4fdd3430 to nil")
	}
	if err := b.ConsumeID(KeyboardButtonStyleTypeID); err != nil {
		return fmt.Errorf("unable to decode keyboardButtonStyle#4fdd3430: %w", err)
	}
	return k.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (k *KeyboardButtonStyle) DecodeBare(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButtonStyle#4fdd3430 to nil")
	}
	{
		if err := k.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode keyboardButtonStyle#4fdd3430: field flags: %w", err)
		}
	}
	k.BgPrimary = k.Flags.Has(0)
	k.BgDanger = k.Flags.Has(1)
	k.BgSuccess = k.Flags.Has(2)
	if k.Flags.Has(3) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode keyboardButtonStyle#4fdd3430: field icon: %w", err)
		}
		k.Icon = value
	}
	return nil
}

// SetBgPrimary sets value of BgPrimary conditional field.
func (k *KeyboardButtonStyle) SetBgPrimary(value bool) {
	if value {
		k.Flags.Set(0)
		k.BgPrimary = true
	} else {
		k.Flags.Unset(0)
		k.BgPrimary = false
	}
}

// GetBgPrimary returns value of BgPrimary conditional field.
func (k *KeyboardButtonStyle) GetBgPrimary() (value bool) {
	if k == nil {
		return
	}
	return k.Flags.Has(0)
}

// SetBgDanger sets value of BgDanger conditional field.
func (k *KeyboardButtonStyle) SetBgDanger(value bool) {
	if value {
		k.Flags.Set(1)
		k.BgDanger = true
	} else {
		k.Flags.Unset(1)
		k.BgDanger = false
	}
}

// GetBgDanger returns value of BgDanger conditional field.
func (k *KeyboardButtonStyle) GetBgDanger() (value bool) {
	if k == nil {
		return
	}
	return k.Flags.Has(1)
}

// SetBgSuccess sets value of BgSuccess conditional field.
func (k *KeyboardButtonStyle) SetBgSuccess(value bool) {
	if value {
		k.Flags.Set(2)
		k.BgSuccess = true
	} else {
		k.Flags.Unset(2)
		k.BgSuccess = false
	}
}

// GetBgSuccess returns value of BgSuccess conditional field.
func (k *KeyboardButtonStyle) GetBgSuccess() (value bool) {
	if k == nil {
		return
	}
	return k.Flags.Has(2)
}

// SetIcon sets value of Icon conditional field.
func (k *KeyboardButtonStyle) SetIcon(value int64) {
	k.Flags.Set(3)
	k.Icon = value
}

// GetIcon returns value of Icon conditional field and
// boolean which is true if field was set.
func (k *KeyboardButtonStyle) GetIcon() (value int64, ok bool) {
	if k == nil {
		return
	}
	if !k.Flags.Has(3) {
		return value, false
	}
	return k.Icon, true
}
