package com.wx.application.adapter.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.wx.application.gorse4j.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wx.application.adapter.dto.qo.GorseQ;
import com.wx.application.base.BaseController;
import com.wx.application.base.ResponseData;
import com.wx.application.core.service.EntrysService;
import com.wx.application.core.service.RiskUserService;
import com.wx.application.nebula.graph.query.NebulaModel;
import com.wx.application.nebula.graph.query.NebulaNode;
import com.wx.application.nebula.graph.service.ImportGraphService;
import com.wx.application.nebula.graph.service.NebulaOperateService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RequestMapping("/gorse")
@RestController("gorseController")
public class GorseController extends BaseController {

    @Autowired
    GorseService gorseService;

    @Autowired
    NebulaOperateService nebulaOperateService;

    @Autowired
    ImportGraphService importGraphService;

    /**
     * 推荐根据userId
     *
     * @param gQo
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/recommend_by_userid")
    public ResponseData recommendByUserId(@RequestBody GorseQ gQo) throws Exception {
        return success(gorseService.getRecommend(gQo.getUserId()));
    }

    /**
     * 根据列表推荐
     *
     * @param gQo
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/popular_by_category")
    public ResponseData popularByCategory(@RequestBody GorseQ gQo) throws Exception {
        List<Item> res = gorseService.getPopular(gQo.getCategory());
        return success(res);
    }

    @PostMapping(value = "/ipt")
    public ResponseData ipt() throws Exception {
        importGraphService.iptUser();
        importGraphService.iptentrys();
        return success();
    }

    @PostMapping(value = "/delete_user/{userId}")
    public ResponseData deleteUser(@PathVariable("userId") String userId) throws Exception {
        return success(gorseService.deleteUser(userId));
    }

    @GetMapping(value = "/get_item/{itemId}")
    public ResponseData getItem(@PathVariable("itemId") String itemId) throws Exception {
        return success(gorseService.getItem(itemId));
    }

    @GetMapping(value = "/get_user/{userId}")
    public ResponseData getUser(@PathVariable("userId") String userId) throws Exception {
        return success(gorseService.getUser(userId));
    }

    @PostMapping(value = "/delete_item/{itemId}")
    public ResponseData deleteItem(@PathVariable("itemId") String itemId) throws Exception {
        return success(gorseService.deleteItem(itemId));
    }

    /**
     * 推荐根据知识图谱
     *
     * @param gQo
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/recommend_by_userid_with_graph")
    public ResponseData recommendByUserIdWithGraph(@RequestBody GorseQ gQo) throws Exception {

        NebulaModel nebulaModel = nebulaOperateService.findOnePathById("", gQo.getUserId());
        List<NebulaNode> nodes = nebulaModel.getNodes();

        if (nodes != null && nodes.size() > 0) {
            nodes.get(0).getVid();
        }

        return success(gorseService.getRecommend(gQo.getUserId()));
    }

    @PostMapping(value = "/get_similar_user")
    public ResponseData getSimilarUser(@RequestBody GorseQ gQo) throws Exception {
        return success(gorseService.getSimilarUser(gQo.getUserId()));
    }

    @PostMapping(value = "/get_similar_item")
    public ResponseData getSimilarItem(@RequestBody GorseQ gQo) throws Exception {
        return success(gorseService.getSimilarItem(gQo.getItemId(), gQo.getCategory(), gQo.getN()));
    }

    @PostMapping(value = "/get_recommend_by_user")
    public ResponseData getRecommendByUser(@RequestBody GorseQ gQo) throws Exception {
        return success(gorseService.getRecommendByUser(gQo.getUserId(), gQo.getRecommendation(), gQo.getCategory(), gQo.getN()));
    }

    @GetMapping(value = "/get_feedback_list/{userId}/{type}")
    public ResponseData getFeedbackList(@PathVariable("userId") String userId, @PathVariable("type") String type) throws Exception {
        return success(gorseService.listFeedback(userId, type));
    }

    @PostMapping(value = "/insert_feedback/{type}/{userId}/{itemId}")
    public ResponseData insertFeedback(@PathVariable("userId") String userId, @PathVariable("type") String type,
                                       @PathVariable("itemId") String itemId) throws Exception {
        List<Feedback> list = new ArrayList<>();
        Feedback fb = new Feedback();
        fb.init(userId, itemId, type);
        list.add(fb);
        return success(gorseService.insertFeedback(list));
    }

    @PutMapping(value = "/bulk/{type}")
    public ResponseData bulkUserOrItem(@RequestParam Map<String, String> data, @RequestParam("file") MultipartFile file, @PathVariable("type") String type) throws Exception {
        return success(gorseService.bulkUserOrItem(data, file, type));
    }

    @GetMapping(value = "/get_bulk/{type}")
    public String getBulkUserOrItem(@PathVariable("type") String type) {
        return gorseService.getBulkUserOrItem(type);
    }

    @GetMapping(value = "/query_users")
    public ResponseData queryUsers(@RequestParam("cursor") String cursor) throws Exception {
        return success(gorseService.getUserPageByCursor(cursor));
    }

    @GetMapping(value = "/query_user/{id}")
    public ResponseData queryUser(@PathVariable("id") String id) throws Exception {
        return success(gorseService.getUserById(id));
    }

    @GetMapping(value = "/query_items")
    public ResponseData queryItems(@RequestParam("cursor") String cursor) throws Exception {
        return success(gorseService.getItemPageByCursor(cursor));
    }

    @GetMapping(value = "/query_item/{id}")
    public ResponseData queryItem(@PathVariable("id") String id) throws Exception {
        return success(gorseService.getItemById(id));
    }


}
