package com.ho.business.util;

import com.ho.business.entity.OrderIssuedReqVO;
import com.ho.business.entity.SendResultVo;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class SendQueueUtil {

    /** todo: 电站下发数据队列 */
    /** start */
    private static final ConcurrentHashMap<Integer, List<OrderIssuedReqVO>> STATION_SEND_MAP = new ConcurrentHashMap<>();

    private static final ReentrantLock stationSendLocker = new ReentrantLock();

    /** todo: 如果队列不为空，那就取出所有的key */
    public static Enumeration<Integer> getSendStationIds(){
        Enumeration<Integer> keys = null;
        if ( STATION_SEND_MAP != null ) {
            keys = STATION_SEND_MAP.keys();
        }
        return keys;
    }

    public static void setSendMap( List<OrderIssuedReqVO> map,Integer stationId ){
        STATION_SEND_MAP.put(stationId,map);
    }

    /** todo: 根据stationId取队列里的数据 */
    public static Map<Integer,List<OrderIssuedReqVO>> getSendMap(){
        Map<Integer,List<OrderIssuedReqVO>> map = new HashMap<>(0);
        stationSendLocker.lock();
        try {
            if ( !STATION_SEND_MAP.isEmpty() ) {
                Set<Map.Entry<Integer, List<OrderIssuedReqVO>>> entries = STATION_SEND_MAP.entrySet();
                for (Map.Entry<Integer, List<OrderIssuedReqVO>> entry : entries) {
                    map.put(entry.getKey(),entry.getValue());
                    STATION_SEND_MAP.remove(entry.getKey());
                    break;
                }
            }
        } finally {
            stationSendLocker.unlock();
        }
        return map;
    }

    public static void removeSendMap( Integer stationId ){
        stationSendLocker.lock();
        try {
            STATION_SEND_MAP.remove(stationId);
        } finally {
            stationSendLocker.unlock();
        }
    }
    /** end */

    /**
     * 根据电站id保存当前计算进度数据
     */
    private static final ConcurrentHashMap<Integer, SendResultVo> ONE_KEY_PROGRESS = new ConcurrentHashMap<Integer, SendResultVo>();

    private static final ReentrantLock oneKeyProgressLock = new ReentrantLock();

    /**
     * 存放进度数据
     * @param stationId
     * @param progress
     */
    public static void setOneKeyProgress( Integer stationId,SendResultVo sendResult ){
        oneKeyProgressLock.lock();
        try {
            ONE_KEY_PROGRESS.put(stationId,sendResult);
        } finally {
            oneKeyProgressLock.unlock();
        }
    }

    /**
     * 获取进度数据
     * @param stationId
     * @return
     */
    public static SendResultVo getOneKeyProgress( Integer stationId ){
        SendResultVo sendResult = null;
        if ( !ONE_KEY_PROGRESS.isEmpty() ) {
            sendResult = ONE_KEY_PROGRESS.get(stationId);
        }
        return sendResult;
    }

    /**
     * 移除进度数据
     * @param stationId
     */
    public static void removeOneKeyProgress( Integer stationId ){
        oneKeyProgressLock.lock();
        try {
            ONE_KEY_PROGRESS.remove(stationId);
        } finally {
            oneKeyProgressLock.unlock();
        }
    }


    private static final ConcurrentHashMap<String, List<OrderIssuedReqVO>> STATION_CONTROL_MAP = new ConcurrentHashMap<>();

    private static final ReentrantLock stationControlLocker = new ReentrantLock();


    public static Enumeration<String> getControlStationIds(){
        Enumeration<String> keys = null;
        if ( STATION_CONTROL_MAP != null ) {
            keys = STATION_CONTROL_MAP.keys();
        }
        return keys;
    }

    public static void setControlMap( List<OrderIssuedReqVO> map,String stationId ){
        STATION_CONTROL_MAP.put(stationId,map);
    }

    /** todo: 根据stationId取队列里的数据 */
    public static Map<String,List<OrderIssuedReqVO>> getControlMap(){
        Map<String,List<OrderIssuedReqVO>> map = new HashMap<>(0);
        stationControlLocker.lock();
        try {
            if ( !STATION_CONTROL_MAP.isEmpty() ) {
                Set<Map.Entry<String, List<OrderIssuedReqVO>>> entries = STATION_CONTROL_MAP.entrySet();
                for (Map.Entry<String, List<OrderIssuedReqVO>> entry : entries) {
                    map.put(entry.getKey(),entry.getValue());
                    STATION_CONTROL_MAP.remove(entry.getKey());
                    break;
                }
            }
        } finally {
            stationControlLocker.unlock();
        }
        return map;
    }

    public static void removeControlMap( String stationId ){
        stationControlLocker.lock();
        try {
            STATION_CONTROL_MAP.remove(stationId);
        } finally {
            stationControlLocker.unlock();
        }
    }

    /**
     *
     */
    /**
     * 根据电站id和点位id保存当前计算进度数据
     */
    private static final ConcurrentHashMap<String, Integer> ONE_CONTROL_PROGRESS = new ConcurrentHashMap<String, Integer>();

    private static final ReentrantLock oneControlProgressLock = new ReentrantLock();

    /**
     * 存放进度数据
     * @param stationId
     * @param progress
     */
    public static void setOneControlProgress( String stationId,Integer isEnd ){
        oneControlProgressLock.lock();
        try {
            ONE_CONTROL_PROGRESS.put(stationId,isEnd);
        } finally {
            oneControlProgressLock.unlock();
        }
    }

    /**
     * 获取进度数据
     * @param stationId
     * @return
     */
    public static Integer getOneControlProgress(String stationId ){
        Integer isEnd = null;
        if ( !ONE_CONTROL_PROGRESS.isEmpty() ) {
            isEnd = ONE_CONTROL_PROGRESS.get(stationId);
        }
        return isEnd;
    }

    /**
     * 移除进度数据
     * @param stationId
     */
    public static void removeOneControlProgress( String stationId ){
        oneControlProgressLock.lock();
        try {
            ONE_CONTROL_PROGRESS.remove(stationId);
        } finally {
            oneControlProgressLock.unlock();
        }
    }

    public static final String REDIS_PROGRESS_KEY = "send_progress";

    public static final String REDIS_PROGRESS_KEY_4_CONTROL = "send_progress_control:";

    public static final String REDIS_KEYS = "send_control_keys:";

}
