package com.yaohun.mcbot.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;

public class CDTimeAPI {

    private static HashSet<CDData> cdData = new HashSet<>();

    public static CDData getCDData(long tencentQQ) {
        for (CDData cDData : cdData) {
            if (cDData.getTencentQQ() == tencentQQ) {
                return cDData;
            }
        }
        CDData data = new CDData(tencentQQ);
        cdData.add(data);
        return data;
    }

    public static void setPlayerCD(long tencentQQ, String key, long mills) {
        CDData data = getCDData(tencentQQ);
        long now = System.currentTimeMillis();
        data.setCD(key, now + mills);
    }

    public static void addPlayerCD(long tencentQQ, String key, long mills) {
        if(isCD(tencentQQ,key)){
            setPlayerCD(tencentQQ,key,getCD(tencentQQ,key) + mills);
        } else {
            setPlayerCD(tencentQQ,key,mills);
        }
    }

    public static long getCD(long tencentQQ, String key) {
        CDData data = getCDData(tencentQQ);
        long now = System.currentTimeMillis();
        return data.getCD(key) - now;
    }

    public static boolean isCD(long tencentQQ, String key) {
        return (getCD(tencentQQ, key) >= 0L);
    }

    public static class CDData{
        private final long tencentQQ;

        private final HashMap<String, Long> cdTime;

        public CDData(long tencentQQ) {
            this.tencentQQ = tencentQQ;
            this.cdTime = new HashMap<>();
        }

        public long getTencentQQ() {
            return tencentQQ;
        }

        public void setCD(String key, long time) {
            this.cdTime.put(key, time);
        }

        public long getCD(String key) {
            return this.cdTime.getOrDefault(key,-1L);
        }

        public HashMap<String, Long> getCdTime() {
            return cdTime;
        }
    }
}
