package converter

import (
	"strconv"
	"strings"
)

func CidrToInt64(cidr string) int64 {

	bits := strings.Split(cidr, ".")

	b0, _ := strconv.Atoi(bits[0])
	b1, _ := strconv.Atoi(bits[1])
	b2, _ := strconv.Atoi(bits[2])
	//b3, _ := strconv.Atoi(bits[3])

	masks := strings.Split(bits[3], "/")

	bb3 := masks[0]
	bb4 := masks[1]

	b3, _ := strconv.Atoi(bb3)
	b4, _ := strconv.Atoi(bb4)

	var sum int64

	sum += int64(b0) << 32
	sum += int64(b1) << 24
	sum += int64(b2) << 16
	sum += int64(b3) << 8
	sum += int64(b4)

	return sum

}

func Int64ToCidr(cidrInt int64) string {

	c1 := cidrInt & 0xFF
	c2 := (cidrInt >> 8) & 0xFF
	c3 := (cidrInt >> 16) & 0xFF
	c4 := (cidrInt >> 24) & 0xFF
	c5 := (cidrInt >> 32) & 0xFF

	var cidr string

	cidr += strconv.FormatInt(c5, 10)
	cidr += "."
	cidr += strconv.FormatInt(c4, 10)
	cidr += "."
	cidr += strconv.FormatInt(c3, 10)
	cidr += "."
	cidr += strconv.FormatInt(c2, 10)
	cidr += "/"
	cidr += strconv.FormatInt(c1, 10)

	return cidr
}
