package utils

import (
	"bytes"
	"encoding/binary"
	"encoding/json"
)

// IntToBytes int to []bytes littleEndian
func IntToBytes(n int32) []byte {
	x := int32(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.LittleEndian, x)
	return bytesBuffer.Bytes()
}

// UintToBytes  uint to []bytes littleEndian
func UintToBytes(n uint32) []byte {
	x := uint32(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.LittleEndian, x)
	return bytesBuffer.Bytes()
}

// UintTobytesBigEnd uint to []bytes bigEndian
func UintTobytesBigEnd(n uint32) []byte {
	x := uint32(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.BigEndian, x)
	return bytesBuffer.Bytes()
}

// Int32TobytesBigEnd int32 to []bytes bigEndian
func Int32TobytesBigEnd(n int32) []byte {
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.BigEndian, n)
	return bytesBuffer.Bytes()
}

// Uint64ToBytes to []bytes
func Uint64ToBytes(n uint64) []byte {
	x := n
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.LittleEndian, x)
	return bytesBuffer.Bytes()
}

// Uint64ToBytesBigEnd to []bytes
func Uint64ToBytesBigEnd(n uint64) []byte {
	x := n
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.BigEndian, x)
	return bytesBuffer.Bytes()
}

// BytesToInt 字节转换成整形
func BytesToInt(b []byte) int32 {
	bytesBuffer := bytes.NewBuffer(b)

	var x int32
	_ = binary.Read(bytesBuffer, binary.LittleEndian, &x)

	return x
}

//BytesToUint32 to []bytes
func BytesToUint32(b []byte) uint32 {
	bytesBuffer := bytes.NewBuffer(b)

	var x int32
	_ = binary.Read(bytesBuffer, binary.LittleEndian, &x)

	return uint32(x)
}

//BytesBigEndToUint32 to uint
func BytesBigEndToUint32(b []byte) uint32 {
	bytesBuffer := bytes.NewBuffer(b)

	var x int32
	_ = binary.Read(bytesBuffer, binary.BigEndian, &x)

	return uint32(x)
}

//Uint16TobytesBigEnd to []bytes
func Uint16TobytesBigEnd(n uint16) []byte {
	x := uint16(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	_ = binary.Write(bytesBuffer, binary.BigEndian, x)
	return bytesBuffer.Bytes()
}

//BytesBigEndToUint16 to uint16
func BytesBigEndToUint16(b []byte) uint16 {
	bytesBuffer := bytes.NewBuffer(b)

	var x int16
	_ = binary.Read(bytesBuffer, binary.BigEndian, &x)

	return uint16(x)
}

//BytesBigEndToUint64 to uint64
func BytesBigEndToUint64(b []byte) uint64 {
	bytesBuffer := bytes.NewBuffer(b)

	var x uint64
	_ = binary.Read(bytesBuffer, binary.BigEndian, &x)

	return x
}

//HTONL32 转换为网络字节序
func HTONL32(v uint32) []byte {
	return UintTobytesBigEnd(v)
}

//HTONLint32 转换为网络字节序
func HTONLInt32(v int32) []byte {
	return Int32TobytesBigEnd(v)
}

//HTONS 转换为网络字节序
func HTONS(v uint16) []byte {
	return Uint16TobytesBigEnd(v)
}

//NTOHL32 转换为主机字节序
func NTOHL32(buf []byte) uint32 {
	return BytesBigEndToUint32(buf)
}

//NTOHL64 转换为主机字节序
func NTOHL64(buf []byte) uint64 {
	return BytesBigEndToUint64(buf)
}

//NTOHS 主机字节序
func NTOHS(buf []byte) uint16 {
	return BytesBigEndToUint16(buf)
}

//DebugString 结构体对应的Json串
func DebugString(v interface{}) string {
	if buf, err := json.MarshalIndent(v, " ", " "); err == nil {
		return string(buf)
	}
	return ""
}
