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

// SMSJobsFinishJobRequest represents TL type `smsjobs.finishJob#4f1ebf24`.
// Finish an SMS job (official clients only).
//
// See https://core.telegram.org/method/smsjobs.finishJob for reference.
type SMSJobsFinishJobRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Job ID.
	JobID string
	// If failed, the error.
	//
	// Use SetError and GetError helpers.
	Error string
}

// SMSJobsFinishJobRequestTypeID is TL type id of SMSJobsFinishJobRequest.
const SMSJobsFinishJobRequestTypeID = 0x4f1ebf24

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

func (f *SMSJobsFinishJobRequest) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.JobID == "") {
		return false
	}
	if !(f.Error == "") {
		return false
	}

	return true
}

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

// FillFrom fills SMSJobsFinishJobRequest from given interface.
func (f *SMSJobsFinishJobRequest) FillFrom(from interface {
	GetJobID() (value string)
	GetError() (value string, ok bool)
}) {
	f.JobID = from.GetJobID()
	if val, ok := from.GetError(); ok {
		f.Error = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*SMSJobsFinishJobRequest) TypeName() string {
	return "smsjobs.finishJob"
}

// TypeInfo returns info about TL type.
func (f *SMSJobsFinishJobRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "smsjobs.finishJob",
		ID:   SMSJobsFinishJobRequestTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "JobID",
			SchemaName: "job_id",
		},
		{
			Name:       "Error",
			SchemaName: "error",
			Null:       !f.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (f *SMSJobsFinishJobRequest) SetFlags() {
	if !(f.Error == "") {
		f.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (f *SMSJobsFinishJobRequest) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode smsjobs.finishJob#4f1ebf24 as nil")
	}
	b.PutID(SMSJobsFinishJobRequestTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *SMSJobsFinishJobRequest) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode smsjobs.finishJob#4f1ebf24 as nil")
	}
	f.SetFlags()
	if err := f.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode smsjobs.finishJob#4f1ebf24: field flags: %w", err)
	}
	b.PutString(f.JobID)
	if f.Flags.Has(0) {
		b.PutString(f.Error)
	}
	return nil
}

// Decode implements bin.Decoder.
func (f *SMSJobsFinishJobRequest) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode smsjobs.finishJob#4f1ebf24 to nil")
	}
	if err := b.ConsumeID(SMSJobsFinishJobRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode smsjobs.finishJob#4f1ebf24: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *SMSJobsFinishJobRequest) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode smsjobs.finishJob#4f1ebf24 to nil")
	}
	{
		if err := f.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode smsjobs.finishJob#4f1ebf24: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode smsjobs.finishJob#4f1ebf24: field job_id: %w", err)
		}
		f.JobID = value
	}
	if f.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode smsjobs.finishJob#4f1ebf24: field error: %w", err)
		}
		f.Error = value
	}
	return nil
}

// GetJobID returns value of JobID field.
func (f *SMSJobsFinishJobRequest) GetJobID() (value string) {
	if f == nil {
		return
	}
	return f.JobID
}

// SetError sets value of Error conditional field.
func (f *SMSJobsFinishJobRequest) SetError(value string) {
	f.Flags.Set(0)
	f.Error = value
}

// GetError returns value of Error conditional field and
// boolean which is true if field was set.
func (f *SMSJobsFinishJobRequest) GetError() (value string, ok bool) {
	if f == nil {
		return
	}
	if !f.Flags.Has(0) {
		return value, false
	}
	return f.Error, true
}

// SMSJobsFinishJob invokes method smsjobs.finishJob#4f1ebf24 returning error if any.
// Finish an SMS job (official clients only).
//
// Possible errors:
//
//	400 SMSJOB_ID_INVALID: The specified job ID is invalid.
//
// See https://core.telegram.org/method/smsjobs.finishJob for reference.
func (c *Client) SMSJobsFinishJob(ctx context.Context, request *SMSJobsFinishJobRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
