package utils

import (
	"errors"
	"reflect"
	"strconv"
	"strings"
)

func Contain(subTarget interface{}, target interface{}) (bool, error) {
	targetValue := reflect.ValueOf(target)
	switch reflect.TypeOf(target).Kind() {
	case reflect.Slice, reflect.Array:
		for i := 0; i < targetValue.Len(); i++ {
			if targetValue.Index(i).Interface() == subTarget {
				return true, nil
			}
		}
	case reflect.Map:
		if targetValue.MapIndex(reflect.ValueOf(subTarget)).IsValid() {
			return true, nil
		}
	}

	return false, errors.New("not in array")
}

func String2IntArray(ids string, seprate string) []int {
	idsstring := strings.Split(ids, seprate)
	if len(idsstring) == 0 {
		return []int{}
	}

	idsint := make([]int, len(idsstring))
	for k, v := range idsstring {
		tmp, _ := strconv.ParseInt(v, 10, 64)
		idsint[k] = int(tmp)
	}

	return idsint
}

func StringContainsIntNumber(orig string, dot string, number int) bool {
	if len(orig) == 0 || len(dot) == 0 {

	}

	arr := String2IntArray(orig, ",")

	if ret, err := Contain(number, arr); err == nil && ret == true {
		return true
	} else {
		return false
	}
}
