package com.fshark.overseas.advert.processor;


import com.fshark.overseas.advert.entity.dao.GameMappingDao;
import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.PlatformEnum;
import com.fshark.overseas.advert.modle.mapping.AdMapping;
import com.fshark.overseas.advert.modle.mapping.Media;
import com.fshark.overseas.advert.modle.mapping.MediaCampaign;
import com.fshark.overseas.advert.modle.metrics.ActivationMetrics;
import com.fshark.overseas.advert.util.AppConfig;
import com.fshark.overseas.advert.util.DatabaseUtils;
import com.fshark.overseas.advert.util.JdbiContext;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.Optional;

/**
 * 更新游戏广告的映射
 */
public class GameAdMappingProcessor extends ProcessFunction<AppsFlyerEvent, ActivationMetrics> {


    private AppConfig appConfig;

    private JdbiContext jdbiContext;

    private GameMappingDao gameMappingDao;


    public GameAdMappingProcessor(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());
        gameMappingDao = jdbiContext.getJdbi().onDemand(GameMappingDao.class);

    }

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


    @Override
    public void processElement(AppsFlyerEvent value, ProcessFunction<AppsFlyerEvent, ActivationMetrics>.Context ctx, Collector<ActivationMetrics> out) throws Exception {
        int platform_id = 1;
        if (PlatformEnum.IOS.getName().equals(value.getPlatform())) {
            platform_id = 2;
        } else if (PlatformEnum.PC.getName().equals(value.getPlatform())) {
            platform_id = 3;
        } else if (PlatformEnum.MUTI.getName().equals(value.getPlatform())) {
            platform_id = 4;
        }

        //更新game
        boolean gameByPrimary = gameMappingDao.findGameByPrimary(value.getGame_id(), platform_id).isPresent();
        if (!gameByPrimary) {
            gameMappingDao.saveGame(value, platform_id);
        }

        //mediaSource更新
        Optional<Media> mediaByPrimary = gameMappingDao.findMediaByPrimary(value);
        if (!mediaByPrimary.isPresent()) {
            gameMappingDao.saveMediaSource(value);
        } else {
            //MediaCampaign更新
            Media media = mediaByPrimary.get();
            Optional<MediaCampaign> mediaCampaign = gameMappingDao.findMediaCampaign(value);
            if (!mediaCampaign.isPresent() || !mediaCampaign.get().getAf_group_name().equals(value.getCampaign())) {
                gameMappingDao.saveMediaCampaign(value, media);
            }
            //        adMapping 更新
            Optional<AdMapping> adMapping = gameMappingDao.findAdMapping(value);
            if (!adMapping.isPresent() ||
                    !adMapping.get().getAf_ad_group_name().equals(value.getCampaign()) ||
                    !adMapping.get().getAf_ad_plan_name().equals(value.getAd_set()) ||
                    !adMapping.get().getAf_ad_name().equals(value.getAd())) {
                gameMappingDao.saveAdMapping(value, media);
            }
        }
    }
}
