package com.gic.haoban.manage.service.service.content.adaptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.api.enums.content.MaterialInteractRecordEventType;
import com.gic.haoban.manage.service.pojo.bo.content.message.InteractRecordMessageBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author MUSI
 * @Date 2023/9/4 3:14 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component
public class MessageConvertHelper {


    /**
     * 转换消息
     * @param text
     * @return
     */
    public InteractRecordMessageBO convertMessageBo(String text) {

        JSONObject jsonObject = JSON.parseObject(text);
        if (MapUtils.isEmpty(jsonObject)) {
            return null;
        }
        InteractRecordMessageBO interactRecordMessageBO = new InteractRecordMessageBO();

        String appKey = jsonObject.getString("app_key");
        interactRecordMessageBO.setAppKey(appKey);

        String gp = jsonObject.getString("gp");
        JSONObject gpJson = JSON.parseObject(gp);

        String cusp = jsonObject.getString("cusp");
        JSONObject cuspJson = JSON.parseObject(cusp);
        Integer isPreview = cuspJson.getInteger("is_preview");
        if (Objects.equals(isPreview, 1)) {
            log.info("预览数据不处理");
            return null;
        }

        String sysp = jsonObject.getString("sysp");
        JSONObject syspJson = JSON.parseObject(sysp);

        String enterpriseId = gpJson.getString("merch_id");
        interactRecordMessageBO.setEnterpriseId(enterpriseId);

        String memberId = gpJson.getString("member_id");
        interactRecordMessageBO.setMemberId(memberId);

        String unionId = gpJson.getString("union_id");
        interactRecordMessageBO.setUnionId(unionId);

        String clerkId = cuspJson.getString("share_id");
        interactRecordMessageBO.setClerkId(clerkId);

        String storeId = cuspJson.getString("share_shop_id");
        interactRecordMessageBO.setStoreId(storeId);

        String materialId = cuspJson.getString("material_id");
        interactRecordMessageBO.setMaterialId(materialId);

        String shareMaterialChannel = cuspJson.getString("share_material_channel");
        interactRecordMessageBO.setChannelSource(this.convertChannelSource(shareMaterialChannel));
        String shareSource = cuspJson.getString("share_source");
        interactRecordMessageBO.setShareSource(this.convertShareSource(shareSource));

        String eventCode = jsonObject.getString("event_code");
        interactRecordMessageBO.setEventType(this.convertEventType(eventCode));

        String weimoFlag = cuspJson.getString("is_weiMob");
        interactRecordMessageBO.setWeimoGoodsFlag(this.convertWeimoFlag(weimoFlag));
        String goodsChannel = cuspJson.getString("goods_channel");
        interactRecordMessageBO.setGoodsChannel(goodsChannel);
        String goodsId = cuspJson.getString("goods_id");
        interactRecordMessageBO.setGoodsId(goodsId);
        String wxaLinkId = cuspJson.getString("wxa_link_id");
        interactRecordMessageBO.setWxaLinkId(wxaLinkId);
        String goodsCode = cuspJson.getString("goods_code");
        interactRecordMessageBO.setGoodsCode(goodsCode);
        String goodsName = cuspJson.getString("goods_name");
        interactRecordMessageBO.setGoodsName(goodsName);
        Long duration = jsonObject.getLong("duration");
        Integer durationInt = 0;
        if (duration != null && duration < Integer.MAX_VALUE) {
            durationInt = duration.intValue();
        }
        interactRecordMessageBO.setDurationTime(durationInt);

        String orderNumber = cuspJson.getString("order_number");
        interactRecordMessageBO.setOrderNumber(orderNumber);

        String runningUuid = gpJson.getString("runningUuid");
        interactRecordMessageBO.setBusinessUUId(runningUuid);

        Long lastAccessTime = jsonObject.getLong("server_timestamp");
        interactRecordMessageBO.setLastAccessTime(lastAccessTime);

        String refUrl = syspJson.getString("ref_url");
        interactRecordMessageBO.setRefUrl(refUrl);

        dealRecordFilter(eventCode, interactRecordMessageBO);
        return interactRecordMessageBO;
    }

    private Integer convertChannelSource(String shareMaterialChannel) {
        if (StringUtils.isBlank(shareMaterialChannel)) {
            return null;
        }
        switch (shareMaterialChannel) {
            case "null" :
            case "NULL":
                return null;
            case "朋友圈":
                return 1;
            case "客户群":
                return 2;
            case "对话框":
                return 3;
            case "潜客推荐":
                return 6;
            default:
                return 0;
        }
    }

    private Integer convertShareSource(String shareSource) {
        if (StringUtils.isBlank(shareSource)) {
            return null;
        }
        switch (shareSource) {
            case "null" :
            case "NULL":
                return null;
            case "意向客户":
                return 1;
            case "智能选款":
                return 2;
            case "连单推荐":
                return 3;
            case "其他":
            default:
                return -1;
        }
    }

    private void dealRecordFilter(String eventCode,InteractRecordMessageBO interactRecordMessageBO) {
        if (StringUtils.isBlank(eventCode) || interactRecordMessageBO == null) {
            return;
        }
        // 商品详情页埋点如果是微盟类型商品不计算页面停留时长
        if (Objects.equals("store_goods_detail", eventCode)
                && Objects.equals("微盟", interactRecordMessageBO.getGoodsChannel())) {
            interactRecordMessageBO.setDurationTime(0);
        }
    }

    private Integer convertEventType(String eventCode) {
        if (StringUtils.isBlank(eventCode)) {
            return null;
        }
        switch (eventCode) {
            case "material_page":
            case "material_page_duration":
            case "recommended_product_list":
                return MaterialInteractRecordEventType.VISIT_MATERIAL.getCode();
            case "store_goods_detail":
            case "click_goods":
                return MaterialInteractRecordEventType.VISIT_PRODUCT.getCode();
            case "store_buy_sucess":
                return 3;
            case "store_add_cart":
                return MaterialInteractRecordEventType.ADD_SHOPPING_CART.getCode();
            default:
                return null;
        }
    }

    public boolean convertWeimoFlag(String weimoFlag) {
        if (StringUtils.isBlank(weimoFlag)) {
            return false;
        }
        return StringUtils.equals(weimoFlag, "是");
    }
}
