package com.idfc.streaming;

import com.idfc.streaming.models.pojo.UserEventSink;
import com.idfc.streaming.utils.ConfigHandler;
import com.idfc.streaming.utils.FlinkStreamExecutionEnv;
import com.idfc.streaming.utils.GenericClassDeserializer;
import com.idfc.streaming.utils.SyncedBoundedOutOfOrdernessWatermarks;
import org.apache.flink.api.common.eventtime.WatermarkGenerator;
import org.apache.flink.api.common.eventtime.WatermarkGeneratorSupplier;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;

import java.sql.Timestamp;
import java.time.Duration;
import java.util.Properties;

public class EosWithEventTImeAndWatermarkRdsSink {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = FlinkStreamExecutionEnv.getFlinkExecution();
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
//        env.setParallelism(1);
//        env.getConfig().setAutoWatermarkInterval(100);



        System.out.println("Flink Configuration Parameters:");
        env.getConfig().toConfiguration().toMap().forEach((key, value) -> {
            System.out.println(key + " = " + value);
        });

        Properties kafkaSecurityProps = new Properties();

        kafkaSecurityProps.put("security.protocol", "SASL_SSL");
        kafkaSecurityProps.put("sasl.mechanism", "SCRAM-SHA-512");
        kafkaSecurityProps.put("sasl.jaas.config", String.format(
                "org.apache.kafka.common.security.scram.ScramLoginModule required " +
                        "username=\"%s\" password=\"%s\";",
                ConfigHandler.get("aws.msk.kafka.user.name"),
                ConfigHandler.get("aws.msk.kafka.password")
        ));


        KafkaSource kafkaSource = KafkaSource
                .<UserEventSink>builder()
                .setBootstrapServers(ConfigHandler.get("data.bootstrap.servers"))
                .setGroupId(ConfigHandler.get("data.local.test.consumer.group.id"))
                .setTopics(ConfigHandler.get("data.sample.user.activity.topic.name"))
                .setDeserializer(new GenericClassDeserializer<>(UserEventSink.class))
                .setStartingOffsets(OffsetsInitializer.earliest())
                .setProperties(kafkaSecurityProps)
                .build();


//        boolean customWaterMark = Boolean.valueOf(ConfigHandler.getWithDefaultFallBack("use.custom.sync.watermark","true"));
        WatermarkStrategy<UserEventSink> watermarkStrategy =
                WatermarkStrategy.forGenerator(new WatermarkGeneratorSupplier<UserEventSink>() {
                    @Override
                    public WatermarkGenerator<UserEventSink> createWatermarkGenerator(Context context) {
                        return  new SyncedBoundedOutOfOrdernessWatermarks(Duration.ofSeconds(10));
                    }
                }).withTimestampAssigner((event, recordTimestamp) -> event.getEventEpoch());

        SingleOutputStreamOperator<UserEventSink> userActivityStream = env.fromSource(kafkaSource,watermarkStrategy ,"generic-kafka-source");
        OutputTag<UserEventSink> lateEventsTag = new OutputTag<UserEventSink>("device-type-late-events") {};
        SingleOutputStreamOperator<Tuple3<Timestamp, String, Double>> deviceTypeAggregatedStream = userActivityStream
                .keyBy(UserEventSink::getLocation)
                .window(TumblingEventTimeWindows.of(Time.minutes(1)))
//                .allowedLateness(Duration.ofSeconds(10))
                .sideOutputLateData(lateEventsTag)
                .trigger(EventTimeTrigger.create())
                .aggregate(new AggregateFunction<UserEventSink, Double, Double>() {
                    @Override
                    public Double createAccumulator() {
                        return 0.0;
                    }

                    @Override
                    public Double add(UserEventSink event, Double acc) {
                        return acc + event.getAmount();
                    }

                    @Override
                    public Double getResult(Double acc) {
                        return acc;
                    }

                    @Override
                    public Double merge(Double a, Double b) {
                        return a + b;
                    }
                }, new ProcessWindowFunction<Double, Tuple3<Timestamp, String, Double>, String, TimeWindow>() {
                    @Override
                    public void process(String key, Context context, Iterable<Double> elements, Collector<Tuple3<Timestamp, String, Double>> out) {
                        long windowStart = context.window().getStart();
                        long windowEnd = context.window().getEnd();

                        Timestamp startTimestamp = new Timestamp(windowStart);
                        Timestamp endTimestamp = new Timestamp(windowEnd);

                        Double totalAmount = elements.iterator().next();
                        out.collect(new Tuple3<>(startTimestamp, key, totalAmount));
                    }
                });

        deviceTypeAggregatedStream.getSideOutput(lateEventsTag).print();


        deviceTypeAggregatedStream.print();


//        deviceTypeAggregatedStream
//                .getSideOutput(lateEventsTag)
//                .process(new ProcessFunction<UserEventSink, UserEventSink>() {
//                    @Override
//                    public void processElement(UserEventSink event, Context ctx, Collector<UserEventSink> out) {
//                        long processingTime = ctx.timerService().currentProcessingTime();
//                        System.out.println("LATE EVENT: " + event +
//                                " | Event Time: " + new Timestamp(event.getEventEpoch()) +
//                                " | Current Processing Time: " + new Timestamp(processingTime) +
//                                " | Lateness: " + (processingTime - event.getEventEpoch()) + " ms");
//                        out.collect(event);
//                    }
//                });
//        deviceTypeAggregatedStream.print();

//        deviceTypeAggregatedStream.addSink(JdbcSink.sink(
//                "INSERT INTO flink_aggregated_metrics " +
//                        "(window_start, device_type, total_amount,updated_epoch) VALUES (?, ?, ?, ?); " ,
//                (statement, record) -> {
//                    statement.setTimestamp(1, record.f0);
//                    statement.setString(2, record.f1);
//                    statement.setDouble(3, record.f2);
//                    statement.setLong(4,System.currentTimeMillis());
//                },
//                JdbcExecutionOptions.builder()
//                        .withBatchSize(100)
//                        .withBatchIntervalMs(200)
//                        .withMaxRetries(3)
//                        .build(),
//                new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
//                        .withUrl("jdbc:mysql://localhost:3306/flink_db")
////                        .withDriverName("org.postgresql.Driver")
//                        .withUsername("root")
//                        .withPassword("pass")
//                        .build()
//        ));

        env.execute("Flink Job for EOS testing");
    }

}
