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

import com.tzld.piaoquan.recommend.feature.client.FeatureClient;
import com.tzld.piaoquan.recommend.feature.domain.video.base.ItemFeature;
import com.tzld.piaoquan.recommend.feature.domain.video.base.UserActionFeature;
import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeature;
import com.tzld.piaoquan.recommend.feature.model.feature.UserActionFeatureProto;
import com.tzld.piaoquan.recommend.feature.model.feature.UserFeatureProto;
import com.tzld.piaoquan.recommend.feature.model.feature.VideoFeatureProto;
import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * @author dyp
 */
@Component
@Slf4j
public class FeatureRemoteService {

    @Autowired
    private FeatureClient client;

    // 有性能问题再增加localCache
    public UserFeature getUserFeature(String mid) {
        if (StringUtils.isBlank(mid)) {
            return null;
        }
        UserFeatureProto proto = client.getUserFeature(mid);
        return convert(proto);
    }

    private UserFeature convert(UserFeatureProto proto) {
        if (proto == null) {
            return null;
        }
        UserFeature feature = new UserFeature();
        feature.setUid(proto.getUid());
        feature.setMid(proto.getMid());
        if (proto.hasDay1CntFeature()) {
            feature.setDay1_cnt_features(convert(proto.getDay1CntFeature()));
        } else {
            feature.setDay1_cnt_features(new UserActionFeature());
        }
        if (proto.hasDay3CntFeature()) {
            feature.setDay3_cnt_features(convert(proto.getDay3CntFeature()));
        } else {
            feature.setDay3_cnt_features(new UserActionFeature());
        }
        if (proto.hasDay7CntFeature()) {
            feature.setDay7_cnt_features(convert(proto.getDay7CntFeature()));
        } else {
            feature.setDay7_cnt_features(new UserActionFeature());
        }
        if (proto.hasMonth3CntFeature()) {
            feature.setMonth3_cnt_features(convert(proto.getMonth3CntFeature()));
        } else {
            feature.setMonth3_cnt_features(new UserActionFeature());
        }
        feature.setUser_cycle_bucket_7days(proto.getUserCycleBucket7Day());
        feature.setUser_cycle_bucket_30days(proto.getUserCycleBucket30Day());
        feature.setUser_share_bucket_30days(proto.getUserShareBucket30Day());

        return feature;
    }

    private UserActionFeature convert(UserActionFeatureProto proto) {
        UserActionFeature feature = new UserActionFeature();
        feature.setClick_cnt(proto.getClickCnt());
        feature.setCtr(proto.getCtr());
        feature.setExp_cnt(proto.getExpCnt());
        feature.setReturn_cnt(proto.getReturnCnt());
        feature.setRov(proto.getRov());
        feature.setShare_cnt(proto.getShareCnt());
        feature.setStr(proto.getStr());
        return feature;
    }

    public ItemFeature getVideoFeature(Long videoId) {
        if (videoId == null) {
            return null;
        }
        VideoFeatureProto proto = client.getVideoFeature(videoId);
        return convert(proto);
    }

    public Map<Long, ItemFeature> getVideoFeatureMap(List<Long> videoIds) {
        if (CollectionUtils.isEmpty(videoIds)) {
            return Collections.emptyMap();
        }
        List<VideoFeatureProto> protoList = client.getAllVideoFeature(videoIds);
        return CommonCollectionUtils.toMap(protoList, p -> NumberUtils.toLong(p.getVideoId(), 0L), this::convert);
    }


    private ItemFeature convert(VideoFeatureProto proto) {
        if (proto == null) {
            return null;
        }
        ItemFeature feature = new ItemFeature();

        feature.setPlayLength(proto.getPlayLength());
        feature.setTags(proto.getTags());
        feature.setTotalTime(proto.getTotalTime());
        feature.setUpId(proto.getUpId());
        feature.setVideoId(proto.getVideoId());
        feature.setDaysSinceUpload(proto.getDaysSinceUpload());


        if (proto.hasVideoDay1CntFeature()) {
            feature.setDay1_cnt_features(convert(proto.getVideoDay1CntFeature()));
        } else {
            feature.setDay1_cnt_features(new UserActionFeature());
        }
        if (proto.hasVideoDay3CntFeature()) {
            feature.setDay3_cnt_features(convert(proto.getVideoDay3CntFeature()));
        } else {
            feature.setDay3_cnt_features(new UserActionFeature());
        }
        if (proto.hasVideoDay7CntFeature()) {
            feature.setDay7_cnt_features(convert(proto.getVideoDay7CntFeature()));
        } else {
            feature.setDay7_cnt_features(new UserActionFeature());
        }
        if (proto.hasVideoMonth3CntFeature()) {
            feature.setMonth3_cnt_features(convert(proto.getVideoMonth3CntFeature()));
        } else {
            feature.setMonth3_cnt_features(new UserActionFeature());
        }
        return feature;
    }


}
