package gotest

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
	"time"
	//	"encoding/json"
	//"zcash"/*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":"ZEC","timestamp":"%v","signature":"%v"}`, timestamp, Signature([]byte ("/coinproxy/createaddr"), 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_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":"ZEC","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":"ZEC","starth":10,"endh":100,"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":"ZEC","amount":"0.00005","address":"t1fY6WoALcAp8EfNTYbdyHYuaWNW4QcDz7H","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":"ZEC","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 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
}

//local test is ok,not support remote test by docker deploy so 
/*

func Test_WalletGetAccountAddress(t *testing.T) {

	var zcashapi zcash.ZcashApi
	addr, err := zcashapi.WalletGetAccountAddress("")
	if err == nil {
		t.Logf("read WalletGetAccountAddress bytes:%s", addr)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalletAddrList(t *testing.T) {

	var zcashapi zcash.ZcashApi
	addr, err := zcashapi.WalletAddrList("")
	if err == nil {
		t.Logf("read WalletAddrList bytes:%s", addr)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WallectInfo(t *testing.T) {

	var zcashapi zcash.ZcashApi
	addr, err := zcashapi.WallectInfo()
	if err == nil {
		t.Logf("read WallectInfo bytes:%s", addr)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalleExportWalletAddress(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WalleExportWalletAddress("fuzameiwallet")
	if err == nil {
		t.Logf("read WalleExportWalletAddress bytes:%s", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}
func Test_WallectGetNewZaddress(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WallectGetNewZaddress()
	if err == nil {
		t.Logf("read WallectGetNewZaddress bytes:%s", filedir)
		zkey, err := zcashapi.WallectExportZkey(filedir)
	        if err == nil {
        	        t.Logf("read WallectExportZkey bytes:%s", zkey)
        	}
		if err != nil {
                	t.Errorf("err:%s", err.Error())
                	return
        	}

	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}


func Test_WallectListZaddresses(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WallectListZaddresses()
	if err == nil {
		t.Logf("read WallectListZaddresses bytes:%s", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalleGetReceivedByAccount(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WalleGetReceivedByAccount("", 1)
	if err == nil {
		t.Logf("read WalleGetReceivedByAccount bytes:%.8f", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalleGetReceivedByAddress(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WalleGetReceivedByAddress("t1T8ei5znEsKhXmembrM8iQqB55GcUt6GRK", 1)
	if err == nil {
		t.Logf("read WalleGetReceivedByAddress bytes:%.8f", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalleGetbalanceByaddress(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WalleGetbalanceByaddress("t1T8ei5znEsKhXmembrM8iQqB55GcUt6GRK", 1)
	if err == nil {
		t.Logf("read WalleGetbalanceByaddress bytes:%.8f", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalleGetTotalBalance(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WalleGetTotalBalance(1)
	if err == nil {
		t.Logf("read WalleGetTotalBalance bytes:%s", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalletListReceivedByZaddress(t *testing.T) {

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WallezListReceivedByZaddress("zcdAm8ftZiDJ8G6iBfDPfeaWwh7JNcrkERFEQv1VXK7F5hwkH3K9vex9TLkxJzskqLcYKd2NPs4TeutpSk4VUPjB8wmbfUD", 1)
	if err == nil {
		t.Logf("read WallezListReceivedByZaddress bytes:%s", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}

func Test_WalletSetTxFee(t *testing.T) {

        var zcashapi zcash.ZcashApi
        filedir, err := zcashapi.WalletSetTxFee(float64(0))
        if err == nil {
                t.Logf("read WalletSetTxFee bytes:%v", filedir)
        }
        if err != nil {
                t.Errorf("err:%s", err.Error())
                return
        }
        return
}


func Test_WalletZ_SendFrom(t *testing.T) {

	//sendmanyarry:= []zcash.ZcashAmountsToSend{{"t1Hy5oLGmdTLXFCqB4WMKbNacmbNuBL7q33",0.1,""}} 
	//sendmanyarry := [{"address":"t1Hy5oLGmdTLXFCqB4WMKbNacmbNuBL7q33","amount":0.1,"memo":""},{"address":"t1RwAwki1n4z1i7M9oJhj9zTszTBYt6t1Lk","amount":0.2,"memo":""}] 
	//sendmanyarry[0].address = "t1Hy5oLGmdTLXFCqB4WMKbNacmbNuBL7q33"
	//sendmanyarry[0].amount = 0.1
	//sendmanyarry[0].memo = ""
	//sendmanyarry[1].address = "t1RwAwki1n4z1i7M9oJhj9zTszTBYt6t1Lk"
	//sendmanyarry[1].amount = 0.3
	//sendmanyarry[1].memo = ""

	var zcashapi zcash.ZcashApi
	filedir, err := zcashapi.WalletZ_Send("t1T8ei5znEsKhXmembrM8iQqB55GcUt6GRK","zcfUAY3nq5bg2aqdj8FPf3KT5rFkTyhCLiHVvBnmVJ72ZpQwgufdqNHrBAaf8KpJzaNHf7NmtktUhBLRnqoP69sVvkjGUQg",0.000001,1,0.00000000)
	if err == nil {
		t.Logf("read WalletSetTxFee bytes:%s", filedir)
	}
	if err != nil {
		t.Errorf("err:%s", err.Error())
		return
	}
	return
}
*/

