package com.fshark.overseas.advert.processor;

import com.alibaba.fastjson.JSONObject;
import com.fshark.overseas.advert.entity.GameAccount;
import com.fshark.overseas.advert.entity.ServerRoleAccount;
import com.fshark.overseas.advert.entity.dao.GameAccountDao;
import com.fshark.overseas.advert.entity.dao.GameMappingDao;
import com.fshark.overseas.advert.entity.dao.OrderDao;
import com.fshark.overseas.advert.entity.dao.ServerRoleDao;
import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.AppsflyerPurchase;
import com.fshark.overseas.advert.modle.OrderData;
import com.fshark.overseas.advert.modle.SinkMetricEvent;
import com.fshark.overseas.advert.modle.mapping.Game;
import com.fshark.overseas.advert.modle.metrics.ActivationMetrics;
import com.fshark.overseas.advert.util.*;
import com.google.gson.Gson;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.http.util.TextUtils;

import java.util.Optional;

public class OuidOrderDataProcessor extends RichFlatMapFunction<AppsFlyerEvent, String> {

    private AppConfig appConfig;

    private JdbiContext jdbiContext;
    private GameAccountDao gameAccountDao;

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

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


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

    @Override
    public void flatMap(AppsFlyerEvent value, Collector<String> out) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(value.getEvent_value());
        String ouid = jsonObject.getString("ouid");
        AppsflyerPurchase appsflyerPurchase = new Gson().fromJson(value.getEvent_value(), AppsflyerPurchase.class);
        if (appsflyerPurchase == null || appsflyerPurchase.getBundle_id() == null || TextUtils.isEmpty(ouid)) {  //不是我们上报的格式
            return;
        }
        if ("ios".equalsIgnoreCase(value.getPlatform())) {
            String bundleId = appsflyerPurchase.getBundle_id();
            value.setGame_id(bundleId == null ? value.getGame_id() : bundleId);
        }
        OrderData orderData = new OrderData(value);
        orderData.setOuid(ouid);
        Optional<GameAccount> ouidRegister = gameAccountDao.findByOuid(value, ouid, Constants.TYPE_OUID_REGISTER);
        if (ouidRegister.isPresent()) {
            GameAccount gameAccount = ouidRegister.get();
            orderData.setMedia_source(gameAccount.getMediaSource());
            orderData.setCampaign_id(gameAccount.getCampaign_id());
            orderData.setAd_set_id(gameAccount.getAd_set_id());
            orderData.setAd_id(gameAccount.getAd_id());
            orderData.setOuid_reg_time(gameAccount.getEvent_time());
            orderData.setChannel(gameAccount.getChannel());
            orderData.setRegion(gameAccount.getRegion());
            orderData.setData_type(CommonUtils.setType(Long.parseLong(DateUtils.formatAsYMD(gameAccount.getEvent_time())), orderData.getMedia_source(), orderData.getData_type()));
        }

        SinkMetricEvent sinkMetricEvent = new SinkMetricEvent();
        sinkMetricEvent.setEventType(Constants.TYPE_SINK_OUID_ORDER);
        sinkMetricEvent.setOrderData(orderData);
        out.collect(new Gson().toJson(sinkMetricEvent));
    }
}
