package config

import (
	"fmt"
)

const (
	// TODO: use image in private registry
	// RegistryPrefix is the default public docker image registry prefix
	RegistryPrefix = "gcr.io/pingcap-public/dbaas"
	// ThirdPartyRegistryPrefix is the default third-party docker image registry prefix
	ThirdPartyRegistryPrefix = "gcr.io/pingcap-public/third-party"

	// GottyImage is the name of gotty image
	GottyImage = "gotty"

	// BusyboxImage is the name of busybox image
	BusyboxImage = "busybox"

	// HealthCheckerImage is the name of tidb health-checker sidecar image
	HealthCheckerImage = "health-checker"

	// GracefulShutdownTiDBWebhookImage is the name of graceful-shutdown-tidb-webhook image
	GracefulShutdownTiDBWebhookImage = "graceful-shutdown-tidb-webhook"

	// GrafanaImage is the name of grafana image
	GrafanaImage = "grafana"

	// PrometheusImage is the name of prometheus image
	PrometheusImage = "prometheus"

	// TiDBMonitorReloaderImage is the name of tidb-monitor-reloader image
	TiDBMonitorReloaderImage = "tidb-monitor-reloader"

	// TiDBMonitorInitializerImage is the name of tidb-monitor-initializer image
	TiDBMonitorInitializerImage = "tidb-monitor-initializer"

	// TiDBBackupManagerImage is the name of tidb backup manager image
	TiDBBackupManagerImage = "tidb-backup-manager"

	// TiDBOperatorImage is the name of TiDB Operator image
	TiDBOperatorImage = "tidb-operator"

	// AdvancedStatefulsetImage is the name of AdvancedStatefulSet controller image
	AdvancedStatefulsetImage = "advanced-statefulset"

	// MysqlClientImage is the image name that contains python mysql client
	MysqlClientImage = "tnir/mysqlclient"

	VectorImage = "vector"

	VmagentImage = "victoriametrics/vmagent"
)

// copied from "github.com/tidbcloud/infra-provider/pkg/gitops-sdk/consts"
const (
	OverrideRegistryKey    = "eks.infra.pingcap.com/override-cluster-registry"
	OverrideRepoPrefixKey  = "eks.infra.pingcap.com/override-cluster-repoprefix"
	OverrideHelperImageKey = "eks.infra.pingcap.com/override-cluster-helper-image"
)

var (
	// GottyVersion is the version of GottyImage
	GottyVersion = "20211103"

	// BusyboxVersion is the version of BusyboxImage
	BusyboxVersion = "1.31.1"

	// HealthCheckerVersion is the version of HealthCheckerImage
	HealthCheckerVersion = "20201224"

	// GracefulShutdownTiDBWebhookVersion is the version of GracefulShutdownTiDBWebhookImage
	GracefulShutdownTiDBWebhookVersion = "20210107"

	// MysqlClientVersion is the version of python mysql client docker image
	MysqlClientVersion = "20201014"

	// GrafanaVersion is the version of grafana image
	GrafanaVersion = "6.0.1"

	// PrometheusVersion is the version of prometheus image
	PrometheusVersion = "v2.11.1"

	// TiDBMonitorReloaderVersion is the version of tidb-monitor-reloader image
	TiDBMonitorReloaderVersion = "v1.0.1"

	// TiDBOperatorVersion is the version of TiDB Operator image
	TiDBOperatorVersion = "v1.2.6"

	// AdvancedStatefulsetVersion is the version of AdvancedStatefulsetImage
	AdvancedStatefulsetVersion = "v0.5.0"

	VectorForLogVersion = "0.31.0-2-debian"

	VectorForClusterVersion = "0.31.0-2-debian"

	VmagentVersion = "v1.78.0"
)

func GetImage(image, version string) string {
	return fmt.Sprintf("%s:%s", GetBaseImage(image), version)
}

func GetBaseImage(image string) string {
	return fmt.Sprintf("%s/%s", RegistryPrefix, image)
}

func GetThirdPartyImage(image, version string) string {
	return fmt.Sprintf("%s/%s:%s", ThirdPartyRegistryPrefix, image, version)
}

func GetGottyImage() string {
	return GetImage(GottyImage, GottyVersion)
}

func GetBusyboxImage() string {
	return GetImage(BusyboxImage, BusyboxVersion)
}

func GetHealthCheckerImage() string {
	return GetImage(HealthCheckerImage, HealthCheckerVersion)
}

func GetGracefulShutdownTiDBWebhookImage() string {
	return GetImage(GracefulShutdownTiDBWebhookImage, GracefulShutdownTiDBWebhookVersion)
}

func GetTiDBBackupManagerImage() string {
	return GetImage(TiDBBackupManagerImage, TiDBOperatorVersion)
}

func GetTiDBOperatorImage() string {
	return GetImage(TiDBOperatorImage, TiDBOperatorVersion)
}

func GetAdvancedStatefulsetImage() string {
	return GetImage(AdvancedStatefulsetImage, AdvancedStatefulsetVersion)
}

func GetMysqlClientImage() string {
	return GetThirdPartyImage(MysqlClientImage, MysqlClientVersion)
}

func GetThirdPartyRepo(image string) string {
	return fmt.Sprintf("%s/%s", ThirdPartyRegistryPrefix, image)
}

func GetVectorForLogRepo() string {
	return GetBaseImage(VectorImage)
}

func GetVectorForClusterImage() string {
	return GetImage(VectorImage, VectorForClusterVersion)
}

func GetVmagentRepo() string {
	return GetThirdPartyRepo(VmagentImage)
}

func GetOverrideBusyboxImage(anno map[string]string) string {
	helperImage := GetBusyboxImage()
	if anno == nil {
		return helperImage
	}
	if img := anno[OverrideHelperImageKey]; img != "" {
		helperImage = img
	}
	return helperImage
}
