package com.tzld.piaoquan.recommend.server.web;

import com.google.protobuf.InvalidProtocolBufferException;
import com.tzld.piaoquan.recommend.server.client.ProtobufUtils;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendResponse;
import com.tzld.piaoquan.recommend.server.service.VideoRecommendService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dyp
 */
@RestController
@Slf4j
public class RecommendV2Controller {
    @Autowired
    private VideoRecommendService videoRecommendService;
//
//    @Resource
//    TopRecommendPipeline topRecommendPipeline;
//
//    @Resource
//    FlowPoolRecommendPipeline flowPoolRecommendPipeline;

    @RequestMapping("/homepage/recommend/v2")
    public String homepageRecommend(@RequestBody RecommendRequest httpRequest) {
        MDC.put("appType", String.valueOf(httpRequest.getAppType()));
        RecommendResponse response = videoRecommendService.homepageRecommend(httpRequest);
        String result = "";
        try {
            result = ProtobufUtils.toJson(response);
        } catch (InvalidProtocolBufferException e) {
            log.error("homepageRecommend ProtobufUtils.toJson", e);
        }
        return result;
    }

    @RequestMapping("/relevant/recommend/v2")
    public String relevantRecommend(@RequestBody RecommendRequest httpRequest) {
        MDC.put("appType", String.valueOf(httpRequest.getAppType()));

        RecommendResponse response = videoRecommendService.relevantRecommend(httpRequest);
        String result = "";
        try {
            result = ProtobufUtils.toJson(response);
        } catch (InvalidProtocolBufferException e) {
            log.error("relevantRecommend ProtobufUtils.toJson", e);
        }
        return result;
    }

//    @RequestMapping("/test/feedByRec")
//    public String feedByRec(@RequestBody RecommendRequest httpRequest) {
//        MDC.put("appType", String.valueOf(httpRequest.getAppType()));
//
//        User user = new User();
//        user.setRegion(httpRequest.getCityCode());
//        List<Video> videoList = topRecommendPipeline.feeds(httpRequest, 0, user, true);
//        String result = "";
//        try {
//            result = JSONObject.toJSONString(videoList);
//        } catch (Exception e) {
//            log.error("feedByRec ProtobufUtils.toJson", e);
//        }
//        return result;
//    }
//
//    @RequestMapping("/test/flowPool")
//    public String flowPool(@RequestBody RecommendRequest httpRequest) {
//        MDC.put("appType", String.valueOf(httpRequest.getAppType()));
//
//        List<Video> videoList = flowPoolRecommendPipeline.feedByRec(httpRequest, 0, true);
//        String result = "";
//        try {
//            result = JSONObject.toJSONString(videoList);
//        } catch (Exception e) {
//            log.error("flowPool ProtobufUtils.toJson", e);
//        }
//        return result;
//    }
}
