/*
Crafter.Build
Copyright (C) 2025 Catcrafts®
Catcrafts.net

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
*/

module;

#include <map>
#include <functional>
#include <vector>
#include <algorithm>

export module Crafter.Event;

namespace Crafter {
    export template <class T>
    class Event;

    export template<>
    class Event<void>;

    export template<typename T>
    class EventListener {
    public:
        int priority;
        EventListener();
        void SetEvent(Event<T>* eventToListenTo, std::function<void(T)> function, int priority = 0);
        EventListener(Event<T>* eventToListenTo, std::function<void(T)> function, int priority = 0);
        ~EventListener();
        Event<T>* eventToListenTo;
        std::function<void(T)> function;
    };

    export template<>
    class EventListener<void> {
    public:
        int priority;
        EventListener();
        void SetEvent(Event<void>* eventToListenTo, std::function<void()> function, int priority = 0);
        EventListener(Event<void>* eventToListenTo, std::function<void()> function, int priority = 0);
        ~EventListener();
        Event<void>* eventToListenTo;
        std::function<void()> function;
    };

    export template<typename T>
    class Event {
    private:
        std::map<int, std::vector<EventListener<T>*>> listeners;
    public:
        ~Event() {
            for (const auto& listenerSlice : listeners) {
                const std::vector<EventListener<T>*> slice = listenerSlice.second;
                for (const auto& listener : slice) {
                    listener->eventToListenTo = nullptr;
                }
            }
        }

        void AddListener(EventListener<T>* listener, int priority = 0) {
            if (listeners.contains(priority)) {
                listeners[priority].push_back(listener);
            }
            else {
                listeners[priority] = std::vector<EventListener<T>*>{ listener };
            }
        }

        void RemoveListener(EventListener<T>* listener) {
            listeners[listener->priority].erase(std::remove(listeners[listener->priority].begin(), listeners[listener->priority].end(), listener), listeners[listener->priority].end());
        }

        void Invoke(T data) {
            for (const auto& listenerSlice : listeners) {
                const std::vector<EventListener<T>*> slice = listenerSlice.second;
                for (const auto& listener : slice) {
                    listener->function(data);
                }
            }
        }
    };

    template<typename T> void EventListener<T>::SetEvent(Event<T>* eventToListenTo, std::function<void(T)> listener, int priority)
    {
        this->priority = priority;
        if (this->eventToListenTo != nullptr) {
            eventToListenTo->RemoveListener(this);
        }
        this->eventToListenTo = eventToListenTo;
        eventToListenTo->AddListener(this);
        this->function = listener;
    }

    template<typename T> EventListener<T>::EventListener(Event<T>* eventToListenTo, std::function<void(T)> listener, int priority) : priority(priority), eventToListenTo(eventToListenTo), function(listener) {
        eventToListenTo->AddListener(this);
    }

    template<typename T> EventListener<T>::~EventListener() {
        if (eventToListenTo != nullptr) {
            eventToListenTo->RemoveListener(this);
        }
    }

    template<typename T> EventListener<T>::EventListener() {
        eventToListenTo = nullptr;
    }

    export template<>
        class Event<void> {
        private:
            std::map<int, std::vector<EventListener<void>*>> listeners;

        public:
            ~Event() {
                for (const auto& listenerSlice : listeners) {
                    const std::vector<EventListener<void>*> slice = listenerSlice.second;
                    for (const auto& listener : slice) {
                        listener->eventToListenTo = nullptr;
                    }
                }
            }

            void AddListener(EventListener<void>* listener, int priority = 0) {
                if (listeners.contains(priority)) {
                    listeners[priority].push_back(listener);
                }
                else {
                    listeners[priority] = std::vector<EventListener<void>*>{ listener };
                }
            }

            void RemoveListener(EventListener<void>* listener) {
                listeners[listener->priority].erase(std::remove(listeners[listener->priority].begin(), listeners[listener->priority].end(), listener), listeners[listener->priority].end());
            }

            void Invoke() {
                for (const auto& listenerSlice : listeners) {
                    const std::vector<EventListener<void>*> slice = listenerSlice.second;
                    for (const auto& listener : slice) {
                        listener->function();
                    }
                }
            }
    };

    void EventListener<void>::SetEvent(Event<void>* eventToListenTo, std::function<void()> listener, int priority)
    {
        this->priority = priority;
        if (this->eventToListenTo != nullptr) {
            eventToListenTo->RemoveListener(this);
        }
        this->eventToListenTo = eventToListenTo;
        eventToListenTo->AddListener(this);
        this->function = listener;
    }

    EventListener<void>::EventListener(Event<void>* eventToListenTo, std::function<void()> listener, int priority) : priority(priority), eventToListenTo(eventToListenTo), function(listener) {
        eventToListenTo->AddListener(this);
    }

    EventListener<void>::~EventListener() {
        if (eventToListenTo != nullptr) {
            eventToListenTo->RemoveListener(this);
        }
    }

    EventListener<void>::EventListener() {
        eventToListenTo = nullptr;
    }
}