package org.jeecg.modules.adweb.marketing.googleads.vo;

import lombok.Data;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.adweb.common.constant.NumConstant;
import org.jeecg.modules.adweb.common.util.NumberUtil;
import org.jeecg.modules.adweb.marketing.googleads.dto.MetricsDTO;

import java.util.Map;

/**
 * @author wfansh
 */
@Data
public class PlacementStatsVO {

    private String placement; // 展示位

    private String type; // 类型

    private long impressions; // 展示数

    private long clicks; // 点击数

    private double cost; // 花费

    public static PlacementStatsVO fromMetricsDTO(Map.Entry<String, MetricsDTO> metricsDTOEntry) {
        PlacementStatsVO placementStatsVO = new PlacementStatsVO();

        placementStatsVO.setPlacement(metricsDTOEntry.getKey());
        placementStatsVO.setType(parseType(metricsDTOEntry.getKey()));

        MetricsDTO metricsDTO = metricsDTOEntry.getValue();
        placementStatsVO.setImpressions(metricsDTO.getImpressions());
        placementStatsVO.setClicks(metricsDTO.getClicks());
        placementStatsVO.setCost(
                NumberUtil.formatDecimal(
                                NumberUtil.safeDivide(
                                        metricsDTO.getCostMicros(), NumConstant.ONE_MILLION),
                                2)
                        .doubleValue());

        return placementStatsVO;
    }

    private static String parseType(String placement) {
        // 1. Mobile apps
        if (StringUtils.startsWithIgnoreCase(placement, "iOS:")) {
            return "iOS应用";
        }

        if (StringUtils.startsWithIgnoreCase(placement, "Android:")) {
            return "Android应用";
        }

        if (StringUtils.startsWithIgnoreCase(placement, "other:")) {
            return "其它应用";
        }

        // 2.Google products
        if (StringUtils.equalsIgnoreCase(placement, "Google")) {
            return placement;
        }

        // 3.YouTube
        if (StringUtils.startsWithIgnoreCase(placement, "YouTube -")) {
            return "YouTube";
        }

        // 4. 网站
        return "网站";
    }
}
