package dictionary_admin

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"

	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"

	"net/http"
)

type GetDictionaryListAdminLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

func NewGetDictionaryListAdminLogic(r *http.Request, ctx context.Context, svcCtx *svc.ServiceContext) *GetDictionaryListAdminLogic {
	return &GetDictionaryListAdminLogic{
		r:      r,
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetDictionaryListAdminLogic) GetDictionaryListAdmin(req *types.DictionaryReq) (resp *types.DictionaryListResp, err error) {
	rpcResp, err := l.svcCtx.CoreRpc.GetDictionaryList(l.ctx, &core.DictionaryReq{
		ID:          req.ID,
		Name:        req.Name,
		Title:       req.Title,
		WithDetails: false,
		Page:        l.svcCtx.Convert.ConvertPaginationRpc(req.Pagination),
	})
	if err != nil {
		return nil, err
	}
	resp = &types.DictionaryListResp{
		Page: l.svcCtx.Convert.ConvertPaginationApi(rpcResp.Page),
		List: make([]*types.DictionaryInfo, len(rpcResp.Data)),
	}
	for i, item := range rpcResp.Data {
		resp.List[i] = l.svcCtx.Convert.ConvertDictionary(l.ctx, item)
	}

	return
}
