// hash_dgb.go
package dgb

/*
#cgo CFLAGS : -I../include
#cgo LDFLAGS: -L../lib -lsodium -lmultihash -lx1x

#include <stdio.h>
#include <stdlib.h>
#include "skein.h"
#include "qubit.h"
#include "odocrypt.h"
*/
import "C"

import (
	"bytes"
	//"encoding/hex"
	"log"
	"unsafe"

	"github.com/btcsuite/btcd/chaincfg/chainhash"
	"github.com/btcsuite/btcd/wire"
)

const DGB_BLOCK_VERSION_ALGO int32 = (7 << 9)

// DGB
func Build_DgbPowHash(h wire.BlockHeader) chainhash.Hash {
	outputs := make([]byte, 32)
	var powhash chainhash.Hash
	buf := bytes.NewBuffer(make([]byte, 0, wire.MaxBlockHeaderPayload))
	err := h.Serialize(buf)
	if err != nil {
		log.Println("[dgb] powhash: Serialize error:", err)
		return powhash
	}
	//test_dgb_pow := "0000002013bea8369753c2a405f4704f698f7322c0a52f3ab89e853a14c3b124cf667d098612a82ce1741ab44ae31b5aeb6a7e622386cc4baeb57a046ed1d0fa676fee6cd0f008678ef70f1a6e9deb7a"
	//test_dgb_bytes, _ := hex.DecodeString(test_dgb_pow)

	//log.Println("[dgb]in", hex.EncodeToString(buf.Bytes()))
	in := (*C.char)(C.CBytes(buf.Bytes()))
	//in := (*C.char)(C.CBytes(test_dgb_bytes))
	defer C.free(unsafe.Pointer(in))

	output := (*C.char)(C.malloc(32))
	if output == nil {
		log.Println("[dgb] powhash: malloc failed")
		return powhash
	}
	defer C.free(unsafe.Pointer(output))

	algo := (h.Version & DGB_BLOCK_VERSION_ALGO) >> 9
	switch algo {
	case 3:
		C.skein_hash(in, output, C.uint(wire.MaxBlockHeaderPayload))
	case 4:
		C.qubithash(unsafe.Pointer(output), unsafe.Pointer(in))
	case 7:
		var nShapechangeInterval uint32 = 10 * 24 * 60 * 60
		var t uint32 = uint32(h.Timestamp.Unix())
		var key uint32 = uint32(t - t%nShapechangeInterval)
		C.HashOdo(output, in, C.uint(key))
	}

	//outputs := C.GoBytes((*C.uint8_t)((unsafe.Pointer)(output)), 32)
	/*p := uintptr(unsafe.Pointer(output))
	for i := 0; i < 32; i++ {
		j := *(*byte)(unsafe.Pointer(p))
		outputs[i] = j
		p += unsafe.Sizeof(j)
	}*/
	outputs = C.GoBytes(unsafe.Pointer(output), 32)
	//log.Println("[dgb]out", outputs)

	err = powhash.SetBytes(outputs)
	if err != nil {
		log.Println("[dgb] powhash: SetBytes error:", err)
		return powhash
	}
	return powhash
}