package com.yaohun.mcbot.manage;

import com.yaohun.mcbot.McBot;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CacheManager {

    public static HashMap<String,CodeData> codeDataMap = new HashMap<>();
    public static HashMap<String ,String> randomCodeMap = new HashMap<>();

    public static void setRandomCodeData(String userName,String code){
        randomCodeMap.put(userName,code);
    }

    public static String getRandomCodeData(String userName){
        if(randomCodeMap.containsKey(userName)){
            return randomCodeMap.get(userName);
        }
        return null;
    }

    public static void setCodeDataMap(String randomCode,CacheManager.CodeData codeData){
        codeDataMap.put(randomCode,codeData);
    }

    public static CodeData getCodeData(String randomCode){
        if(codeDataMap.containsKey(randomCode)){
            return codeDataMap.get(randomCode);
        }
        return null;
    }

    public static void cleanUpTheCache(String playerName,String randomCode){
        codeDataMap.remove(playerName);
        randomCodeMap.remove(randomCode);
    }

    public static void checkingCleanUpData(){
        if(codeDataMap.isEmpty()){
            return;
        }
        codeDataMap.clear();
        McBot.inst().getLogger().info("[McBot] 定时清理缓存池...");
    }

    public static class CodeData {
        private String randomCode;
        private String userName;
        private String tencentQQ;
        private long recordingTime;

        public CodeData(String userName, String randomCode, String tencentQQ) {
            this.userName = userName;
            this.randomCode = randomCode;
            this.tencentQQ = tencentQQ;
            this.recordingTime = System.currentTimeMillis();
        }

        public String getUserName() {
            return userName;
        }

        public String getRandomCode() {
            return randomCode;
        }

        public String getTencentQQ() {
            return tencentQQ;
        }

        public boolean isRecordingTime() {
            long nowTime = System.currentTimeMillis();
            if(nowTime - recordingTime > (1000 * 120)){
                return true;
            }
            return false;
        }
    }
}
