package chain33

import (
	"github.com/33cn/chain33/types"
	"google.golang.org/grpc"
	"context"
	"chain33-pai/pkg/setting"
)

type PaiClient struct {

}

var (
	paiClient types.Chain33Client
	paiNetgrpcAddr = "localhost:8802"
)

func Setup() {
	maxReceLimit := grpc.WithMaxMsgSize(30*1024*1024)
	conn, err := grpc.Dial(setting.BlockSetting.Host, grpc.WithInsecure(),maxReceLimit)
	if err != nil {
		panic(err)
	}
	paiClient = types.NewChain33Client(conn)
}

func (p *PaiClient) GetWalletStatus() (*types.WalletStatus,error) {
	return paiClient.GetWalletStatus(context.Background(),&types.ReqNil{})
}

func (p *PaiClient) GetPeerInfo() (*types.PeerList,error) {
	return paiClient.GetPeerInfo(context.Background(),&types.ReqNil{})
}

func (p *PaiClient) IsNtpClockSync() (*types.Reply,error) {
	return paiClient.IsNtpClockSync(context.Background(),&types.ReqNil{})
}

func (p *PaiClient) GetNetInfo() (*types.NodeNetInfo,error) {
	return paiClient.NetInfo(context.Background(),&types.ReqNil{})
}

func (p *PaiClient) QueryTransaction(hash []byte) (*types.TransactionDetail, error) {
	return paiClient.QueryTransaction(context.Background(),&types.ReqHash{Hash:hash})
}

func (p *PaiClient) GetBlocks(req *types.ReqBlocks) (*types.BlockDetails,error) {
	var detail types.BlockDetails
	reply,err := paiClient.GetBlocks(context.Background(),req)
	if err != nil {
		return nil,err
	}
	err = types.Decode(reply.Msg,&detail)
	if err != nil {
		return nil,err
	}
	return &detail,nil
}

func (p *PaiClient) GetLastHeader() (*types.Header,error) {
	return paiClient.GetLastHeader(context.Background(),&types.ReqNil{})
}