package gotest

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
	"time"
	//"lite"/*local test need*/
)

func Test_newaddr(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/createaddr`)
	//timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	//postdata := fmt.Sprintf(`{"cointype":"BCC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/createaddr"), timestamp))
	postdata := fmt.Sprintf(`{"cointype":"BCC"}`)
	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_Balance(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/coinbalance`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"BCC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/coinbalance"), 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_TransInfo(t *testing.T) {

	//first get block height then test transinfo

	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/query/transinfo`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"BCC","starth":1,"endh":1243398,"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/query/transinfo"), 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_TransferAccounts(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendcoins`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"BCC","amount":"0.5","address":"Lg7zNKVUfEJeeCJ5oDMgVS9s2afCQKxrCr","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/sendcoins"), 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_ChainHeight(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/blockchain/height`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"BCC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/blockchain/height"), 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_ListUnspent(t *testing.T) {
        client := http.DefaultClient
        geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/listunspent`)
        timestamp := fmt.Sprintf("%v", time.Now().Unix())
        t.Logf("post url:%v", geturl)


        postdata := fmt.Sprintf(`{"cointype":"BCC","address":["1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg","14Tud2jEf4rV3TJssKg7VYuoLh6VgRxXBY"],"timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte("/coinproxy/listunspent"), 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_SendRawTransaction(t *testing.T) {
        client := http.DefaultClient
        geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sendrawtransaction`)
        t.Logf("post url:%v", geturl)

        //var b []byte
        postdata := fmt.Sprintf(`{"cointype":"BCC","data":"0100000002e7ca9a8950b6bb4b4f5bd2a57d85b6f328e818f5c401586a2dfbe6cdac22011b010000008a4730440220141d4bc8c50b93c13e083f00657688c59863d334051412b3bdeb489f0054a10f02204748ce85de1c16cc6a54ecd1a84ec442b50bb3e2e2826f293a34b1c1ad49632a0141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff0c00ee71246819f7d9f5450151ef6bbdf681d00917cae9e6d3d63ff3fe0f97d9000000008b483045022100b8e2f91ead5eaf0b6c7eade504deb15eb22e531c547d34ffc86891ca1eac31a402202c31e5291541c9ec7a5752e404a7f1ccb6dee875b4c63c042a3388dbf74fb3c60141048d4bceb2879580a9d0bcd897c4fe5cfdf490f93bea099785e6680e0c16372b91e124d22b12b40627d2cc098452d7cabbd0f0fb5079fc5c5062ccb4558aec830effffffff02a08f3e00000000001976a914631134f74ce5f456d2256d29d64657cf3fc8a6c388acc05c1500000000001976a914a1de9978896cdf29aa844942d652d5705e3e2c5a88ac00000000","address":""}`)
        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
}
