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

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

func (l *GetDictionaryDetailAdminLogic) GetDictionaryDetailAdmin(req *types.DictionaryDetailReq) (resp *types.DictionaryDetailInfo, 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.GetDictionaryDetail(l.ctx, &core.DictionaryDetailReq{
		ID:             req.ID,
		DictId:         req.DictionaryID,
		Title:          req.Title,
		Key:            req.Key,
		WithDictionary: false,
	})
	if err != nil {
		return nil, err
	}
	resp = l.svcCtx.Convert.ConvertDictionaryDetail(l.ctx, rpcResp)

	return
}
