/*
Copyright 2018 The CDI Authors.

Licensed 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.
*/

package namespaced

import (
	"fmt"

	corev1 "k8s.io/api/core/v1"
	rbacv1 "k8s.io/api/rbac/v1"
	"k8s.io/apimachinery/pkg/runtime"

	"sigs.k8s.io/controller-runtime/pkg/client"

	sdkapi "kubevirt.io/controller-lifecycle-operator-sdk/api"
	utils "kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk/resources"
)

// FactoryArgs contains the required parameters to generate all namespaced resources
type FactoryArgs struct {
	OperatorVersion        string `required:"true" split_words:"true"`
	ControllerImage        string `required:"true" split_words:"true"`
	DeployClusterResources string `required:"true" split_words:"true"`
	ImporterImage          string `required:"true" split_words:"true"`
	ClonerImage            string `required:"true" split_words:"true"`
	OvirtPopulatorImage    string `required:"true" split_words:"true"`
	APIServerImage         string `required:"true" envconfig:"apiserver_image"`
	UploadProxyImage       string `required:"true" split_words:"true"`
	UploadServerImage      string `required:"true" split_words:"true"`
	Verbosity              string `required:"true"`
	PullPolicy             string `required:"true" split_words:"true"`
	ImagePullSecrets       []corev1.LocalObjectReference
	PriorityClassName      string
	Namespace              string
	InfraNodePlacement     *sdkapi.NodePlacement
	ControllerReplicas     int32
	APIServerReplicas      int32
	UploadProxyReplicas    int32
}

type factoryFunc func(*FactoryArgs) []client.Object

type namespaceHaver interface {
	SetNamespace(string)
	GetNamespace() string
}

var factoryFunctions = map[string]factoryFunc{
	"apiserver":   createAPIServerResources,
	"controller":  createControllerResources,
	"uploadproxy": createUploadProxyResources,
	"cronjob":     createCronJobResources,
}

var additionalFactoryFunctions = map[string]factoryFunc{
	"networkpolicies": createNetworkPolicies,
}

// CreateAllResources creates all namespaced resources
func CreateAllResources(args *FactoryArgs) ([]client.Object, error) {
	var resources []client.Object
	for group := range factoryFunctions {
		rs, err := CreateResourceGroup(group, args)
		if err != nil {
			return nil, err
		}
		resources = append(resources, rs...)
	}
	return resources, nil
}

// CreateResourceGroup creates namespaced resources for a specific group/component
func CreateResourceGroup(group string, args *FactoryArgs) ([]client.Object, error) {
	f, ok := getFactoryFunc(group)
	if !ok {
		return nil, fmt.Errorf("group %s does not exist", group)
	}
	resources := f(args)
	for _, resource := range resources {
		utils.ValidateGVKs([]runtime.Object{resource})
		assignNamspaceIfMissing(resource, args.Namespace)
	}
	return resources, nil
}

func assignNamspaceIfMissing(resource client.Object, namespace string) {
	obj, ok := resource.(namespaceHaver)
	if !ok {
		return
	}

	if obj.GetNamespace() == "" {
		obj.SetNamespace(namespace)
	}
}

// GetRolePolicyRules returns all namespaced PolicyRules
func GetRolePolicyRules() []rbacv1.PolicyRule {
	result := getAPIServerNamespacedRules()
	result = append(result, getControllerNamespacedRules()...)
	result = append(result, getUploadProxyNamespacedRules()...)
	result = append(result, GetPrometheusNamespacedRules()...)
	return result
}

func getFactoryFunc(group string) (factoryFunc, bool) {
	if f, ok := factoryFunctions[group]; ok {
		return f, true
	}
	if f, ok := additionalFactoryFunctions[group]; ok {
		return f, true
	}
	return nil, false
}
