package org.diagbot.push.controller;

import com.alibaba.fastjson.JSONObject;
import org.diagbot.common.push.bean.ResponseData;
import org.diagbot.common.push.bean.SearchData;
import org.diagbot.common.push.work.ParamsDataProxy;
import org.diagbot.graph.javabean.GdbResponse;
import org.diagbot.graph.jdbc.DriverManager;
import org.diagbot.graph.jdbc.Neo4jAPI;
import org.diagbot.graphWeb.work.*;
import org.diagbot.pub.api.Response;
import org.diagbot.pub.web.BaseController;
import org.neo4j.driver.v1.Driver;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/graph")
public class GraphController extends BaseController {
    {
        listView = "/pages/graph/list.html";
    }

    @RequestMapping({ "/index" })
    public String index() {
        return listView;
    }

    /**
     * 推送诊断,治疗
     *
     * @param request
     * @param searchData
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/push", method = RequestMethod.POST)
    @ResponseBody
    public Response<ResponseData> bayesPageData(HttpServletRequest request, @RequestBody SearchData searchData) throws Exception {
        Response<ResponseData> response = new Response();
        GraphCalculate graphCalculate = new GraphCalculate();
        ResponseData responseData = graphCalculate.calculate(request, searchData);
        response.setData(responseData);
        return response;
    }

    /**
     * 推送警惕
     *
     * @param request
     * @param searchData
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/highRisk", method = RequestMethod.POST)
    @ResponseBody
    public Response<GdbResponse> getHighRisk(HttpServletRequest request, @RequestBody SearchData searchData) throws Exception {
        HighRiskCalculate highRiskCalculate = new HighRiskCalculate();
        Response<GdbResponse> gdbResponseResponse = highRiskCalculate.calculateHighRisk(request, searchData);
        return gdbResponseResponse;
    }

    /**
     * 推送化验,辅检
     *
     * @param request
     * @param searchData
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/lisPacs", method = RequestMethod.POST)
    @ResponseBody
    public Response<ResponseData> lisPacsData(HttpServletRequest request, @RequestBody SearchData searchData) throws Exception {
        Response<ResponseData> response = new Response();
        LisPacsCalculate lisPacsCalculate = new LisPacsCalculate();
        ResponseData lisPacs = lisPacsCalculate.getLisPacs(request, searchData);
        response.setData(lisPacs);
        return response;
    }

    @RequestMapping(value = "/scale", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> scale(HttpServletRequest request, @RequestBody SearchData searchData) throws Exception {
        ParamsDataProxy paramsDataProxy = new ParamsDataProxy();
        paramsDataProxy.createSearchData(searchData);
        //        Driver driver = DriverManager.newDrive("localhost", "neo4j", "root");
        Driver driver = DriverManager.newDrive();
        Neo4jAPI neo4jAPI = new Neo4jAPI(driver);
        Set<String> inputs = searchData.getInputs().keySet();
        String[] input = inputs.toArray(new String[inputs.size()]);
        Map<String, Object> scale = neo4jAPI.getScale(input, searchData.getScaleName());
        return scale;
    }

    //        //测试用
    //        @RequestMapping(value = "/scaleTest", method = RequestMethod.POST)
    //        @ResponseBody
    //        public Map<String, Object> scaleTest(HttpServletRequest request, @RequestBody SearchData searchData) throws Exception {
    //            Driver driver = DriverManager.newDrive();
    //            Neo4jAPI neo4jAPI = new Neo4jAPI(driver);
    //            Map<String, Object> scale = neo4jAPI.getScaleNoCalc(searchData.getScaleName());
    //            return scale;
    //        }

    @RequestMapping(value = "/scaleCalc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> scaleCalc(HttpServletRequest request, @RequestBody JSONObject jsonObject) throws Exception {
        Map<String, Object> maps = null;
        ScaleCalculate scaleCalculate = new ScaleCalculate();
        if (Integer.parseInt(jsonObject.get("type").toString()) == 1) {
            maps = scaleCalculate.scaleScoreCalc(jsonObject);
        } else {
            maps = scaleCalculate.scaleCalcMethod(jsonObject);
        }
        return maps;
    }

}
