// NOTE this is a generated file! do not edit!

package clock

import (
	"context"

	"github.com/golang/protobuf/proto"

	eventbus "code.justin.tv/eventbus/client"

	"code.justin.tv/eventbus/schema/pkg/eventbus/authorization"
	"code.justin.tv/eventbus/client/encryption"
)

const (
	UpdateEventType = "ClockUpdate"
)

type Update = ClockUpdate

type ClockUpdateHandler func(context.Context, *eventbus.Header, *ClockUpdate) error

func (h ClockUpdateHandler) Handler() eventbus.Handler {
	return func(ctx context.Context, message eventbus.RawMessage) error {
		dst := &ClockUpdate{}
		err := proto.Unmarshal(message.Payload, dst)
		if err != nil {
			return err
		}
		return h(ctx, message.Header, dst)
	}
}

func RegisterClockUpdateHandler(mux *eventbus.Mux, f ClockUpdateHandler) {
	mux.RegisterHandler(UpdateEventType, f.Handler())
}

func RegisterUpdateHandler(mux *eventbus.Mux, f ClockUpdateHandler) {
	RegisterClockUpdateHandler(mux, f)
}

func (*ClockUpdate) EventBusName() string {
	return UpdateEventType
}
func getClockUpdateClockUpdateSecretTimeAuthContext(environment string) authorization.Context {
	return authorization.Context{
		authorization.EventType:   "ClockUpdate",
		authorization.Environment: environment,
		authorization.MessageName: "ClockUpdate",
		authorization.FieldName:   "SecretTime",
	}
}

func (m *ClockUpdate) SetEncryptedSecretTime(p encryption.Provider, plaintext string) error {
	if m == nil {
		return authorization.ErrEncryptionNilReceiver
	}

	encCtx := getClockUpdateClockUpdateSecretTimeAuthContext(p.Environment())
	err := authorization.ValidateContext(encCtx)
	if err != nil {
		return err
	}

	enc := p.Encrypter()
	b, err := enc.EncryptString(encCtx, plaintext)
	if err != nil {
		return err
	}
	m.SecretTime = &authorization.String{
		OlePayload: b,
	}
	return err
}

func (m *ClockUpdate) GetDecryptedSecretTime(dec encryption.Decrypter) (string, error) {
	if m == nil {
		return "", authorization.ErrDecryptionNilReceiver
	}
	if m.GetSecretTime() == nil {
		return "", nil
	}
	return dec.DecryptString(m.GetSecretTime().GetOlePayload())
}
