package gotest

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
	"time"
)

func Test_QueryByBlockId(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://101.37.212.15:7783/v1/transbank/blockid`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	postdata := fmt.Sprintf(`{"cointype":"BTC","blockid":"482331","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/blockid"), timestamp))
	request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	resp, err := client.Do(request)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}

	defer resp.Body.Close()
	rbs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}

	t.Logf("read bytes:%v", string(rbs))

	return
}
/*
func Test_QueryByTransIdLoop(t *testing.T) {
	for i := 0; i < 100; i++ {
		Test_QueryByTransId(t)
		t.Logf("Test_QueryByTransIdLoop:%v", i)
	}
}
*/
func Test_QueryByTransId(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://101.37.212.15:7783/v1/transbank/transid`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)
	//transcationid := "fba22732c61d177bb21e0b96ad6a10ee2a8fc19e33156907841453d38a749abd"

	postdata := fmt.Sprintf(`{"cointype":"BTC","transcationid":"6a14fc8c625c92de83dd26643a3094e1191c7ab6fd4c3387093ae04b27abef49","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/transid"), timestamp))
	request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	resp, err := client.Do(request)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	defer resp.Body.Close()
	rbs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	t.Logf("read bytes:%v", string(rbs))
	return
}

func Test_QueryByAddress(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://101.37.212.15:7783/v1/transbank/address`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)
	//address := "LLw3b23h9XmZNirwSFV3ZD17HwVEGaNyMm"

	postdata := fmt.Sprintf(`{"cointype":"BTC","address":"128nRaMd4PSNHhAHGCyJExWqfBJ9mXETZH","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/address"), timestamp))
	request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	resp, err := client.Do(request)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	defer resp.Body.Close()
	rbs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	t.Logf("read bytes:%v", string(rbs))
	return
}

func Test_QueryBlockHeight(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://101.37.212.15:7783/v1/transbank/blockheight`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	postdata := fmt.Sprintf(`{"cointype":"BTC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/v1/transbank/blockheight"), timestamp))
	request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	resp, err := client.Do(request)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	defer resp.Body.Close()
	rbs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	t.Logf("read bytes:%v", string(rbs))
	return
}

func Signature(body []byte, timestamp string) string {

        sha1Contain := sha1.New()
        byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
        sha1Contain.Write(byteSha1)
        localSig := hex.EncodeToString(sha1Contain.Sum(nil))
        return localSig
}

