package config

import (
	"cicv-data-closedloop/common/config/c_log"
	"cicv-data-closedloop/common/util"
	"gopkg.in/yaml.v3"
	"os"
	"strings"
	"sync"
	"time"
)

type MonitorStruct struct {
	Url string `yaml:"url"`
}

type platform struct {
	UrlDeviceAuth string `yaml:"url-device-auth"`
	UrlTaskPoll   string `yaml:"url-task-poll"`
	UrlTask       string `yaml:"url-task"`
}

type rosbagStruct struct {
	Path string   `yaml:"path"`
	Envs []string `yaml:"envs"`
}

type hostStruct struct {
	Name   string       `yaml:"name"`
	Ip     string       `yaml:"ip"`
	Topics []string     `yaml:"topics"`
	Rosbag rosbagStruct `yaml:"rosbag"`
}

type ros struct {
	MasterAddress string   `yaml:"master-address"`
	Nodes         []string `yaml:"nodes"`
}

type disk struct {
	Name string `yaml:"name"`
	Used uint64 `yaml:"used"`
}
type trigger struct {
	Label  string   `yaml:"label"`
	Topics []string `yaml:"topics"`
}

type cloudConfig struct {
	FullCollect           bool          `yaml:"full-collect"`
	ConfigRefreshInterval int           `yaml:"config-refresh-interval"` // 配置刷新时间间隔
	BagNumber             int           `yaml:"bag-number"`
	TimeWindowSendGap     int           `yaml:"time-window-send-gap"` // 主节点向从节点发送窗口的最小时间间隔
	MapBagPath            string        `yaml:"map-bag-path"`
	TfstaticBagPath       string        `yaml:"tfstatic-bag-path"`
	CostmapBagPath        string        `yaml:"costmap-bag-path"`
	BagDataDir            string        `yaml:"bag-data-dir"`
	BagCopyDir            string        `yaml:"bag-copy-dir"`
	TriggersDir           string        `yaml:"triggers-dir"`
	RpcPort               string        `yaml:"rpc-port"`
	Triggers              []trigger     `yaml:"triggers"`
	Hosts                 []hostStruct  `yaml:"hosts"`
	Ros                   ros           `yaml:"ros"`
	Platform              platform      `yaml:"platform"`
	Disk                  disk          `yaml:"disk"`
	Monitor               MonitorStruct `yaml:"monitor"`
}

var (
	CloudConfig      cloudConfig
	CloudConfigMutex sync.RWMutex
)

// InitCloudConfig 初始化业务配置
func InitCloudConfig() {
	// history20240401:朴津机器人额外加一个获取sn码
	var snCode string
	for {
		time.Sleep(time.Duration(2) * time.Second)
		var command []string
		command = append(command, "get")
		command = append(command, "sn")
		_, snOutput, err := util.ExecuteSync(LocalConfig.RosparamPath, command...)
		if err != nil {
			c_log.GlobalLogger.Error("执行获取sn码命令", command, "出错:", err)
			continue
		}
		c_log.GlobalLogger.Error("执行获取sn码命令", command, "成功,结果为:", snOutput)
		snCode = strings.Replace(strings.Replace(snOutput, " ", "", -1), "\n", "", -1)
		LocalConfig.SecretKey = snCode
		LocalConfig.EquipmentNo = "pjibot-" + snCode
		break
	}
	c_log.GlobalLogger.Info("本地机器人sn码为:", snCode)

	c_log.GlobalLogger.Info("初始化OSS配置文件 - 开始。")
	// 获取文件的目录
	_ = util.CreateParentDir(LocalConfig.CloudConfigLocalPath)
	// 3 ------- 获取 yaml 字符串 -------
	cloudConfigObjectKey := LocalConfig.OssBasePrefix + LocalConfig.EquipmentNo + "/" + LocalConfig.CloudConfigFilename
	for {
		time.Sleep(time.Duration(2) * time.Second)
		OssMutex.Lock()
		err := OssBucket.GetObjectToFile(cloudConfigObjectKey, LocalConfig.CloudConfigLocalPath)
		OssMutex.Unlock()
		if err != nil {
			c_log.GlobalLogger.Error("下载 OSS 上的配置文件 "+cloudConfigObjectKey+" 失败,请尽快在 OSS 上传配置文件。", err)
			continue
		}
		break
	}

	content, err := os.ReadFile(LocalConfig.CloudConfigLocalPath)
	if err != nil {
		c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 读取失败:", err)
		os.Exit(-1)
	}

	// 4 ------- 解析YAML内容 -------
	var newCloudConfig cloudConfig
	err = yaml.Unmarshal(content, &newCloudConfig)
	if err != nil {
		c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
		os.Exit(-1)
	}

	// 5 ------- 校验 yaml -------
	if checkCloudConfig(newCloudConfig) {
		CloudConfigMutex.RLock()
		CloudConfig = newCloudConfig
		CloudConfigMutex.RUnlock()
	} else {
		c_log.GlobalLogger.Error("程序崩溃,配置文件格式错误:", newCloudConfig)
		os.Exit(-1)
	}
	c_log.GlobalLogger.Info("初始化OSS配置文件 - 成功。")
	util.CreateDir(CloudConfig.BagDataDir)
	util.CreateDir(CloudConfig.BagCopyDir)
}

// refreshCloudConfig 更新业务配置
func refreshCloudConfig() {
	// 获取文件的目录
	_ = util.CreateParentDir(LocalConfig.CloudConfigLocalPath)
	// 3 ------- 获取 yaml 字符串 -------
	var content []byte
	cloudConfigObjectKey := LocalConfig.OssBasePrefix + LocalConfig.EquipmentNo + "/" + LocalConfig.CloudConfigFilename
	OssMutex.Lock()
	err := OssBucket.GetObjectToFile(cloudConfigObjectKey, LocalConfig.CloudConfigLocalPath)
	OssMutex.Unlock()
	if err != nil {
		c_log.GlobalLogger.Error("下载oss上的配置文件"+cloudConfigObjectKey+"失败。", err)
		//os.Exit(-1)
	}

	content, err = os.ReadFile(LocalConfig.CloudConfigLocalPath)
	if err != nil {
		c_log.GlobalLogger.Error("配置文件 ", LocalConfig.CloudConfigLocalPath, " 读取失败:", err)
		os.Exit(-1)
	}

	// 4 ------- 解析YAML内容 -------
	var newCloudConfig cloudConfig
	err = yaml.Unmarshal(content, &newCloudConfig)
	if err != nil {
		c_log.GlobalLogger.Error("配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
		os.Exit(-1)
	}

	// 5 ------- 校验 yaml -------
	if checkCloudConfig(newCloudConfig) {
		CloudConfigMutex.RLock()
		CloudConfig = newCloudConfig
		CloudConfigMutex.RUnlock()
	} else {
		c_log.GlobalLogger.Error("配置文件格式错误:", newCloudConfig)
		os.Exit(-1)
	}
	util.CreateDir(CloudConfig.BagDataDir)
	util.CreateDir(CloudConfig.BagCopyDir)

}

// RefreshCloudConfig 轮询oss上的配置文件更新到本地
func RefreshCloudConfig() {
	for {
		time.Sleep(time.Duration(CloudConfig.ConfigRefreshInterval) * time.Second)
		refreshCloudConfig()
	}
}

// CheckConfig 校验 cfg.yaml 文件
func checkCloudConfig(check cloudConfig) bool {
	if len(check.Hosts) != 1 {
		c_log.GlobalLogger.Error("cloud-config.yaml中配置的hosts必须为1。")
		os.Exit(-1)
	}
	return true
}