package cn.estsh.i3plus.pojo.base.codemaker;

/**
 * @Description :
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2019-01-10 13:27
 * @Modify:
 **/
@Deprecated
public class SnowflakeId {
    private long workerId;
    private long datacenterId;
    private long sequence = 0L;

    public SnowflakeId(){
        this.workerId = 1;
        this.datacenterId = 1;
        this.sequence = 0L;
    }

    public SnowflakeId(long workerId, long datacenterId){
        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0",maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0",maxDatacenterId));
        }

        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    private long twepoch = 1288834974657L;

    private long workerIdBits = 5L;
    private long datacenterIdBits = 5L;
    private long maxWorkerId = -1L ^ (-1L << workerIdBits);
    private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
    private long sequenceBits = 12L;

    private long workerIdShift = sequenceBits;
    private long datacenterIdShift = sequenceBits + workerIdBits;
    private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    private long sequenceMask = -1L ^ (-1L << sequenceBits);

    private long lastTimestamp = -1L;

    private long getWorkerId(){
        return workerId;
    }

    private long getDatacenterId(){
        return datacenterId;
    }

    public synchronized long nextId() {
        long timestamp = timeGen();

        if (timestamp < lastTimestamp) {
            System.err.printf("clock is moving backwards.  Rejecting requests until %d.", lastTimestamp);
            throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds",
                    lastTimestamp - timestamp));
        }

        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0;
        }

        lastTimestamp = timestamp;
        return ((timestamp - twepoch) << timestampLeftShift) |
                (datacenterId << datacenterIdShift) |
                (workerId << workerIdShift) |
                sequence;
    }

    private long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    private long timeGen(){
        return System.currentTimeMillis();
    }

    //---------------测试---------------
    public static void main(String[] args) {
        SnowflakeId worker = new SnowflakeId();
        Runnable testRun = new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 10; i++) {
                    System.out.println(i + "=" + worker.nextId());
                }
            }
        };
        Thread thread1 = new Thread(testRun);
        thread1.start();
        Thread thread2 = new Thread(testRun);
        thread2.start();
        Thread thread3 = new Thread(testRun);
        thread3.start();
        Thread thread4 = new Thread(testRun);
        thread4.start();
        Thread thread5 = new Thread(testRun);
        thread5.start();

        System.out.println(new SnowflakeId().nextId());

        for (int i = 0; i < 10; i++) {
            //System.out.println(worker.nextId());
        }
    }
}
