package com.yaohun.demonskills.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 void setPlayerCD(UUID uuid, String key, long mills) {
        CDData data = getCDData(uuid);
        long now = System.currentTimeMillis();
        data.setCD(key, now + mills);
    }

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

    public static boolean isCD(UUID uuid, String key) {
        return (getCD(uuid, key) > 0L);
    }

    public static CDData getCDData(UUID uuid) {
        for (CDData cDData : cdData) {
            if (cDData.getUuid().equals(uuid))
                return cDData;
        }
        CDData data = new CDData(uuid);
        cdData.add(data);
        return data;
    }

    public static class CDData{
        private final UUID uuid;

        private final HashMap<String, Long> cdTime;

        public CDData(UUID uuid) {
            this.uuid = uuid;
            this.cdTime = new HashMap<>();
        }

        public UUID getUuid() {
            return this.uuid;
        }

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

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