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;

/**
 * @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);

        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 eventCode = jsonObject.getString("event_code");
        interactRecordMessageBO.setEventType(this.convertEventType(eventCode));

        String weimoFlag = cuspJson.getString("is_weiMob");
        interactRecordMessageBO.setWeimoGoodsFlag(this.convertWeimoFlag(weimoFlag));
        String goodsId = cuspJson.getString("goods_id");
        interactRecordMessageBO.setGoodsId(goodsId);
        String goodsCode = cuspJson.getString("goods_code");
        interactRecordMessageBO.setGoodsCode(goodsCode);
        String goodsName = cuspJson.getString("goods_name");
        interactRecordMessageBO.setGoodsName(goodsName);

        Integer duration = jsonObject.getInteger("duration");
        interactRecordMessageBO.setDurationTime(duration);

        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);

        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;
            default:
                return 0;
        }
    }

    private Integer convertEventType(String eventCode) {
        if (StringUtils.isBlank(eventCode)) {
            return null;
        }
        switch (eventCode) {
            case "material_page":
            case "material_page_duration":
                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, "是");
    }
}
