package org.ts.ddcs.worker;


import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.config.ServerConfig;
import org.ts.ddcs.db.DatabaseService;
import org.ts.ddcs.log.LogHelper;


//import org.springframework.amqp.core.AmqpTemplate;
import org.ts.ddcs.mqtt.Mqtt;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Component
public class DataProcess {

    //    private ExecutorService logSelectThreadPool = Executors.newFixedThreadPool(3);
//    private ExecutorService logSplitThreadPool = Executors.newFixedThreadPool(1);
  //  private ExecutorService logMapThreadPool = Executors.newFixedThreadPool(1);
    // private ExecutorService logCollectErrorThreadPool = Executors.newFixedThreadPool(1);
    //  private ExecutorService logDataCollectThreadPool = Executors.newFixedThreadPool(1);
    // private ExecutorService fdfsThreadPool = Executors.newFixedThreadPool(2);


    private static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("data-process-pool-%d").build();
    /**
     * 公共线程池
     **/
    private static ExecutorService publicThreadPool = new ThreadPoolExecutor(1, 100,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());

    @Autowired
    public RedisTemplate redisTemplate;
//
//    @Autowired
//    public JdbcTemplate jdbcTemplate;

    @Autowired
    public DatabaseService databaseService;

//    @Autowired
//    private HbaseTemplate hbaseTemplate;

/*    @Autowired
    public AmqpTemplate amqpTemplate;*/

    @Autowired
    public Mqtt mqttServer;

    @Autowired
    public ServerConfig config;

    @Autowired
    public KafkaTemplate kafkaTemplate;

    public long msgNotifyTime=0;


    public static ServerConfig config() {
        return DataProcessHolder.newInstance().dataProcess.config;
    }


    @PostConstruct
    public void init() {
        DataProcessHolder.newInstance().dataProcess = this;
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
    }

//    public void SubmitLogSelectTask(String log) {
//        try {
//            LogSelectWorker workertask = new LogSelectWorker(log);
//            FutureTask<Integer> futureTask = new FutureTask<>(workertask);
//            this.logSelectThreadPool.submit(futureTask);
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }

//    public void SubmitFdfsTask(String tm) {
//        try {
//            ReadFdfsFileWorker workertask = new ReadFdfsFileWorker(tm);
//            FutureTask<Integer> futureTask = new FutureTask<>(workertask);
//            this.fdfsThreadPool.submit(futureTask);
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }

//    public void test() {
//        List<Map<String, Object>> list = hbaseTemplate.find("ts:pptn", "hour", new RowMapper<Map<String, Object>>() {
//
//            @Override
//            public Map<String, Object> mapRow(Result result, int i) throws Exception {
//                Map<String, Object> d = new HashMap<>();
//                List<Cell> cells = result.listCells();
//                for (Cell cell : cells) {
//                    String field = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
//                    String res = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
//                    System.out.println(field + res);
//                }
//
//                return d;
//            }
//        });
//    }


//    public void SubmitCollectDataTask(String log) {
//        try {
//            LogDataCollectWorker workertask = new LogDataCollectWorker(log);
//            FutureTask<Integer> futureTask = new FutureTask<>(workertask);
//            // this.logDataCollectThreadPool.submit(futureTask);
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }


//    public void SubmitSplitLogTask(String log) {
//        try {
//            LogSpliteWorker workertask = new LogSpliteWorker(log);
//            FutureTask<Integer> futureTask = new FutureTask<>(workertask);
//            this.logSplitThreadPool.submit(futureTask);
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }

//    public void SubmitSplitLzLogTask(String log) {
//        try {
//            LzLogSpliteWorker workertask = new LzLogSpliteWorker(log);
//            FutureTask<Integer> futureTask = new FutureTask<>(workertask);
//            this.logSplitThreadPool.submit(futureTask);
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }

    public void submitMapTask(JSONObject log) {
        try {
            LogMapWorker task = new LogMapWorker(log);
            FutureTask<Integer> futureTask = new FutureTask<>(task);
            publicThreadPool.submit(futureTask);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

//    public void SubmitCollectErrorTask(JSONObject log) {
//        try {
//            LogCollectErrorWorker workertask = new LogCollectErrorWorker(log);
//            FutureTask<Integer> futureTask = new FutureTask<>(workertask);
//            //  this.logCollectErrorThreadPool.submit(futureTask);
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }
}
