package character

import (
	"errors"
	"gadmin/utility/snowflake"
	"log"
	"net"
)

var twitSnowNode *snowflake.Node

func InitTwitEquipmentGuid() {
	var machineID uint16 = 1
	if id, err := lower16BitPrivateIP(); err == nil {
		machineID = id
	}
	machineID &= 0x3ff
	var err error
	twitSnowNode, err = snowflake.NewNode(int64(machineID))
	if err != nil {
		log.Fatalln(err)
	}
}

func GetTwitEquipmentGuid() string {
	id := twitSnowNode.Generate()
	return id.String()
}

func privateIPv4() (net.IP, error) {
	as, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}

	for _, a := range as {
		ipnet, ok := a.(*net.IPNet)
		if !ok || ipnet.IP.IsLoopback() {
			continue
		}

		ip := ipnet.IP.To4()
		if isPrivateIPv4(ip) {
			return ip, nil
		}
	}
	return nil, errors.New("no private ip address")
}

func isPrivateIPv4(ip net.IP) bool {
	return ip != nil &&
		(ip[0] == 10 || ip[0] == 172 && (ip[1] >= 16 && ip[1] < 32) || ip[0] == 192 && ip[1] == 168)
}

func lower16BitPrivateIP() (uint16, error) {
	ip, err := privateIPv4()
	if err != nil {
		return 0, err
	}

	return uint16(ip[2])<<8 + uint16(ip[3]), nil
}
