#include <websocketpp/config/asio_no_tls.hpp>

#include <websocketpp/server.hpp>
#include "../../nlohmann/json.hpp"

#include <iostream>
#include <set>
#include <map>

/*#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>*/
#include <websocketpp/common/thread.hpp>
using json = nlohmann::json;

typedef websocketpp::server<websocketpp::config::asio> server;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;

using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::lock_guard;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;

/* on_open insert connection_hdl into channel
 * on_close remove connection_hdl from channel
 * on_message queue send to all channels
 */

enum action_type {
    SUBSCRIBE,
    UNSUBSCRIBE,
    MESSAGE
};

struct action {
    action(action_type t, connection_hdl h) : type(t), hdl(h) {}
    action(action_type t, connection_hdl h, server::message_ptr m)
      : type(t), hdl(h), msg(m) {}

    action_type type;
    websocketpp::connection_hdl hdl;
    server::message_ptr msg;
};

class broadcast_server {
public:
    broadcast_server() {
        // Initialize Asio Transport
        m_server.init_asio();

        // Register handler callbacks
        m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
        m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
        m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
    }

    void run(uint16_t port) {
        // listen on specified port
        m_server.listen(port);

        // Start the server accept loop
        m_server.start_accept();

        // Start the ASIO io_service run loop
        try {
            m_server.run();
        } catch (const std::exception & e) {
            std::cout << e.what() << std::endl;
        }
    }

    void on_open(connection_hdl hdl) {
        {
            lock_guard<mutex> guard(m_action_lock);
            //std::cout << "on_open" << std::endl;
            m_actions.push(action(SUBSCRIBE,hdl));
        }
        m_action_cond.notify_one();
    }

    void on_close(connection_hdl hdl) {
        {
            lock_guard<mutex> guard(m_action_lock);
            //std::cout << "on_close" << std::endl;
            m_actions.push(action(UNSUBSCRIBE,hdl));
        }
        m_action_cond.notify_one();
    }

    void on_message(connection_hdl hdl, server::message_ptr msg) {
        // queue message up for sending by processing thread
        {
            lock_guard<mutex> guard(m_action_lock);
            //std::cout << "on_message" << std::endl;
            m_actions.push(action(MESSAGE,hdl,msg));
        }
        m_action_cond.notify_one();
    }

    void process_messages() {
        while(1) {
            unique_lock<mutex> lock(m_action_lock);

            while(m_actions.empty()) {
                m_action_cond.wait(lock);
            }

            action a = m_actions.front();
            m_actions.pop();

            lock.unlock();

            if (a.type == SUBSCRIBE) {
                lock_guard<mutex> guard(m_connection_lock);
                //m_connections.insert(a.hdl);

				m_NumOfClient = m_NumOfClient + 1;	 // clean
				if (m_NumOfClient > 2) { // 若房間大於2人
					m_iKey = m_iKey + 1; // 開新房
					m_NumOfClient = 0;
				}
				m_HubTo_list[m_iKey].insert(a.hdl);
				
            } else if (a.type == UNSUBSCRIBE) {
                lock_guard<mutex> guard(m_connection_lock);
                m_connections.erase(a.hdl);
            } else if (a.type == MESSAGE) {
                lock_guard<mutex> guard(m_connection_lock);
				m_person["neame"] = "NULL";
				m_person["age"] = 20;
                //con_list::iterator it;
					int index = 0;
					auto folder = a.msg->get_payload();
					std::cout << "folder ==" << folder << std::endl;
					//folder.erase(std::remove(folder.begin(), folder.end(), '\n'), folder.end());
					//folder.erase(std::remove(folder.begin(), folder.end(), '\t'), folder.end());
					//folder.erase(std::remove(folder.begin(), folder.end(), ' '), folder.end());
					std::replace(folder.begin(), folder.end(), '/n', '//n');
					//auto tt = websocketpp::frame::opcode::TEXT;
					json j_from_cbor = folder;
					//std::cin >> j_from_cbor;
					//auto keep = it.value("_id", 0);
					//std::cout << "kepp = " << keep << std::endl;
					//auto keep["_id"] = j_from_cbor["_id"];
					for (json::iterator it = j_from_cbor.begin(); it != j_from_cbor.end(); ++it) {
						std::cout << it.key() << " : " << it.value() << "\n";
					}
					
					try {
						//json::parse(it);
					}
					catch (json::parse_error e) {
						// output exception information
						std::cout << "message: " << e.what() << '\n'
							<< "exception id: " << e.id << '\n'
							<< "byte position of error: " << e.byte << std::endl;

					}
					std::cout << "---------------------START--------------------------" << std::endl;
					std::cout << j_from_cbor << std::endl;
					std::cout << "---------------------END--------FFOLDER--------------------------" << std::endl;



					con_list set = m_HubTo_list.at(index);
					for (con_list::iterator getit = set.begin(); getit != set.end(); ++getit) {
						m_server.send(*getit, a.msg);
					}		
				
            } else {
                // undefined.
            }
        }
    }
private:
    typedef std::set<connection_hdl,std::owner_less<connection_hdl> > con_list;
	typedef std::map<int , con_list> HubToCon_list; // int == NameOfChatroom , con_list == list of client in this room

    server m_server;
    con_list m_connections;
	//
	HubToCon_list m_HubTo_list ; // 建立一個多通道(房) 聊天
	int m_iKey = 0;
	int m_NumOfClient = 0;
	//
    std::queue<action> m_actions;

    mutex m_action_lock;
    mutex m_connection_lock;
    condition_variable m_action_cond;
	json m_person;
};

int main() {
    try {
    broadcast_server server_instance;

    // Start a thread to run the processing loop
    thread t(bind(&broadcast_server::process_messages,&server_instance));

    // Run the asio loop with the main thread
    server_instance.run(9002);

    t.join();

    } catch (websocketpp::exception const & e) {
        std::cout << e.what() << std::endl;
    }
}
