package com.fs.utils;

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

public class TwelveDigitSnowflake {
    private final long START_TIMESTAMP = 1672502400000L;
    private final long MACHINE_BIT = 5;
    private final long SEQUENCE_BIT = 7;
    private final long TIMESTAMP_BIT = 30;
    private final long MAX_MACHINE_NUM = ~(-1L << MACHINE_BIT);
    private final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT);

    private final long MACHINE_LEFT = SEQUENCE_BIT;
    private final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT;

    private long machineId;
    private long sequence = 0L;
    private long lastTimeStamp = -1L;

    // 构造函数，传入机器ID
    public TwelveDigitSnowflake(long machineId) {
        if (machineId > MAX_MACHINE_NUM || machineId < 0) {
            throw new IllegalArgumentException("机器ID超出范围");
        }
        this.machineId = machineId;
    }

    // 生成下一个ID
    public synchronized long nextId() {
        long currentTimeStamp = getCurrentTimeStamp();

        // 处理时钟回拨
        if (currentTimeStamp < lastTimeStamp) {
            long offset = lastTimeStamp - currentTimeStamp;
            if (offset <= 5) {
                try {
                    wait(offset);
                    currentTimeStamp = getCurrentTimeStamp();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else { // 超过5毫秒回拨，抛出异常
                throw new RuntimeException("时钟回拨异常，无法生成ID");
            }
        }

        if (currentTimeStamp == lastTimeStamp) {
            sequence = (sequence + 1) & MAX_SEQUENCE;
            if (sequence == 0) {
                currentTimeStamp = getNextMill();
            }
        } else {
            sequence = 0L;
        }

        lastTimeStamp = currentTimeStamp;

        // 组合ID：时间戳 + 机器ID + 序列号
        long id = ((currentTimeStamp - START_TIMESTAMP) << TIMESTAMP_LEFT)
                | (machineId << MACHINE_LEFT)
                | sequence;
        return id;
    }

    private long getNextMill() {
        long mill = getCurrentTimeStamp();
        while (mill <= lastTimeStamp) {
            mill = getCurrentTimeStamp();
        }
        return mill;
    }

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

    public static void main(String[] args) {
        TwelveDigitSnowflake snowflake = new TwelveDigitSnowflake(1);
        Map<String,Long> check=new HashMap<>();
        for (int i = 0; i < 10000000; i++) {
            long id = snowflake.nextId();
            if(check.containsKey(String.valueOf(id))){
                throw new RuntimeException("雪花ID重复啦！！！！！！！");
            }
            check.put(String.valueOf(id),0L);
        }
    }
}
