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

// SetChatAffiliateProgramRequest represents TL type `setChatAffiliateProgram#e00237`.
type SetChatAffiliateProgramRequest struct {
	// Identifier of the chat with an owned bot for which affiliate program is changed
	ChatID int64
	// Parameters of the affiliate program; pass null to close the currently active program.
	// If there is an active program, then commission and program duration can only be
	// increased.
	Parameters AffiliateProgramParameters
}

// SetChatAffiliateProgramRequestTypeID is TL type id of SetChatAffiliateProgramRequest.
const SetChatAffiliateProgramRequestTypeID = 0xe00237

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

func (s *SetChatAffiliateProgramRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.Parameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetChatAffiliateProgramRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setChatAffiliateProgram",
		ID:   SetChatAffiliateProgramRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetChatAffiliateProgramRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatAffiliateProgram#e00237 as nil")
	}
	b.PutID(SetChatAffiliateProgramRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetChatAffiliateProgramRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatAffiliateProgram#e00237 as nil")
	}
	b.PutInt53(s.ChatID)
	if err := s.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setChatAffiliateProgram#e00237: field parameters: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetChatAffiliateProgramRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatAffiliateProgram#e00237 to nil")
	}
	if err := b.ConsumeID(SetChatAffiliateProgramRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setChatAffiliateProgram#e00237: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetChatAffiliateProgramRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatAffiliateProgram#e00237 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setChatAffiliateProgram#e00237: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		if err := s.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setChatAffiliateProgram#e00237: field parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetChatAffiliateProgramRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatAffiliateProgram#e00237 as nil")
	}
	b.ObjStart()
	b.PutID("setChatAffiliateProgram")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("parameters")
	if err := s.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setChatAffiliateProgram#e00237: field parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetChatAffiliateProgramRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatAffiliateProgram#e00237 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setChatAffiliateProgram"); err != nil {
				return fmt.Errorf("unable to decode setChatAffiliateProgram#e00237: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setChatAffiliateProgram#e00237: field chat_id: %w", err)
			}
			s.ChatID = value
		case "parameters":
			if err := s.Parameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setChatAffiliateProgram#e00237: field parameters: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (s *SetChatAffiliateProgramRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetParameters returns value of Parameters field.
func (s *SetChatAffiliateProgramRequest) GetParameters() (value AffiliateProgramParameters) {
	if s == nil {
		return
	}
	return s.Parameters
}

// SetChatAffiliateProgram invokes method setChatAffiliateProgram#e00237 returning error if any.
func (c *Client) SetChatAffiliateProgram(ctx context.Context, request *SetChatAffiliateProgramRequest) error {
	var ok Ok

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