/*

 Copyright (c) [2023] [pcm]
 [pcm-coordinator] is licensed under Mulan PSL v2.
 You can use this software according to the terms and conditions of the Mulan PSL v2.
 You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 See the Mulan PSL v2 for more details.

*/

package utils

import (
	"encoding/json"
	"fmt"
	"github.com/robfig/cron/v3"
	"io"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime"
	"mime/multipart"
	"sigs.k8s.io/yaml"
	"strconv"
	"strings"
)

// Convert 通过JSON赋值
func Convert(source interface{}, target interface{}) {
	jsonByte, _ := json.Marshal(source)
	json.Unmarshal(jsonByte, &target)
}

// Int64ToString int64转string
func Int64ToString(value int64) string {
	return strconv.FormatInt(value, 10)
}

// EntryIdToString EntryID转string
func EntryIdToString(id cron.EntryID) string {
	return strconv.Itoa(int(id))
}

func StringToInt(value string) int {
	intValue, _ := strconv.Atoi(value)
	return intValue
}
func StringToInt64(value string) int64 {
	intValue, _ := strconv.ParseInt(value, 10, 64)
	return intValue
}

func StringToFloat64(value string) float64 {
	floatValue, _ := strconv.ParseFloat(value, 64)
	return floatValue
}

func RunTimeToSeconds(runTime string) int {
	time := strings.Split(runTime, ":")
	day, _ := strconv.Atoi(time[0])
	hour, _ := strconv.Atoi(time[1])
	seconds, _ := strconv.Atoi(time[2])
	return day*3600 + hour*60 + seconds
}

func Yaml2struct(fileHeader *multipart.FileHeader, req interface{}) error {
	file, err := fileHeader.Open()
	if err != nil {
		return err
	}
	fileByte, err := io.ReadAll(file)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal(fileByte, &req)
	if err != nil {
		return err
	}
	return nil
}

func K8sUnstructured(dataString string, target interface{}) {
	unstructuredList := unstructured.UnstructuredList{}
	json.Unmarshal([]byte(dataString), &unstructuredList)
	runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredList.UnstructuredContent(), target)
}

// removeDuplication_map 去重数组
func RemoveDuplication_map(arr []string) []string {
	set := make(map[string]struct{}, len(arr))
	j := 0
	for _, v := range arr {
		_, ok := set[v]
		if ok {
			continue
		}
		set[v] = struct{}{}
		arr[j] = v
		j++
	}
	return arr[:j]
}

func ConvertStructToMap(in interface{}) map[string]string {
	out := make(map[string]string)

	b, _ := json.Marshal(&in)
	var m map[string]interface{}
	_ = json.Unmarshal(b, &m)

	for k, v := range m {
		out[k] = fmt.Sprintf("%v", v)
	}
	return out
}

func StringToYaml(in string) *string {
	// 将Go字符串转换为字节数组
	byteArray := []byte(in)
	// 解析YAML数据
	var yamlData map[string]interface{}
	err := yaml.Unmarshal(byteArray, &yamlData)
	// 将解析后的数据转换为YAML格式
	yamlString, err := yaml.Marshal(yamlData)
	if err != nil {
		fmt.Println(err)
	}
	resp := string(yamlString)
	return &resp
}
