package com.zhiqiyun.open.mvc.manager.controller;

import com.dliyun.oap.framework.service.RouterService;
import com.dliyun.oap.framework.service.ServiceMethodHandler;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import com.zhiqiyun.open.mvc.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {
    @Autowired
    private RouterService routerService;

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    @GetMapping("/")
    public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/index.html");
    }

    @ResponseBody
    @PostMapping("/dashboard")
    public Result dashboard() throws IOException {
        Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
        int serviceMethodHandlerCount = serviceMethodHandlerMap.size();

        long appKeyCount = this.appKeyInfoService.count();

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("serviceMethodHandlerCount", serviceMethodHandlerCount);
        dataMap.put("appKeyCount", appKeyCount);

        return Result.instance(Result.Code.SUCCESS).setData(dataMap);
    }
}
