package dcrcash

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

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

	fmt.Printf("post url:%v", geturl)

	//var b []byte
	//postdata := fmt.Sprintf(`{"cointype":"BCC","timestamp":"%v"}`, timestamp)
	postdata := fmt.Sprintf(`{"cointype":"DCR"}`)
	request, err := http.NewRequest("POST", geturl, strings.NewReader(postdata))
	if err != nil {
		fmt.Printf("err:%s", err.Error())
		return
	}
	resp, err := client.Do(request)
	if err != nil {
		fmt.Printf("err:%s", err.Error())
		return
	}

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

	fmt.Printf("read bytes:%v", string(rbs))

	return
}*/

func Test_Balance(t *testing.T) {
	client := http.DefaultClient
	geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/coinbalance`)
	//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":"DCR","timestamp":"%v"}`, 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://118.31.213.53:8803/coinproxy/query/transinfo`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"DCR","starth":1,"endh":1243398,"timestamp":"%v"}`, 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://118.31.213.53:8803/coinproxy/blockchain/height`)
	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":"DCR","timestamp":"%v"}`, 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://118.31.213.53:8803/coinproxy/listunspent`)
	//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":"DCR","address":[2],"timestamp":"%v"}`, 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`)
	//geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sendrawtransaction`)
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"DCR","data":"a4db9908e6d01a347d2fe5040592851b0497da93fc5889e737daf5225f1d66c8","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 Test_SumCoinBalance(t *testing.T) {
	client := http.DefaultClient
	//geturl := fmt.Sprintf(`http://localhost:15741/coinproxy/sumcoinbalance`)
	geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sumcoinbalance`)
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"DCR","sumamount":600}`)
	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`)
	geturl := fmt.Sprintf(`http://118.31.213.53:8803/coinproxy/sendcoins`)
	timestamp := fmt.Sprintf("%v", time.Now().Unix())
	t.Logf("post url:%v", geturl)

	//var b []byte
	postdata := fmt.Sprintf(`{"cointype":"DCR","amount":"0.02","address":"DsctbrcDVEF4g4gqTpRF8ciZMqwe4va32uK","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 Signature(body []byte, timestamp string) string {

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