package com.tzld.longarticle.recommend.server.remote;

import com.google.common.reflect.TypeToken;
import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
import com.tzld.longarticle.recommend.server.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author dyp
 */
@Service
@Slf4j
public class NLPRemoteService {

    private final CloseableHttpClient client = HttpPoolFactory.defaultPool();

    private String nlpServerUrl;

    // TODO 如果过滤失败，那么认为所有视频都被过滤掉
    public List<Long> filterViewedVideo(int appType, String mid, String uid, List<Long> videoIds, String cityCode,
                                        Set<String> abExpCodes, Long hotSceneType, String clientIp) {

        CloseableHttpResponse chr = null;
        try {
            HttpPost post = new HttpPost(nlpServerUrl);
            post.addHeader("Connection", "close");
            post.addHeader("content-type", "application/json");
            Map<String, Object> param = new HashMap<>();
            param.put("appType", appType);
            post.setEntity(new StringEntity(JSONUtils.toJson(param)));

            chr = client.execute(post);
            if (chr == null
                    || chr.getStatusLine() == null
                    || chr.getStatusLine().getStatusCode() != 200) {
                log.error("filterViewedVideo failed http status exception!");
                return Collections.emptyList();
            }
            HttpEntity entity = chr.getEntity();
            if (entity == null) {
                return Collections.emptyList();
            }
            String content = EntityUtils.toString(entity);
            Map<String, String> data = JSONUtils.fromJson(content,
                    new TypeToken<Map<String, String>>() {
                    },
                    Collections.emptyMap());
            if (data.get("code") == null
                    || !data.get("code").equals("0")) {
                log.error("filterViewedVideo failed videoFilterUrl return code exception! data={}",
                        JSONUtils.toJson(data));
                return Collections.emptyList();
            }
            return JSONUtils.fromJson(data.get("data"),
                    new TypeToken<List<Long>>() {
                    }, Collections.emptyList());

        } catch (Exception e) {
            log.error("invoke http filterViewedVideo error", e);
        } finally {
            try {
                if (chr != null) {
                    chr.close();
                }
            } catch (Exception ex) {
                log.error("close CloseableHttpResponse error", ex);
            }
        }
        return Collections.emptyList();
    }

}
