/* events
 * Copyright (c) 2016-2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "events/EventQueue.h"
#include "events/mbed_events.h"

using mbed::Callback;

namespace events {

EventQueue::EventQueue(unsigned event_size, unsigned char *event_pointer)
{
    if (event_size == 0) {
        // As static queue (EventQueue(0)) won't perform any access to its data buffer
        // we can pass (0, NULL)
        equeue_create_inplace(&_equeue, 0, NULL);
    } else {
        if (!event_pointer) {
            equeue_create(&_equeue, event_size);
        } else {
            equeue_create_inplace(&_equeue, event_size, event_pointer);
        }
    }
}

EventQueue::~EventQueue()
{
    equeue_destroy(&_equeue);
}

void EventQueue::dispatch(int ms)
{
    return equeue_dispatch(&_equeue, ms);
}

void EventQueue::break_dispatch()
{
    return equeue_break(&_equeue);
}

unsigned EventQueue::tick()
{
    return equeue_tick();
}

bool EventQueue::cancel(int id)
{
    return equeue_cancel(&_equeue, id);
}

int EventQueue::time_left(int id)
{
    return equeue_timeleft(&_equeue, id);
}

void EventQueue::background(Callback<void(int)> update)
{
    _update = update;

    if (_update) {
        equeue_background(&_equeue, &Callback<void(int)>::thunk, &_update);
    } else {
        equeue_background(&_equeue, 0, 0);
    }
}

int EventQueue::chain(EventQueue *target)
{
    if (target) {
        return equeue_chain(&_equeue, &target->_equeue);
    } else {
        return equeue_chain(&_equeue, 0);
    }
}
}
