package com.fshark.overseas.advert.processor;

import com.fshark.overseas.advert.entity.GameAccount;
import com.fshark.overseas.advert.entity.dao.GameAccountDao;
import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.metrics.ActivationMetrics;
import com.fshark.overseas.advert.modle.metrics.BaseMetrics;
import com.fshark.overseas.advert.util.*;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.util.Collector;

import java.util.Locale;
import java.util.Optional;

/**
 * 设备激活
 */
public class ActivitionProcess extends RichFlatMapFunction<AppsFlyerEvent, ActivationMetrics> {

    private AppConfig appConfig;

    private JdbiContext jdbiContext;
    private GameAccountDao gameAccountDao;

    public ActivitionProcess(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        jdbiContext = DatabaseUtils.getCHGenJdbiClient(appConfig.getDbHost(), appConfig.getDbUser(), appConfig.getDbPassword());
        gameAccountDao = jdbiContext.getJdbi().onDemand(GameAccountDao.class);
    }

    @Override
    public void flatMap(AppsFlyerEvent value, Collector<ActivationMetrics> out) throws Exception {
        ActivationMetrics activationMetrics = new ActivationMetrics();
        copyBaseMetrics(value, activationMetrics);
        activationMetrics.setUid(value.getUid());
        Optional<GameAccount> account = gameAccountDao.findByPrimary(value, Constants.TYPE_ACTIVIYION);
        String event_time = DateUtils.formatAsYMDHMS(value.getEvent_time());

        if (!account.isPresent() || value.getIs_retargeting()) { //
            gameAccountDao.insertActivition(value, event_time);
            activationMetrics.setData_activation(1L);
            activationMetrics.setData_date(Long.parseLong(DateUtils.formatAsYMD(event_time)));
            activationMetrics.setData_hour(Integer.parseInt(DateUtils.formatAsHH(event_time)));
            out.collect(activationMetrics);
        }
    }

    private void copyBaseMetrics(AppsFlyerEvent value, BaseMetrics metrics) {
        metrics.setTime_zone(value.getTime_zone());
        metrics.setPlatform(value.getPlatform());
        metrics.setMedia_source(value.getMedia_source());
        metrics.setGame_id(value.getGame_id());
        metrics.setCampaign_id(value.getCampaign_id());
        metrics.setAd_set_id(value.getAd_set_id());
        metrics.setAd_id(value.getAd_id());
        metrics.setRegion(value.getRegion());
        metrics.setChannel(value.getChannel());
        metrics.setData_type(1);
        if (value.getIs_retargeting()) { //再营销数据
            metrics.setData_type(2);
        }
        if (value.getCampaign().toLowerCase(Locale.ROOT).startsWith("acp")) { //预注册数据
            metrics.setData_type(3);
        }

    }

    @Override
    public void close() throws Exception {
        super.close();
        if (jdbiContext != null) {
            jdbiContext.getDataSource().close();
        }
    }

}
