#pragma once
#include "abstract_tcp_connect.hpp"
#include "reactor.hpp"
#include "server_tcp_connect.hpp"
#include <mutex>
#include <thread>
#include <unordered_map>
#include <memory>
#include <vector>

namespace tinyrpc {
    class TcpServer;
    class IOThread {
        friend  class IOThreadPool;
    public:
        void addClient(TcpServer* ser, int fd);
        static IOThread* getThisIoThread();
        // void removeFd(int fd);
        Reactor* getReactor() {return m_reactor;}
    private:
        IOThread();
        ~IOThread();
        void mainFunc();     
    private:
        std::unordered_map<int, std::shared_ptr<ServerTcpConnection>> m_clients;
        std::thread m_thread;
        Reactor* m_reactor{nullptr};
    };


    class IOThreadPool {

    public:
        IOThreadPool(int size);
        IOThread* getIOThread();
        ~IOThreadPool();
    private:
        std::mutex m_mtx;
        int m_idx {-1};
        const std::vector<IOThread*> m_IOThreads{};
    };

}