package stringUtil

import (
	"bytes"
	"fmt"
	"reflect"
)

// map转换为字符串(如果类型不匹配)
// data:map数据
// separator1:间隔符1
// separator1:间隔符2
// 返回值:
// result:转换后的字符串
// err:错误信息
func MapToString(data interface{}, separator1, separator2 string) (result string, err error) {
	if data == nil {
		return
	}

	val := reflect.ValueOf(data)
	if val.Kind() != reflect.Map {
		err = fmt.Errorf("只能转换Map类型的，当前类型是:%v", val.Kind().String())
		return
	}

	if val.Len() <= 0 {
		return
	}

	for _, keyItem := range val.MapKeys() {
		valItem := val.MapIndex(keyItem)
		result = result + fmt.Sprintf("%v%s%v%s", keyItem.Interface(), separator1, valItem.Interface(), separator2)
	}

	result = result[:len(result)-1]

	return
}

// map转换为字符串(如果类型不匹配)
// data:map数据
// separator1:间隔符1
// separator1:间隔符2
// valGetFunc:结果值获取函数
// 返回值:
// result:转换后的字符串
// err:错误信息
func MapToString2(data interface{}, separator1, separator2 string, valGetFunc func(val interface{}) interface{}) (result string, err error) {
	if data == nil {
		return
	}

	val := reflect.ValueOf(data)
	if val.Kind() != reflect.Map {
		err = fmt.Errorf("只能转换Map类型的，当前类型是:%v", val.Kind().String())
		return
	}

	if val.Len() <= 0 {
		return
	}

	for _, keyItem := range val.MapKeys() {
		valItem := val.MapIndex(keyItem)
		result = result + fmt.Sprintf("%v%s%v%s", keyItem.Interface(), separator1, valGetFunc(valItem.Interface()), separator2)
	}

	result = result[:len(result)-1]

	return
}

// 把一个集合转换成字符串
// data:slice类型的集合
// separator:分隔符
// 返回值:
// result:转换后的字符串
// err:错误信息对象
func SliceToString(data interface{}, separator string) (result string, err error) {
	if data == nil {
		return
	}

	value := reflect.ValueOf(data)
	if value.Kind() != reflect.Slice && value.Kind() != reflect.Array {
		err = fmt.Errorf("目标类型不正确，只能是slice或array 当前类型是:%v", value.Kind().String())
		return
	}

	if value.Len() <= 0 {
		return
	}

	for i := 0; i < value.Len(); i++ {
		valItem := value.Index(i)
		result = result + fmt.Sprintf("%s%v", separator, valItem.Interface())
	}
	result = result[1:]

	return
}

func StringListToString(list []string, delimiter string) string {
	var buffer bytes.Buffer
	for i, v := range list {
		if i != len(list)-1 {
			buffer.WriteString(v)
			buffer.WriteString(delimiter)
		} else {
			buffer.WriteString(v)
		}
	}

	return buffer.String()
}

func IntListToString(list []int, delimiter string) string {
	var buffer bytes.Buffer
	for i, v := range list {
		if i != len(list)-1 {
			buffer.WriteString(fmt.Sprintf("%d", v))
			buffer.WriteString(delimiter)
		} else {
			buffer.WriteString(fmt.Sprintf("%d", v))
		}
	}

	return buffer.String()
}

func Int32ListToString(list []int32, delimiter string) string {
	var buffer bytes.Buffer
	for i, v := range list {
		if i != len(list)-1 {
			buffer.WriteString(fmt.Sprintf("%d", v))
			buffer.WriteString(delimiter)
		} else {
			buffer.WriteString(fmt.Sprintf("%d", v))
		}
	}

	return buffer.String()
}

func Int64ListToString(list []int64, delimiter string) string {
	var buffer bytes.Buffer
	for i, v := range list {
		if i != len(list)-1 {
			buffer.WriteString(fmt.Sprintf("%d", v))
			buffer.WriteString(delimiter)
		} else {
			buffer.WriteString(fmt.Sprintf("%d", v))
		}
	}

	return buffer.String()
}
