package bitcoin

import (
	"encoding/json"
	//"errors"
	"fmt"
	"strconv"
	. "template"
	"time"

	simplejson "github.com/go-simplejson"
	tml "github.com/toml-master"
	btc "github.com/piotrnar/gocoin/lib/btc"
)

var (
	Config  config
	CoinApi BitCoinApi
)

type BitCoin struct {
	walletstatus bool
}

const (
	NameCoin = "BTC"
)

const BACKUPPUBKEYINDEX = "backuppubkeyindex"

// PubKey
var PubKey string = ""


func (m BitCoin) InitDriver(parm interface{}) bool {
	if m.initconfig() == false {
		return false
	}
	go m.getwalletinfo()
	return true
}

//get the server's total available balance
func (m BitCoin) CoinBalance(parm interface{}) ([]byte, error) {

	confirmedBalance, err := CoinApi.WalleGetBalance("*")
	if err != nil {
		LOG("ERROR", " GetBalance err:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
	}

	UnconfirmedBalance, err := CoinApi.WalleGetUnconfirmedBalance("")
	if err != nil {
		LOG("ERROR", " GetUnconfirmedBalance err:%s", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETUNCONFIRMEDBALANCE_ERR, getErrMsg(GETUNCONFIRMEDBALANCE_ERR, nil))
	}

	var jbuf []byte
	resp := make(map[string]interface{})
	resp["confirmedbalance"] = confirmedBalance
	resp["unconfirmed"] = UnconfirmedBalance
	resp["errcode"] = 0
	resp["exact"] = 0

	if jbuf, err = json.Marshal(resp); err != nil {
		LOG("ERROR", " inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}

	return jbuf, nil
}

//Returns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'.
func (m BitCoin) TransInfo(parm interface{}) ([]byte, error) {

	pjs := parm.(*simplejson.Json)
	startH := pjs.Get("starth").MustUint64()
	endH := pjs.Get("endh").MustUint64()

	if startH < 1 {
		LOG("DEBUG", " input para  err:%v", startH)
		startH = 1
	}

	//get longest block count
	blockcount, err := CoinApi.WalleGetBlockCount()
	if err != nil {
		LOG("ERROR", " WalleGetBlockCount  err:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil))
	}

	//get startblock hash by height

	if startH > blockcount {
		LOG("DEBUG", " startheight:%v >  blockcount:%v", startH, blockcount)
		startH = blockcount
	}

	startblockhash, err := CoinApi.WalletGetBlockHash(uint64(startH - 1))
	if err != nil {
		LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
	}

	//get endblock time by height
	if endH > blockcount {
		LOG("DEBUG", " endheight:%v >  blockcount:%v", endH, blockcount)
		endH = blockcount
	}

	endblockhash, err := CoinApi.WalletGetBlockHash(uint64(endH))
	if err != nil {
		LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
	}

	endblocktime, err := CoinApi.WalletGetBlockTime(endblockhash)
	if err != nil {
		LOG("ERROR", " WalletGetBlockTime endH:%v err:%v", endH, err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
	}

	//Get all transactions in blocks since block [blockhash]
	resp, err := CoinApi.WalleListSinceBlock(startblockhash)
	if err != nil {
		LOG("ERROR", " WalleListSinceBlock err:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, TRANSINFO_ERR, getErrMsg(TRANSINFO_ERR, nil))
	}

	var litelisttransactionsinfo []Listtransactions

	if err = json.Unmarshal([]byte(resp), &litelisttransactionsinfo); err != nil {
		LOG("ERROR", " inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}

	outputsArr := make([]map[string]interface{}, 0)

	for _, listtransaction := range litelisttransactionsinfo {

		// get all transactions from block startHright to endHeight
		if listtransaction.Blocktime <= endblocktime {
			requestData := make(map[string]interface{})
			if listtransaction.Amount < 0 {
				LOG("DEBUG", "listtransaction.Address:%v,value:%v,txid:%v",listtransaction.Address,listtransaction.Amount,listtransaction.Txid)
				continue
			}
			confirmationheight, err := CoinApi.WalletGetBlockHeight(listtransaction.Blockhash)
			if err != nil {
				LOG("ERROR", "WalletGetBlockHeight error:%v", err.Error())
				continue
			}
			requestData["confirmationheight"] = confirmationheight
			requestData["address"] = listtransaction.Address
			requestData["confirmationtimestamp"] = listtransaction.Timereceived
			requestData["value"] = listtransaction.Amount
			requestData["transactionid"] = listtransaction.Txid
			requestData["confirmations"] = listtransaction.Confirmations
			requestData["walletaddress"] = true
			requestData["vout"] = listtransaction.Vout
			outputsArr = append(outputsArr, requestData)
		}
	}

	var jbuf []byte
	resptmp := make(map[string]interface{})
	resptmp["errcode"] = 0
	resptmp["cointype"] = "BTC"
	resptmp["outputs"] = outputsArr

	if jbuf, err = json.Marshal(resptmp); err != nil {
		LOG("ERROR", " inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	return jbuf, nil

}

//Sent an amount from an account to a lite address.
func (m BitCoin) TransferAccounts(parm interface{}) ([]byte, error) {

	pjs := parm.(*simplejson.Json)
	amountTmp := pjs.Get("amount").MustString()
	destination := pjs.Get("address").MustString()

	if len(amountTmp) == 0 {
		LOG("ERROR", " amount illeagal")
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, AMOUNT_ERR, getErrMsg(AMOUNT_ERR, nil))
	}
	f, err := strconv.ParseFloat(amountTmp, 64)
	if err != nil {
		LOG("ERROR", " inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}

	amount := float64(f)

	transactionid, err := CoinApi.WalleSendFrom("", destination, amount, 1, "", "")
	if err != nil {
		LOG("ERROR", " Send coin error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
	}

	if len(transactionid) != 0 {
		var jbuf []byte
		resp := make(map[string]interface{})
		resp["transcationids"] = []string{transactionid}
		resp["errcode"] = 0
		resp["msg"] = "ok"

		if jbuf, err = json.Marshal(resp); err != nil {
			LOG("ERROR", " inner json error:%v", err.Error())
			return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
		}
		return jbuf, nil
	}
	LOG("ERROR", " transactionid is null error:%v", err.Error())
	return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
}

//create a new  address&key for receiving payments
func (m BitCoin) CreateAddress(parm interface{}) ([]byte, error) {
	// Create Address From HdWallet
	result, err := m.CreateAddressFromHdWallet()
	return result, err
}


//Create Address From HdWallet  address&key for receiving payments
func (m BitCoin) CreateAddressFromHdWallet() ([]byte, error) {

	if len(PubKey) == 0 {
		LOG("ERROR", " CreateAddressFromHdWallet PubKey is null!")
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}

	var backupindex uint32
	//get BACKUPPUBKEYINDEX from bitcoindb
	backuppubkeyindex, err := bitcoindb.Get([]byte(BACKUPPUBKEYINDEX))
	if err != nil {
		LOG("ERROR", " bitcoindb.Get erroe!")
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	if len(string(backuppubkeyindex)) == 0 {
		backupindex = 0
	} else {
		fmt.Println("litedb.Get BACKUPPUBKEYINDEX:", string(backuppubkeyindex))

		if err := json.Unmarshal([]byte(backuppubkeyindex), &backupindex); err != nil {
			fmt.Println("Unmarshal err :", err.Error())
			return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
		}
	}
	index := backupindex + 1

	pubkey := btc.StringChild(PubKey, index)
	addr, err := btc.StringAddress(pubkey)
	if err != nil {
		LOG("ERROR", " StringAddress error!Pub:%v,index:%v", PubKey, index)
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	var jbuf []byte
	resp := make(map[string]interface{})
	resp["address"] = addr
	resp["privkey"] = ""
	resp["errcode"] = 0

	if jbuf, err = json.Marshal(resp); err != nil {
		LOG("ERROR", " inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	// back up index in db
	var pubkeyindex []byte
	if pubkeyindex, err = json.Marshal(index); err != nil {
		fmt.Println("Marshal err :", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	bitcoindb.Put([]byte(BACKUPPUBKEYINDEX), pubkeyindex)

	//Import the Address in Wallet watche only cannot be used to spend
	_,err = CoinApi.WalletImportAddress(addr, false)
	if err != nil{
		LOG("ERROR", "WalletImportAddress:%v, error:%v",addr, err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	return jbuf, nil

}


//create a new  address&key for receiving payments
func (m BitCoin) CreateAddressFromWallet() ([]byte, error) {

	addr, err := CoinApi.WalletNewAddr()
	if err != nil {
		LOG("ERROR", "  get new address error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETNEWADDRESS_ERR, getErrMsg(GETNEWADDRESS_ERR, nil))
	}
	if len(addr) != 0 {
		//get key by the address
		key, err := CoinApi.WalletDumpPrivKey(addr)

		if err != nil {
			LOG("ERROR", " dump private key error:%v", err.Error())
			return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, DUMPPRIVKEY_ERR, getErrMsg(DUMPPRIVKEY_ERR, nil))
		}
		if len(key) != 0 {

			var jbuf []byte
			resp := make(map[string]interface{})
			resp["address"] = addr
			resp["privkey"] = key
			resp["errcode"] = 0

			if jbuf, err = json.Marshal(resp); err != nil {
				LOG("ERROR", " inner json error:%v", err.Error())
				return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
			}
			return jbuf, nil
		}
		LOG("ERROR", " privkey is null error:%v")
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	LOG("ERROR", " address is null error:%v")
	return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
}

func (m BitCoin) getwalletinfo() bool {
	fmt.Println("WalletInfo:")
	m.walletstatus = false
	for {
		if m.walletstatus == false {
			resp, err := CoinApi.WalletInfo()
			if err != nil {
				LOG("ERROR", "WalletInfo error:%v", err.Error())
				time.Sleep(time.Second * 1)
				continue
			}

			LOG("DEBUG", "WalletInfo resp:%v", string(resp))
			m.walletstatus = true
		}

		time.Sleep(time.Second)
	}
}

//config rpc from lite.toml
func (m BitCoin) initconfig() bool {
	if _, err := tml.DecodeFile("./src/bitcoin/bitcoin.toml", &Config); err != nil {
		fmt.Println(err)
		return false
	}

	fmt.Printf("%+v\n", Config)

	User := Config.Rpc.Rpcuser
	Password := Config.Rpc.Rpcpassword
	Host := Config.Rpc.Rpchost
	Port := Config.Rpc.Rpcport

	CoinApi.InitRpcConf(User, Password, Host, Port)

	initLiteErr()

	//init bitcoin db
	bitcoindb.InitGoleveldb()
	PubKey = Config.Pubkey.Pubkeystr
	LOG("DEBUG", "PubKey:%v", PubKey)

	f, err := strconv.ParseFloat(Config.Limit.Fee, 64)
	if err != nil {
		LOG("ERROR", "inner json error:%v", err.Error())
		return false
	}

	fee := float64(f)
	CoinApi.WalletSetTxFee(fee)
	return true
}

func (m BitCoin) ChainHeight(parm interface{}) ([]byte, error) {

	blockcount, err := CoinApi.WalleGetBlockCount()
	if err != nil {
		LOG("ERROR", "  get current block count error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil))
	}

	sendstr := fmt.Sprintf(`{"errcode":%v,"height":%v}`, OK, blockcount)
	return []byte(sendstr), nil
}


func (m BitCoin) ListUnspent(parm interface{}) ([]byte, error) {
	pjs := parm.(*simplejson.Json)
	address,err := pjs.Get("address").StringArray()
	if err != nil{
		LOG("ERROR", "  input parm error:%v", err.Error())
                return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
	}

	unspent, err := CoinApi.Listunspent(address)
	if err != nil {
		LOG("ERROR", "  get listunspent:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
	}

	var jbuf []byte
	resptmp := make(map[string]interface{})
	resptmp["errcode"] = 0
	resptmp["cointype"] = "BTC"
	resptmp["unspents"] = unspent

	if jbuf, err = json.Marshal(resptmp); err != nil {
		LOG("ERROR", " inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	return jbuf, nil
}

func (m BitCoin) SendRawTransaction(parm interface{}) ([]byte, error) {

	pjs := parm.(*simplejson.Json)
	transdata := pjs.Get("data").MustString()

	if len(transdata) == 0 {
		return nil, fmt.Errorf(`{"cointype":"BTC","errcode":%v,"msg":"%v"}`, PARM_ERR, getErrMsg(PARM_ERR, []byte("hexstring is null")))
	}
	transactionhex, err := CoinApi.SendRawTransaction(transdata)
	if err != nil {
		LOG("ERROR", " SendRawTransaction err:%s", err.Error())
		return nil, fmt.Errorf(`{"cointype":"BTC","errcode":%v,"msg":"%s"}`, SEND_RAW_TRANSACTION_ERR, getErrMsg(SEND_RAW_TRANSACTION_ERR, nil))
	}

	var jbuf []byte
	resp := make(map[string]interface{})
	resp["cointype"] = "BTC"
	resp["transactionids"] = transactionhex
	resp["errcode"] = 0
	resp["msg"] = "OK"

	if jbuf, err = json.Marshal(resp); err != nil {
		LOG("ERROR", "inner json error:%v", err.Error())
		return nil, fmt.Errorf(`{"cointype":"BTC","errcode":%v,"msg":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
	}
	return jbuf, nil
}


func init() {
	Register(NameCoin, &BitCoin{})
}
