/*
Copyright 2021 The KodeRover 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 config

import (
	"fmt"
	"path/filepath"

	"github.com/spf13/viper"

	"github.com/koderover/zadig/pkg/setting"
)

// SystemAddress is the fully qualified domain name of the system, or an IP Address.
// Port and protocol are required if necessary.
// for example: foo.bar.com, https://for.bar.com, http://1.2.3.4:5678
func SystemAddress() string {
	return viper.GetString(setting.ENVSystemAddress)
}

func Enterprise() bool {
	return viper.GetBool(setting.ENVEnterprise)
}

func Mode() string {
	mode := viper.GetString(setting.ENVMode)
	if mode == "" {
		return setting.DebugMode
	}

	return mode
}

func LogLevel() string {
	return "debug"
}

func SendLogToFile() bool {
	return true
}

func LogPath() string {
	return fmt.Sprintf("/var/log/%s/", setting.ProductName)
}

func LogName() string {
	return "product.log"
}

func RequestLogName() string {
	return "request.log"
}

func LogFile() string {
	return LogPath() + LogName()
}

func RequestLogFile() string {
	return LogPath() + RequestLogName()
}

func GetServiceByCode(code int) *setting.ServiceInfo {
	return setting.Services[code]
}

func AslanServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.Aslan)
}

func UserServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.User)
}

func SecretKey() string {
	return viper.GetString(setting.ENVSecretKey)
}

func AslanServiceAddress() string {
	s := AslanServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func UserServiceAddress() string {
	s := UserServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func AslanxServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.Aslanx)
}

func AslanxServiceAddress() string {
	s := AslanxServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func HubServerServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.HubServer)
}

func HubServerServiceAddress() string {
	s := HubServerServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func ClairServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.Clair)
}

func ClairServiceAddress() string {
	s := ClairServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func CollieServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.Collie)
}

func CollieServiceAddress() string {
	s := CollieServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func WarpDriveServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.WarpDrive)
}

func WarpDriveServiceName() string {
	return WarpDriveServiceInfo().Name
}

func OPAServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.OPA)
}

func OPAServiceAddress() string {
	s := OPAServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func VendorServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.Vendor)
}

func VendorServiceAddress() string {
	s := VendorServiceInfo()
	return GetServiceAddress(s.Name, s.Port)
}

func GetServiceAddress(name string, port int32) string {
	return fmt.Sprintf("http://%s:%d", name, port)
}

func MinioServiceInfo() *setting.ServiceInfo {
	return GetServiceByCode(setting.Minio)
}

func MinioServiceName() string {
	return MinioServiceInfo().Name
}

func DataPath() string {
	return "/app/data"
}
func WorkflowDataPath() string {
	return "/workflow/data"
}

func ObjectStorageServicePath(project, service string) string {
	return filepath.Join(project, "service", service)
}

func ObjectStorageProductionServicePath(project, service string) string {
	return filepath.Join(project, "production-service", service)
}

func ObjectStorageTemplatePath(name, kind string) string {
	return filepath.Join("templates", kind, name)
}

func ObjectStorageDeliveryVersionPath(project string) string {
	return filepath.Join("delivery-distributes", "files", project)
}

func ObjectStorageChartTemplatePath(name string) string {
	return ObjectStorageTemplatePath(name, setting.ChartTemplatesPath)
}

func LocalTestServicePath(project, service string) string {
	return filepath.Join(DataPath(), project, "test", service)
}

func LocalWorkflowServicePath(project, service string) string {
	return filepath.Join(WorkflowDataPath(), project, service)
}

// LocalTestServicePathWithRevision returns a test service path with a given revision.
func LocalTestServicePathWithRevision(project, service, revision string) string {
	return filepath.Join(DataPath(), project, "test", service, revision)
}

// LocalProductionServicePathWithRevision returns a production service path with a given revision.
func LocalProductionServicePathWithRevision(project, service, revision string) string {
	return filepath.Join(DataPath(), project, "production", service, revision)
}

func LocalTemplatePath(name, kind string) string {
	return filepath.Join(DataPath(), "templates", kind, name)
}

func LocalChartTemplatePath(name string) string {
	return LocalTemplatePath(name, setting.ChartTemplatesPath)
}

func MongoURI() string {
	return viper.GetString(setting.ENVMongoDBConnectionString)
}

func MongoDatabase() string {
	return viper.GetString(setting.ENVAslanDBName)
}

func PolicyDatabase() string {
	return MongoDatabase() + "_policy"
}

func MysqlUser() string {
	return viper.GetString(setting.ENVMysqlUser)
}

func MysqlUserDB() string {
	return viper.GetString(setting.ENVMysqlUserDB)
}

func MysqlPassword() string {
	return viper.GetString(setting.ENVMysqlPassword)
}

func MysqlHost() string {
	return viper.GetString(setting.ENVMysqlHost)
}

func Namespace() string {
	return viper.GetString(setting.ENVNamespace)
}

func RoleBindingNameFromUIDAndRole(uid string, role setting.RoleType, roleNamespace string) string {
	return fmt.Sprintf("%s-%s-%s", uid, role, roleNamespace)
}

func BuildResourceKey(resourceType, projectName, labelBinding string) string {
	return fmt.Sprintf("%s-%s-%s", resourceType, projectName, labelBinding)
}

func AdminPassword() string {
	return viper.GetString(setting.ENVAdminPassword)
}

func AdminEmail() string {
	return viper.GetString(setting.ENVAdminEmail)
}
