package cn.stylefeng.team.view.modular.controller;

import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.team.view.api.entity.View;
import cn.stylefeng.team.view.api.pojo.ParserDetail;
import cn.stylefeng.team.view.modular.parser.ParserContainer;
import cn.stylefeng.team.view.modular.pojo.request.ViewRequest;
import cn.stylefeng.team.view.modular.pojo.response.ViewFieldDTO;
import cn.stylefeng.team.view.modular.pojo.response.ViewFieldResult;
import cn.stylefeng.team.view.modular.service.ViewService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@ApiResource(name = "视图配置需要的接口")
public class ViewDetailController {

    @Resource
    private ViewService viewService;

    @GetResource(name = "获取所有转化器列表", path = "/devopsViewDetail/getParserList")
    public ResponseData<Collection<ParserDetail>> getParserList(@RequestParam(name = "searchText", required = false) String searchText) {
        return new SuccessResponseData<>(ParserContainer.queryParser(searchText));
    }

    @GetResource(name = "获取视图设计详情", path = "/devopsViewDetail/getViewConfig")
    public ResponseData<View> getViewConfig(@Validated(ViewRequest.detail.class) ViewRequest viewRequest) {
        return new SuccessResponseData<>(viewService.getViewConfig(viewRequest));
    }

    @PostResource(name = "保存视图设计配置", path = "/devopsViewDetail/saveViewConfig")
    public ResponseData<?> saveViewConfig(@RequestBody @Validated(ViewRequest.saveViewConfig.class) ViewRequest viewRequest) {
        viewService.saveViewConfig(viewRequest);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "获取一键导入的字段列表", path = "/devopsViewDetail/getImportFieldList")
    public ResponseData<ViewFieldDTO> getImportFieldList(@Validated(ViewRequest.detail.class) ViewRequest viewRequest) {
        return new SuccessResponseData<>(viewService.getImportFieldList(viewRequest));
    }

    @GetResource(name = "获取基于表单的字段列表和搜索条件列表", path = "/devopsViewDetail/getFormBasedFieldList")
    public ResponseData<ViewFieldResult> getFormBasedFieldList(@Validated(ViewRequest.formFieldSearch.class) ViewRequest viewRequest) {
        return new SuccessResponseData<>(viewService.getFormBasedFieldList(viewRequest));
    }

}
