package com.fshark.overseas.advert.sink;

import com.fshark.overseas.advert.entity.GameAccount;
import com.fshark.overseas.advert.entity.dao.GameAccountDao;
import com.fshark.overseas.advert.entity.dao.OrderDao;
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.util.*;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import java.util.Optional;

public class PurchaseSink extends RichSinkFunction<AppsFlyerEvent> {

    private AppConfig appConfig;

    private JdbiContext jdbiContext;
    private OrderDao orderDao;
    private GameAccountDao gameAccountDao;

    public PurchaseSink(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());
        orderDao = jdbiContext.getJdbi().onDemand(OrderDao.class);
        gameAccountDao = jdbiContext.getJdbi().onDemand(GameAccountDao.class);
    }

    @Override
    public void invoke(AppsFlyerEvent value, Context context) throws Exception {
        super.invoke(value, context);
        AppsflyerPurchase appsflyerPurchase = new Gson().fromJson(value.getEvent_value(), AppsflyerPurchase.class);
        if (appsflyerPurchase == null || appsflyerPurchase.getBundle_id() == null) {  //不是我们上报的格式
            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);
        Optional<GameAccount> accountActivition = gameAccountDao.findByPrimary(value, Constants.TYPE_ACTIVIYION);
        if (accountActivition.isPresent()) {
            GameAccount gameAccount = accountActivition.get();
            orderData.setActivation_time(gameAccount.getEvent_time());
            CommonUtils.setType(Long.parseLong(DateUtils.formatAsYMD(gameAccount.getEvent_time())),orderData.getMedia_source(),orderData.getData_type());
        }

        Optional<GameAccount> accountRegister = gameAccountDao.findByPrimary(value, Constants.TYPE_REGISTER);
        if (accountRegister.isPresent()) {
            GameAccount gameAccount = accountRegister.get();
            orderData.setReg_time(gameAccount.getEvent_time());
        }
        orderDao.insertOrder(orderData);
        gameAccountDao.insertPauchase(value);
    }

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

    }
}
