package util

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
)

func CheckError(err error) (r bool) {
	r = true
	if err != nil {
		// panic(err)
		r = false
	}

	return
}

func Get(url string) (body string, err error) {

	if url == "" {
		err = errors.New("url is empty\n" + url)
		return
	}

	fmt.Println("url:", url)
	resp, err := http.Get(url)
	// if err != nil || resp.StatusCode != 200 {
	// 	err = errors.New("this site is not available\n" + url)
	// 	return
	// }
	var data []byte
	defer resp.Body.Close()
	data, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	body = string(data)
	return
}

func GetFloat32(s string) float32 {
	v, err := strconv.ParseFloat(s, 32)
	if err != nil {
		v = 0
	}
	return float32(v)
}

func Unique(intSlice []string) []string {
	keys := make(map[string]bool)
	list := []string{}
	for _, entry := range intSlice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}

func ChunkSlice(slice []string, chunkSize int) [][]string {
	var chunks [][]string
	for i := 0; i < len(slice); i += chunkSize {
		end := i + chunkSize

		// necessary check to avoid slicing beyond
		// slice capacity
		if end > len(slice) {
			end = len(slice)
		}

		chunks = append(chunks, slice[i:end])
	}

	return chunks
}

func Contains(s []string, str string) bool {
	for _, v := range s {
		if v == str && v != "" {
			return true
		}
	}

	return false
}

func GetPrice(str string) float32 {

	idx := strings.Index(str, "원")
	if idx > 0 {
		str = str[:idx]
	}
	str = strings.Replace(str, ",", "", -1)
	str = strings.TrimSpace(str)
	return GetFloat32(str)
}
