#include "logger.hpp"
#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
using std::cout;
using std::endl;

#define HOST "127.0.0.1"
#define USER "thj"
#define PASSWD "Abcd1234@"
#define DB "test_connection"
#define PORT 4106
#define FILENAME "./makefile"

void log_test()
{
    LOG(NORMAL, "%s-%d", "hello", 10);
    LOG(DEBUG, "hello world");
}

void mysql_test()
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASSWD, DB, PORT);
    const std::string sql = "insert into user values(null, '王五', 25, '11133334444')";
    mysql_util::mysql_execute(mysql, sql);
    mysql_util::mysql_destroy(mysql);
}

void json_test()
{
    Json::Value root;
    root["姓名"] = "小明";
    root["年龄"] = 18;
    root["成绩"].append(80);  
    root["成绩"].append(90);
    root["成绩"].append(100);

    std::string str;
    json_util::serialize(root, str);
    cout << str << endl;

    Json::Value newroot;
    json_util::deserialize(str, newroot);
    cout << "姓名: " << newroot["姓名"].asString() << endl;
    cout << "年龄: " << newroot["年龄"].asInt() << endl; 
    int size = newroot["成绩"].size();
    for(int i = 0; i < size; i++)
    {
        cout << "成绩: " << newroot["成绩"][i].asFloat() << endl;
    }
}

void string_test()
{
    std::string src = "  abc  dfef fe";
    std::string sep = " ";
    std::vector<std::string> res;
    string_util::split(src, sep, res);
    for(auto s : res)
        cout << s << endl;
    cout << endl;
}

void fileRead_test()
{
    std::string data;
    file_util::read(FILENAME, data);
    cout << data << endl;
}

void db_test()
{
    user_table ut(HOST, USER, PASSWD);
    Json::Value user;
    user["username"] = "zhangsan";
    user["password"] = "123456";

    // register
    // ut.registers(user);
    // user["username"] = "lisi";
    // user["password"] = "123321";
    // ut.registers(user);

    // login
    // ut.login(user);
    // user["password"] = "123321";
    // ut.login(user);

    // win and lose
    ut.win(1);
    ut.lose(2);

    // select
    Json::Value data;
    if(ut.select_by_name("zhangsan", data)) {
        std::string msg;
        json_util::serialize(data, msg);
        LOG(DEBUG, "%s", msg.c_str());
    }
    if(ut.select_by_id(2, data)) {
        std::string msg;
        json_util::serialize(data, msg);
        LOG(DEBUG, "%s", msg.c_str());
    }
}

void online_test()
{
    uint64_t id = 1;
    Json::Value user;
    user_table ut(HOST, USER, PASSWD);
    ut.select_by_id(id, user);
    std::string name = user["username"].asString();

    online_manager om;
    wsserver_t::connection_ptr conn;
    om.enter_game_hall(id, conn);
    if(om.is_in_game_hall(id)) LOG(DEBUG, "%s enter game hall", name.c_str());
    om.enter_game_room(id, conn);
    if(om.is_in_game_room(id)) LOG(DEBUG, "%s enter game room", name.c_str());

    om.exit_game_room(id);
    if(om.is_in_game_room(id) == false) LOG(DEBUG, "%s exit game room", name.c_str());
    om.exit_game_hall(id);
    if(om.is_in_game_hall(id) == false) LOG(DEBUG, "%s exit game hall", name.c_str());
}

void room_test() {
    // 游戏房间
    user_table ut(HOST, USER, PASSWD);
    online_manager om;
    // room r(10, &ut, &om);
    // 游戏房间管理
    room_manager rm(&ut, &om);
    room_ptr rp = rm.create_room(10, 20);
}

void matcher_test() {
    user_table ut(HOST, USER, PASSWD);
    online_manager om;
    room_manager rm(&ut, &om);
    matcher mt(&ut, &om, &rm);
}

int main()
{
    // log_test();
    // mysql_test();
    // json_test();
    // string_test();
    // fileRead_test();
    // db_test();
    // online_test();
    // room_test();
    matcher_test();

    return 0;
}