package gd_admin

import (
	"context"
	"fmt"
	"time"

	"github.com/smallnest/rpcx/client"
	"github.com/smallnest/rpcx/protocol"
	"github.com/smallnest/rpcx/share"
)

type GdAdminXClient struct {
	xcli client.XClient
}

func (a *GdAdminXClient) Init(etcdAddrs []string, basePath, servicePath string) {
	opt := client.Option{
		Retries:        1,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.JSON,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}

	d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
	a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)

}

func (a *GdAdminXClient) InitForK8s(servicePath string, k8sServiceName string, k8sServicePort string) {
	opt := client.Option{
		Retries:        1,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.JSON,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}
	sd := client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s:%s", k8sServiceName, k8sServicePort), "")
	a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, sd, opt)
}

func (a *GdAdminXClient) AdminRegister(ctx context.Context, req *AdminRegisterReq) (reply AdminRegisterReply, err error) {
	err = a.xcli.Call(ctx, "AdminRegister", req, &reply)
	return
}

func (a *GdAdminXClient) AdminDelete(ctx context.Context, req *AdminDeleteReq) (reply AdminDeleteReply, err error) {
	err = a.xcli.Call(ctx, "AdminDelete", req, &reply)
	return
}

func (a *GdAdminXClient) AdminModify(ctx context.Context, req *AdminModifyReq) (reply AdminModifyReply, err error) {
	err = a.xcli.Call(ctx, "AdminModify", req, &reply)
	return
}

func (a *GdAdminXClient) AdminGetAll(ctx context.Context, req *AdminGetAllReq) (reply AdminGetAllReply, err error) {
	err = a.xcli.Call(ctx, "AdminGetAll", req, &reply)
	return
}

func (a *GdAdminXClient) AdminLogin(ctx context.Context, req *AdminLoginReq) (reply AdminLoginReply, err error) {
	err = a.xcli.Call(ctx, "AdminLogin", req, &reply)
	return
}

func (a *GdAdminXClient) GetRbacAccess(ctx context.Context, req *GetAccessReq) (reply GetAccessReply, err error) {
	err = a.xcli.Call(ctx, "GetRbacAccess", req, &reply)
	return
}

func (a *GdAdminXClient) GetRbacGroupList(ctx context.Context, req *GetRbacGroupListReq) (reply GetRbacGroupListReply, err error) {
	err = a.xcli.Call(ctx, "GetRbacGroupList", req, &reply)
	return
}

func (a *GdAdminXClient) UpdateRbacGroup(ctx context.Context, req *UpdateRbacGroupReq) (reply UpdateRbacGroupReply, err error) {
	err = a.xcli.Call(ctx, "UpdateRbacGroup", req, &reply)
	return
}

func (a *GdAdminXClient) DeleteRbacGroup(ctx context.Context, req *DeleteRbacGroupReq) (reply DeleteRbacGroupReply, err error) {
	err = a.xcli.Call(ctx, "DeleteRbacGroup", req, &reply)
	return
}

func (a *GdAdminXClient) AddRbacGroup(ctx context.Context, req *AddRbacGroupReq) (reply AddRbacGroupReply, err error) {
	err = a.xcli.Call(ctx, "AddRbacGroup", req, &reply)
	return
}

func (a *GdAdminXClient) GetNodeList(ctx context.Context, req *GetNodeListReq) (reply GetNodeListReply, err error) {
	err = a.xcli.Call(ctx, "GetNodeList", req, &reply)
	return
}

func (a *GdAdminXClient) UpdateAdminStatus(ctx context.Context, req *UpdateStatusReq) (reply UpdateStatusReply, err error) {
	err = a.xcli.Call(ctx, "UpdateAdminStatus", req, &reply)
	return
}

func (a *GdAdminXClient) GetInfo(ctx context.Context, req *GetInfoReq) (reply GetInfoReply, err error) {
	err = a.xcli.Call(ctx, "GetInfo", req, &reply)
	return
}

func (a *GdAdminXClient) AddActionLog(ctx context.Context, req *AddActionLogReq) (reply AddActionLogReply, err error) {
	err = a.xcli.Call(ctx, "AddActionLog", req, &reply)
	return
}

func (a *GdAdminXClient) GetActionLog(ctx context.Context, req *GetLogReq) (reply GetLogReply, err error) {
	err = a.xcli.Call(ctx, "GetActionLog", req, &reply)
	return
}

func (a *GdAdminXClient) AuthGetOSSInfo(ctx context.Context, req *AuthGetOSSInfoReq) (reply AuthGetOSSInfoReply, err error) {
	err = a.xcli.Call(ctx, "AuthGetOSSInfo", req, &reply)
	return
}
