package vm

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/helper/xerr"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"gitlink.org.cn/JointCloud/pcm-openstack/openstack"

	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetNetworkNumLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetNetworkNumLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetNetworkNumLogic {
	return &GetNetworkNumLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetNetworkNumLogic) GetNetworkNum(req *types.ListNetworksReq) (resp *types.NetworkNum, err error) {
	// todo: add your logic here and delete this line
	resp = &types.NetworkNum{}
	ListNetworksReq := &openstack.ListNetworksReq{}
	err = copier.CopyWithOption(ListNetworksReq, req, copier.Option{Converters: utils.Converters})
	ListNetworksResp, err := l.svcCtx.OpenstackRpc.ListNetworks(l.ctx, ListNetworksReq)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrMsg("Failed to get Networks list"), "Failed to get db Servers list err : %v ,req:%+v", err, req)
	}
	var count int = len(ListNetworksResp.Networks)
	resp.NetworkNum = int32(count)
	if err != nil {
		return nil, result.NewDefaultError(err.Error())
	}
	return resp, err
}
