package config

import log "github.com/sirupsen/logrus"

var cache ServiceConfig

func Init(config ServiceConfig) {
	cache = config
	initLog()
	logSwitchInfo()
	log.Infof("config init success. %v", cache)
}

func initLog() {
	loglevel, err := log.ParseLevel(cache.LogLevel)
	if err != nil {
		log.Panicf("parse log level failed. %s", err.Error())
	}
	log.SetLevel(loglevel)
}

func logSwitchInfo() {
	if cache.InstanceMeteringConfig == nil {
		return
	}
	log.Infof("repoter full switch time: %d", cache.InstanceMeteringConfig.BillingReportSwitchTime)
	for _, tenant := range cache.InstanceMeteringConfig.TenantBlockListFullSwitchTime {
		log.Infof("repoter full switch block list tenant %s, blocked until %d", tenant.TenantId, tenant.SwitchTime)
	}
	for _, tenant := range cache.InstanceMeteringConfig.TenantWithSwitchTimes {
		log.Infof("repoter tenant %s switch time: %d", tenant.TenantId, tenant.SwitchTime)
	}
	for name, gate := range cache.InstanceMeteringConfig.FeatureGateFilter {
		log.Infof("feature gate: %s full switch time is %d", name, gate.FullSwitchTime)
		for _, tenant := range gate.Tenants {
			log.Infof("eature gate: %s ,tenant %s switch time: %d", name, tenant.TenantId, tenant.SwitchTime)
		}
		for _, project := range gate.Projects {
			log.Infof("eature gate: %s ,project %s switch time: %d", name, project.ProjectId, project.SwitchTime)
		}
		for _, cluster := range gate.Clusters {
			log.Infof("eature gate: %s ,cluster %s switch time: %d", name, cluster.ClusterId, cluster.SwitchTime)
		}
	}
}

func GetConfig() ServiceConfig {
	return cache
}
