package detail

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 GetDictionaryDetailListAdminLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

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

func (l *GetDictionaryDetailListAdminLogic) GetDictionaryDetailListAdmin(req *types.DictionaryDetailReq) (resp *types.DictionaryDetailListResp, err error) {
	if req.Keyword != "" {
		if req.Title == "" {
			req.Title = req.Keyword
		} else if req.Key == "" {
			req.Key = req.Keyword
		}
	}
	rpcResp, err := l.svcCtx.CoreRpc.GetDictionaryDetailList(l.ctx, &core.DictionaryDetailReq{
		Page:           l.svcCtx.Convert.ConvertPaginationRpc(req.Pagination),
		ID:             req.ID,
		DictId:         req.DictionaryID,
		Title:          req.Title,
		Key:            req.Key,
		WithDictionary: false,
	})
	if err != nil {
		return nil, err
	}
	resp = &types.DictionaryDetailListResp{
		Page: l.svcCtx.Convert.ConvertPaginationApi(rpcResp.Page),
		List: make([]*types.DictionaryDetailInfo, len(rpcResp.Data)),
	}
	for i, item := range rpcResp.Data {
		resp.List[i] = l.svcCtx.Convert.ConvertDictionaryDetail(l.ctx, item)
	}

	return
}
