package cn.fateverse.query.portal;

import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.security.utils.ResponseRender;
import cn.fateverse.query.constant.QueryConstant;
import cn.fateverse.query.entity.DataAdapter;
import cn.fateverse.query.entity.Portal;
import cn.fateverse.query.entity.PortalMapping;
import cn.fateverse.query.entity.bo.PortalBo;
import cn.fateverse.query.entity.dto.SearchInfo;
import cn.fateverse.query.entity.dto.UniConDto;
import cn.fateverse.query.handler.reader.DataAdapterHandlerReader;
import cn.fateverse.query.mapper.DataAdapterMapper;
import cn.fateverse.query.mapper.PortalMapper;
import cn.fateverse.query.mapper.PortalMappingMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Clay
 * @date 2024/4/18 21:42
 */
@Component
public class PortalDispatchServlet {

    @Resource
    private RedisTemplate<String, PortalBo> redisTemplate;

    private final PortalMapper portalMapper;

    private final DataAdapterMapper dataAdapterMapper;

    private final PortalMappingMapper portalMappingMapper;

    private final DataAdapterHandlerReader dataAdapterHandler;


    public PortalDispatchServlet(PortalMapper portalMapper,
                                 DataAdapterMapper dataAdapterMapper,
                                 PortalMappingMapper portalMappingMapper,
                                 DataAdapterHandlerReader dataAdapterHandler) {
        this.portalMapper = portalMapper;
        this.dataAdapterMapper = dataAdapterMapper;
        this.portalMappingMapper = portalMappingMapper;
        this.dataAdapterHandler = dataAdapterHandler;
    }

    private void doDispatch(HttpServletRequest request, HttpServletResponse response) {
        //获取到请求路径
        String path = request.getServletPath();
        //获取请求方法
        String method = request.getMethod();
        //从Redis中获取到当前的方法信息
        PortalBo portalBo = redisTemplate.opsForValue().get(QueryConstant.PORTAL_KEY + path + ":" + method);
        //判断是否为空,如果为空则从数据库中查找
        if (portalBo == null) {
            Portal portal = portalMapper.selectByPath(path, method);
            if (portal == null) {
                response.setStatus(HttpStatus.NOT_FOUND.value());
                return;
            }
            List<PortalMapping> portalMappings = portalMappingMapper.selectByPortalId(portal.getPortalId());
            portalBo = PortalBo.toPortalBo(portal, portalMappings);
            redisTemplate.opsForValue().set(QueryConstant.PORTAL_KEY + portalBo.getPath() + ":" + portalBo.getRequestMethod(), portalBo);
        }
        //进行数据适配器的执行逻辑
        Object result = null;
//        if (portalBo.getState() == 1 || portalBo.getState() == 2) {
        result = dataAdapterHandler.execute(portalBo, request);
//        } else {
//            result = dataAdapterHandler.mockExecute(portalBo, request);
//        }
        //将返回结果放入response
        ResponseRender.renderString(response, Result.ok(result));
    }


}
