package utils

import (
	"fmt"
	"strings"
)

type ArrayCmpare interface {
}

//ArrayInt64Diff 返回切片的差集:arr1-arr2
func ArrayInt64Diff(arr1 []int64, arr2 []int64) []int64 {
	if len(arr2) == 0 {
		return arr1
	}
	setmap := make(map[int64]bool)
	for i := range arr2 {
		setmap[arr2[i]] = true
	}
	var result []int64
	for i := range arr1 {
		if _, ok := setmap[arr1[i]]; !ok {
			result = append(result, arr1[i])
		}
	}

	return result
}

//ArrayIntDiff 返回切片的差集:arr1-arr2
func ArrayIntDiff(arr1 []int, arr2 []int) []int {
	if len(arr2) == 0 {
		return arr1
	}
	setmap := make(map[int]bool)
	for i := range arr2 {
		setmap[arr2[i]] = true
	}
	var result []int
	for i := range arr1 {
		if _, ok := setmap[arr1[i]]; !ok {
			result = append(result, arr1[i])
		}
	}

	return result
}

//ArrayStringIn 检查s字符串是否在切片sl中
func ArrayStringIn(arr1 []string, s string) bool {
	for _, v := range arr1 {
		if strings.Compare(v, s) == 0 {
			return true
		}
	}
	return false
}

func JoinInt64s(ids []int64, spilt string) string {
	var idStrings []string = make([]string, len(ids))
	for i := 0; i < len(ids); i++ {
		idStrings[i] = fmt.Sprintf("%v", ids[i])
	}
	return strings.Join(idStrings, spilt)
}

func ArrayInt64Unique(s []int64) []int64 {
	m := map[int64]bool{}
	newS := []int64{}
	for _, v := range s {
		if _, ok := m[v]; ok {
			continue
		}
		newS = append(newS, v)
	}
	return newS
}

//ArraySetString 构建元素不重复的数组
//可按需扩展
type ArraySetString struct {
	m map[string]int
}

func NewArraySetString() ArraySetString {
	return ArraySetString{
		m: make(map[string]int),
	}
}
func (set *ArraySetString) Has(str string) bool {
	if _, ok := set.m[str]; ok {
		return true
	}
	return false
}

//Add 追加元素
func (set *ArraySetString) Add(str string) {
	if _, ok := set.m[str]; ok {
		return
	}
	l := len(set.m)
	set.m[str] = l
}

//Remove 移除指定元素
func (set *ArraySetString) Remove(str string) {
	delete(set.m, str)
}

//Len 元素长度
func (set *ArraySetString) Len() int {
	return len(set.m)
}

//ToSlice 转换为切片输出
func (set *ArraySetString) ToSlice() []string {
	newSlice := make([]string, len(set.m))
	for k, v := range set.m {
		newSlice[v] = k
	}
	return newSlice
}

//ArraySetString 构建元素不重复的数组
//可按需扩展
type ArraySetInt64 struct {
	m map[int64]int
}

func NewArraySetInt64() *ArraySetInt64 {
	return &ArraySetInt64{
		m: make(map[int64]int),
	}
}
func (set *ArraySetInt64) Has(str int64) bool {
	if _, ok := set.m[str]; ok {
		return true
	}
	return false
}

//Add 追加元素
func (set *ArraySetInt64) Add(str int64) {
	if _, ok := set.m[str]; ok {
		return
	}
	l := len(set.m)
	set.m[str] = l
}

//Remove 移除指定元素
func (set *ArraySetInt64) Remove(str int64) {
	delete(set.m, str)
}

//Len 元素长度
func (set *ArraySetInt64) Len() int {
	return len(set.m)
}

//ToSlice 转换为切片输出
func (set *ArraySetInt64) ToSlice() []int64 {
	newSlice := make([]int64, len(set.m))
	for k, v := range set.m {
		newSlice[v] = k
	}
	return newSlice
}