package cn.com.qmth.stmms.common.redis;

import java.text.MessageFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.io.SocketOutputStream;

import cn.com.qmth.stmms.common.enums.LibraryType;
import cn.com.qmth.stmms.common.enums.MarkStage;

public class RedisKeyBuilder {

    private static final String MARK_CURRENT_TASK_KEY = "mark:current:{0}:{1}";

    private static final String MARK_PUBLIC_TASK_KEY = "mark:public:{0}:{1}";

    private static final String MARK_STANDARD_TASK_KEY = "mark:private:{0}:{1}";

    private static final String MARK_BACK_TASK_KEY = "mark:back:{0}:{1}";

    private static final String MARK_SELF_TASK_KEY = "mark:self:{0}:{1}";

    private static final String MARK_COUNT_KEY = "mark:count:{0}:{1}";

    private static final String BLOCK_TOTAL_COUNT_KEY = "block:total:count:{0}";

    private static final String BLOCK_MARKED_TASK_KEY = "block:marked:task:{0}";
    
    private static final String MARK_CURRENT_TASK_TIME_KEY = "mark:current:time:key:{0}:{1}";
    
    /**
     * 已领取未给分的评卷任务
     * 
     * @param examId
     * @param schoolId
     * @param blockId
     * @return
     */
    public static String getCurrentTaskKey(int examId, String subjectCode) {
        return MessageFormat.format(MARK_CURRENT_TASK_KEY, String.valueOf(examId), subjectCode);
    }

    /**
     * 公共评卷任务
     * 
     * @param examId
     * @param schoolId
     * @param blockId
     * @return
     */
    public static String getPublicTaskKey(LibraryType libraryType, int blockId) {
        return MessageFormat.format(MARK_PUBLIC_TASK_KEY, String.valueOf(libraryType.getValue()),
                String.valueOf(blockId));
    }

    /**
     * 个人标准卷任务
     * 
     * @param examId
     * @param schoolId
     * @param blockId
     * @return
     */
    public static String getStandardTaskKey(MarkStage stage, int markerId) {
        return MessageFormat.format(MARK_STANDARD_TASK_KEY, String.valueOf(stage.getValue()), String.valueOf(markerId));
    }

    /**
     * 个人打回任务
     * 
     * @param examId
     * @param schoolId
     * @param blockId
     * @return
     */
    public static String getBackTaskKey(LibraryType libraryType, int markerId) {
        return MessageFormat.format(MARK_BACK_TASK_KEY, String.valueOf(libraryType.getValue()),
                String.valueOf(markerId));
    }

    /**
     * 个人自评任务
     * 
     * @param examId
     * @param schoolId
     * @param blockId
     * @return
     */
    public static String getSelfTaskKey(LibraryType libraryType, int markerId) {
        return MessageFormat.format(MARK_SELF_TASK_KEY, String.valueOf(libraryType.getValue()),
                String.valueOf(markerId));
    }

    /**
     * 个人评卷数量
     * 
     * @param examId
     * @param schoolId
     * @param markerId
     * @return
     */
    public static String getMarkCountKey(MarkStage stage, int markerId) {
        return MessageFormat.format(MARK_COUNT_KEY, String.valueOf(stage.getValue()), String.valueOf(markerId));
    }

    /**
     * 评卷任务总量
     * 
     * @param blockId
     * @return
     */
    public static String getBlockTotalCountKey(int blockId) {
        return MessageFormat.format(BLOCK_TOTAL_COUNT_KEY, String.valueOf(blockId));
    }

    /**
     * 未评任务总量
     * 
     * @param blockId
     * @return
     */
    // public static String getBlockUnmarkCountKey(LibraryType type, int
    // blockId) {
    // return MessageFormat.format(BLOCK_UNMARK_COUNT_KEY, type.getValue(),
    // blockId);
    // }

    /**
     * 已评任务集合
     * 
     * @param blockId
     * @return
     */
    public static String getBlockMarkedTaskKey(int blockId) {
        return MessageFormat.format(BLOCK_MARKED_TASK_KEY, String.valueOf(blockId));
    }
    
    /**
     * 已领取未给分的评卷任务的领取时间的Key
     * 
     * @param examId
     * @param subjectCode
     * @return
     */
    public static String getCurrentTaskTimeKey(int examId, String subjectCode) {
        return MessageFormat.format(MARK_CURRENT_TASK_TIME_KEY, String.valueOf(examId), subjectCode);
    }
    

    public static String getCurrentTaskKeyByTimeKey(String TimeKey){
        if(!StringUtils.isBlank(TimeKey)){
          return  TimeKey.replace("mark:current:time:key", "mark:current");
        }
        return "";
    }
    
    public static String getCurrentTaskTimePrefix(){
        return MARK_CURRENT_TASK_TIME_KEY;
    }
}
