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

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.protobuf.InvalidProtocolBufferException;
import com.tzld.piaoquan.recommend.server.client.ProtobufUtils;
import com.tzld.piaoquan.recommend.server.client.RecommendHttpRequest;
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.FeatureService;
import com.tzld.piaoquan.recommend.server.service.VideoRecommendService;
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
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
@Deprecated
public class RecommendController {
    @Autowired
    private VideoRecommendService videoRecommendService;

    @Autowired
    private FeatureService featureService;

    @RequestMapping("/demo")
    public String demo() {
//        return JSONUtils.toJson(featureService.getFeature(Lists.newArrayList("10054148", "20637161"), "0", "1"));
        return "";
    }

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

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

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

    private RecommendRequest generate(RecommendHttpRequest httpRequest) {
        if (httpRequest == null) {
            return null;
        }
        RecommendRequest.Builder builder = RecommendRequest.newBuilder();

        builder.setRequestId(Strings.nullToEmpty(httpRequest.getRequestId()))
                .setMid(Strings.nullToEmpty(httpRequest.getMid()))
                .setUid(Strings.nullToEmpty(httpRequest.getUid()))
                .setSize(httpRequest.getSize())
                .setAppType(httpRequest.getAppType())
                .setCityCode(Strings.nullToEmpty(httpRequest.getCityCode()))
                .setProvinceCode(Strings.nullToEmpty(httpRequest.getProvinceCode()))
                .setVersionAuditStatus(httpRequest.getVersionAuditStatus())
                .setRecommendTraceId(Strings.nullToEmpty(httpRequest.getRecommendTraceId()))
                .setVideoId(httpRequest.getVideoId());

        if (CollectionUtils.isNotEmpty(httpRequest.getAbExpCodes())) {
            builder.addAllAbExpCode(httpRequest.getAbExpCodes());
        }
        if (MapUtils.isNotEmpty(httpRequest.getEventIdMap())) {
            builder.putAllEventId(httpRequest.getEventIdMap());
        }

        return builder.build();
    }
}
