package ip2region

import (
	"IPRegion/util"
	"fmt"
	//"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
)

func GetIPInfo(address, searchType string) (ipInfo IpInfo, err error) {
	//https://gitee.com/lionsoul/ip2region/raw/master/data/ip2region.db
	region, err := New("ip2region.db")
	defer region.Close()
	if err != nil {
		fmt.Println(err)
		return
	}
	var ipAddress string
	if util.MatchIP(address) {
		ipAddress = address
	} else {
		ipAddress, err = util.GetIPByDomain(address)
		if err != nil {
			//提供非法ip 返回空结构体
			ipAddress = "266.266.266.266"
		}
	}
	switch searchType {
	case "Memory":
		ip, err := region.MemorySearch(ipAddress)
		return ip, err
	case "Binary":
		ip, err := region.BinarySearch(ipAddress)
		return ip, err
	case "Btree":
		ip, err := region.BtreeSearch(ipAddress)
		return ip, err
	default:
		ip, err := region.MemorySearch(ipAddress)
		return ip, err
	}

}
