package fabric_sdk

import (
	"fmt"
	"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/client/channel"
	"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/common/errors/retry"
	"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/common/providers/core"
	"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/core/config"
	"github.com/fabric-sdk-sample/fabric-sdk-go/pkg/fabsdk"
)

var (
	channelID = "mychannel"
	orgName = "org1"
	ccID = "mycc"
	defaultQueryArgs = [][]byte{[]byte("b")}
	)
func getConfig()core.ConfigProvider{
	configPath := "./config/config_e2e.yaml"
	return config.FromFile(configPath)
}
func CcQuery()[]byte{
	configOpt := getConfig()
	sdk, err := fabsdk.New(configOpt)
	if err != nil {
		fmt.Printf("Failed to create new SDK: %s \n", err)
	}
	defer sdk.Close()
	//prepare channel client context using client context
	clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser("Admin"), fabsdk.WithOrg(orgName))
	// Channel client is used to query and execute transactions (Org1 is default org)
	client, err := channel.New(clientChannelContext)
	if err != nil {
		fmt.Printf("Failed to create new channel client: %s \n", err)
	}
	return queryCC(client,[]string{"grpcs://39.98.194.244:7051"}...)
}
func queryCC( client *channel.Client, targetEndpoints ...string) []byte {
	response, err := client.Query(channel.Request{ChaincodeID: ccID, Fcn: "query", Args: defaultQueryArgs},
		channel.WithRetry(retry.DefaultChannelOpts),
		channel.WithTargetEndpoints(targetEndpoints...),
	)
	if err != nil {
		fmt.Printf("Failed to query funds: %s \n", err)
	}
	return response.Payload
}
func CcInvoke(){
	configOpt := getConfig()
	sdk, err := fabsdk.New(configOpt)
	if err != nil {
		fmt.Printf("Failed to create new SDK: %s \n", err)
	}
	defer sdk.Close()
}