package commonapi

import (
	"fmt"
)

type Template interface {
	InitDriver(parm interface{}) bool
	QueryByTransId(parm interface{}) ([]byte, error)
	QueryByBlockId(parm interface{}) ([]byte, error)
	QueryByAddress(parm interface{}) ([]byte, error)
	QueryBlockInfo(parm interface{}) ([]byte, error)
	QueryBlockHeight(parm interface{}) ([]byte, error)
	QueryTransInfo(parm interface{}) ([]byte, error)
	//	AddrTransInfo(parm interface{}) ([]byte, error)
}

const (
	QUERYTRANSID     = "/v1/transbank/transid"
	QUERYBLOCKID     = "/v1/transbank/blockid"
	QUERYADDRESS     = "/v1/transbank/address"
	QUERYBLOCKINFO   = "/v1/transbank/blockinfo"
	QUERYBLOCKHEIGHT = "/v1/transbank/blockheight"
	QUERYTRANSINFO   = "/v1/transbank/transinfo"
	//ADDRTRANSINFO    = "/v1/transbank/address/alltransinfo"
)

var Handler = map[string]func(Template, interface{}) ([]byte, error){
	QUERYTRANSID:     Template.QueryByTransId,
	QUERYBLOCKID:     Template.QueryByBlockId,
	QUERYADDRESS:     Template.QueryByAddress,
	QUERYBLOCKINFO:   Template.QueryBlockInfo,
	QUERYBLOCKHEIGHT: Template.QueryBlockHeight,
	QUERYTRANSINFO:   Template.QueryTransInfo, //income info
	//ADDRTRANSINFO:    Template.AddrTransInfo,
}

var drivers = make(map[string]Template)

func Register(name string, driver Template) {
	if driver == nil {
		panic("crypto: Register driver is nil")
	}
	if _, dup := drivers[name]; dup {
		fmt.Println("crypto: Register called twice for driver " + name)
		return
	}
	drivers[name] = driver
}

func New(name string) (c Template, err error) {
	c, ok := drivers[name]
	if !ok {
		err = fmt.Errorf("unknown driver %q", name)
		return
	}
	return c, nil
}
