package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.common.HttpUtil;
import com.lightinit.hsdataplatform.dictionary.DicPaasApi;
import com.lightinit.hsdataplatform.dictionary.DicPublishApiState;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.ResourceApiInterfaceWithBLOBs;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IApiPublishService;
import com.lightinit.hsdataplatform.service.admin.IPaasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.lightinit.hsdataplatform.controller.admin.BaseController.getAllFieldInvalidResultState;

@Controller
@RequestMapping("/admin/api_toExamine")
public class APIToExamineController {

    @Autowired
    private IApiPublishService apiPublishService ;
    @Autowired
    private IPaasService paasService ;

    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/api_toExamine/index");
        modelAndView.addObject("dicPublishApiState", DicPublishApiState.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(APIPublishSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo,
                             HttpSession session) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("admin/api_toExamine/list");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        int isFlag = apiPublishService.isAdminFalg(loginUser.getId()) ;
        if (!(isFlag>0)){
            inputModel.setOrgId(loginUser.getOrgId());
        }
        inputModel.setPublishState(DicPublishApiState.PENDING);
        List<APIPublishListModel> outputModel = apiPublishService.queryList(inputModel);
        pager.setTotal(apiPublishService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    @RequestMapping(value = "toExamine.htm/{id}/{publishInfoId}")
    public ModelAndView toExamine(@PathVariable("id") long id,@PathVariable("publishInfoId") long publishInfoId) throws Exception {
        ModelAndView modelAndView = new ModelAndView("admin/api_toExamine/publishInfo_toExamine");
        modelAndView.addObject("resource", apiPublishService.queryOneViewResource(id)) ;
        List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBsList = apiPublishService.queryOneResourceAPIInterface(id) ;
        modelAndView.addObject("resourceApiInterface", resourceApiInterfaceWithBLOBsList) ;
        modelAndView.addObject("publishInfo", apiPublishService.queryOnePublishInfoById(publishInfoId)) ;
        modelAndView.addObject("publishSettingList", apiPublishService.queryListPublishSettingByInfoId(publishInfoId)) ;
        return modelAndView;
    }

    @RequestMapping(value = "publishSettingView.htm/{id}")
    public ModelAndView publishSettingView(@PathVariable("id") long id){
        ModelAndView modelAndView = new ModelAndView("admin/api_publish/publishSettingView");
        modelAndView.addObject("publishSetting", apiPublishService.queryOnePublishSettingById(id)) ;
        return modelAndView;
    }

    @RequestMapping(value = "toExamine.action")
    @ResponseBody
    public ResultState toExamine(APIToExamineModel inputModel) {
        ResultState<Long> resultState = apiPublishService.toExamine(inputModel) ;
        return resultState ;
    }

    @RequestMapping(value = "publishInfoView.htm/{id}/{publishInfoId}")
    public ModelAndView publishInfoView(@PathVariable("id") long id,@PathVariable("publishInfoId") long publishInfoId){
        ModelAndView modelAndView = new ModelAndView("admin/api_toExamine/publishInfo_view");
        modelAndView.addObject("resource", apiPublishService.queryOneViewResource(id)) ;
        List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBsList = apiPublishService.queryOneResourceAPIInterface(id) ;
        modelAndView.addObject("resourceApiInterface", resourceApiInterfaceWithBLOBsList) ;
        modelAndView.addObject("publishInfo", apiPublishService.queryOnePublishInfoById(publishInfoId)) ;
        modelAndView.addObject("publishSettingList", apiPublishService.queryListPublishSettingByInfoId(publishInfoId)) ;
        return modelAndView;
    }

    //===========================添加路由规则=========================
    @RequestMapping(value = "routeView.htm/{id}/{publishInfoId}")
    public ModelAndView routeView(@PathVariable("id") long id,@PathVariable("publishInfoId") long publishInfoId) throws Exception {
        ModelAndView modelAndView = new ModelAndView("admin/api_toExamine/routeView");
        modelAndView.addObject("resource", apiPublishService.queryOneViewResource(id)) ;
        List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBsList = apiPublishService.queryOneResourceAPIInterface(id) ;
        modelAndView.addObject("resourceApiInterface", resourceApiInterfaceWithBLOBsList) ;
        modelAndView.addObject("publishInfo", apiPublishService.queryOnePublishInfoById(publishInfoId)) ;
        modelAndView.addObject("publishSettingList", apiPublishService.queryListPublishSettingByInfoId(publishInfoId)) ;
        //获取租户
        Map<String,Object> tenants = HttpUtil.strToMap(HttpUtil.httpGet(DicPaasApi.TENANT,null,null)) ;
        modelAndView.addObject("tenant", tenants) ;
        return modelAndView;
    }

    @RequestMapping(value = "routeView.action")
    @ResponseBody
    public ResultState routeViewPost(@Valid RouteAddInputModel inputModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        ResultState<Long> resultState = paasService.toExamine(inputModel) ;
        return resultState ;
    }

    @RequestMapping(value = "outRouteView.htm/{id}/{publishInfoId}/{outId2}")
    public ModelAndView outRouteView(@PathVariable("id") long id,@PathVariable("publishInfoId") long publishInfoId
    ,@PathVariable("outId2") long outId2) throws Exception {
        ModelAndView modelAndView = new ModelAndView("admin/api_toExamine/outRouteView");
        modelAndView.addObject("resource", apiPublishService.queryOneViewResource(id)) ;
        List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBsList = apiPublishService.queryOneResourceAPIInterface(id) ;
        modelAndView.addObject("resourceApiInterface", resourceApiInterfaceWithBLOBsList) ;
        modelAndView.addObject("publishInfo", apiPublishService.queryOnePublishInfoById(publishInfoId)) ;
        modelAndView.addObject("publishSettingList", apiPublishService.queryListPublishSettingByInfoId(publishInfoId)) ;
        modelAndView.addObject("outId2", outId2) ;
        return modelAndView;
    }

    @RequestMapping(value = "outRouteView.action")
    @ResponseBody
    public ResultState outRouteView(OutRouteAddInputModel inputModel) {
        ResultState<Long> resultState = paasService.outRouteView(inputModel) ;
        return resultState ;
    }

}
