/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// Generated by tools/resourcegen
// Run "make generate" to update this file.

// nolint:whitespace
package v1alpha1

import (
	"encoding/json"

	"google.golang.org/protobuf/proto"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	k8sruntime "k8s.io/apimachinery/pkg/runtime"

	meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1"
	"github.com/apache/dubbo-admin/pkg/core/logger"
	coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
)

const ServiceProviderMetadataKind coremodel.ResourceKind = "ServiceProviderMetadata"

func init() {
	coremodel.RegisterResourceSchema(ServiceProviderMetadataKind, NewServiceProviderMetadataResource, NewServiceProviderMetadataResourceList)
}

type ServiceProviderMetadataResource struct {
	metav1.TypeMeta `json:",inline"`

	metav1.ObjectMeta `json:"metadata,omitempty"`

	// Mesh is the name of the dubbo mesh this resource belongs to.
	// It may be omitted for cluster-scoped resources.
	Mesh string `json:"mesh,omitempty"`

	// Spec is the specification of the Dubbo ServiceProviderMetadata resource.
	Spec *meshproto.ServiceProviderMetadata `json:"spec,omitempty"`

	// Status is the status of the Dubbo ServiceProviderMetadata resource.
	Status ServiceProviderMetadataResourceStatus `json:"status,omitempty"`
}

type ServiceProviderMetadataResourceStatus struct {
	// define resource-specific status here
}

func (r *ServiceProviderMetadataResource) ResourceKind() coremodel.ResourceKind {
	return ServiceProviderMetadataKind
}

func (r *ServiceProviderMetadataResource) ResourceMesh() string {
	return r.Mesh
}

func (r *ServiceProviderMetadataResource) ResourceKey() string {
	return coremodel.BuildResourceKey(r.Mesh, r.Name)
}

func (r *ServiceProviderMetadataResource) ResourceMeta() metav1.ObjectMeta {
	return r.ObjectMeta
}

func (r *ServiceProviderMetadataResource) ResourceSpec() coremodel.ResourceSpec {
	return r.Spec
}

func (r *ServiceProviderMetadataResource) DeepCopyObject() k8sruntime.Object {
	out := &ServiceProviderMetadataResource{
		TypeMeta: r.TypeMeta,
		Mesh:     r.Mesh,
		Status:   r.Status,
	}

	r.ObjectMeta.DeepCopyInto(&out.ObjectMeta)

	if r.Spec != nil {
		spec, ok := proto.Clone(r.Spec).(*meshproto.ServiceProviderMetadata)
		if !ok {
			logger.Warnf("failed to clone spec %v, spec is not conformed to %s", r.Spec, r.ResourceKind())
			return out
		}
		out.Spec = spec
	}

	return out
}

func (r *ServiceProviderMetadataResource) String() string {
	jsonStr, err := json.Marshal(r)
	if err != nil {
		logger.Errorf("failed to encode ServiceProviderMetadataResource: %s to json, err: %v", r.ResourceKey(), err)
		return ""
	}
	return string(jsonStr)
}

func NewServiceProviderMetadataResourceWithAttributes(name string, mesh string) *ServiceProviderMetadataResource {
	return &ServiceProviderMetadataResource{
		TypeMeta: metav1.TypeMeta{
			Kind:       string(ServiceProviderMetadataKind),
			APIVersion: "v1alpha1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:   name,
			Labels: map[string]string{},
		},
		Mesh: mesh,
		Spec: &meshproto.ServiceProviderMetadata{},
	}
}

func NewServiceProviderMetadataResource() coremodel.Resource {
	return &ServiceProviderMetadataResource{
		TypeMeta: metav1.TypeMeta{
			Kind:       string(ServiceProviderMetadataKind),
			APIVersion: "v1alpha1",
		},
		Spec: &meshproto.ServiceProviderMetadata{},
	}
}

type ServiceProviderMetadataResourceList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []*ServiceProviderMetadataResource `json:"items"`
}

func (r *ServiceProviderMetadataResourceList) DeepCopyObject() k8sruntime.Object {
	out := &ServiceProviderMetadataResourceList{
		TypeMeta: r.TypeMeta,
	}
	r.ListMeta.DeepCopyInto(&out.ListMeta)

	if len(r.Items) == 0 {
		return out
	}
	out.Items = make([]*ServiceProviderMetadataResource, len(r.Items))
	for i := range r.Items {
		out.Items[i] = r.Items[i].DeepCopyObject().(*ServiceProviderMetadataResource)
	}
	return out
}

func NewServiceProviderMetadataResourceList() coremodel.ResourceList {
	return &ServiceProviderMetadataResourceList{
		TypeMeta: metav1.TypeMeta{
			Kind:       string(ServiceProviderMetadataKind),
			APIVersion: "v1alpha1",
		},
		Items: make([]*ServiceProviderMetadataResource, 0),
	}
}

func (r *ServiceProviderMetadataResourceList) SetItems(items []coremodel.Resource) {
	r.Items = make([]*ServiceProviderMetadataResource, len(items))
	for i := range items {
		res, ok := items[i].(*ServiceProviderMetadataResource)
		if !ok {
			logger.Errorf("unexpected resource type, expected: %s, get %s", ServiceProviderMetadataKind, res.ResourceKind())
			continue
		}
		r.Items[i] = res
	}
}

func NewServiceProviderMetadataResourceListWithItems(items ...*ServiceProviderMetadataResource) *ServiceProviderMetadataResourceList {
	return &ServiceProviderMetadataResourceList{
		TypeMeta: metav1.TypeMeta{
			Kind:       string(ServiceProviderMetadataKind),
			APIVersion: "v1alpha1",
		},
		Items: items,
	}
}
