package com.heyu.api.data.utils;

import com.heyu.api.data.entity.api.ApiPostCodeEntity;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class Node implements Delayed {


    private ApiPostCodeEntity apiPostCodeEntity;
    private Long time;

    public Node() {

    }


    public Node(ApiPostCodeEntity apiPostCodeEntity, Long time ) {
        this.apiPostCodeEntity = apiPostCodeEntity;
        this.time = time;
    }


    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.nanoTime();
    }


    @Override
    public int compareTo(Delayed o) {
        if (this.time == null) {
            if (((Node) o).getTime() == null) { // this.time==null && o.time == null  ->  return 0
                return 0;
            }
            return -1;          // this.time == null && o.time !=null ->  return -1
        }
        if (((Node) o).getTime() == null) {            // this.time  !=null && o.time == null -> return 1
            return 1;
        }
        return (int) (this.time - ((Node) o).getTime());  // return this.time - o.time
    }


    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }


}
