package com.tsi.app.common.xnet;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class CircularBlockingQueue<E> implements BlockingQueue<E> {

    private final ReentrantLock lock;
    private final Condition notEmpty;
    private final ArrayDeque<E> queue;
    private final int maxSize;

    public CircularBlockingQueue(int queueSize) {
        this.queue = new ArrayDeque<>(queueSize);
        this.maxSize = queueSize;

        this.lock =  new ReentrantLock();
        this.notEmpty = this.lock.newCondition();
    }

    @Override
    public boolean offer(E e) {
        Objects.requireNonNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (this.queue.size() == this.maxSize) {
                return false;
            }
            this.queue.add(e);
            this.notEmpty.signal();
        } finally {
            lock.unlock();
        }
        return true;
    }

    public E offer_remove(E e) {
        Objects.requireNonNull(e);
        E discard = null;
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (this.queue.size() == this.maxSize) {
                discard = this.queue.remove();
            }
            this.queue.add(e);
            this.notEmpty.signal();
        } finally {
            lock.unlock();
        }
        return discard;
    }

    @Override
    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (this.queue.isEmpty()) {
                this.notEmpty.await();
            }
            return this.queue.poll();
        } finally {
            lock.unlock();
        }
    }

    @Override
    public boolean add(E e) {
        return false;
    }

    @Override
    public E remove() {
        return null;
    }

    @Override
    public E poll() {
        return null;
    }

    @Override
    public E element() {
        return null;
    }

    @Override
    public E peek() {
        return null;
    }

    @Override
    public void put(E e) throws InterruptedException {

    }

    @Override
    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        return null;
    }

    @Override
    public int remainingCapacity() {
        return 0;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public int drainTo(Collection<? super E> c) {
        return 0;
    }

    @Override
    public int drainTo(Collection<? super E> c, int maxElements) {
        return 0;
    }
}
