#include "tile/net/internal/http_engine.h"

#include "tile/base/string.h"
#include "tile/base/thread/latch.h"
#include "tile/rpc/http_handler.h"

#include "gtest/gtest.h"

namespace tile {
namespace internal {
std::string
GetHeader(const std::vector<std::string> &headers, const std::string &key)
{
    for (auto &&e : headers) {
        TILE_LOG_INFO(e);
        if (StartsWithIgnoreCase(e, key)) { return Trim(e.substr(key.size() + 1)); }
    }
    return "";
}

// class HttpEngineTest : public ::testing::Test {
// public:
//   void SetUp() override {
//     server_.AddProtocol("http");
//     server_.AddHttpHandler(
//         "/basic", tile::NewHttpGetHandler([](auto &&r, auto &&w, auto &&c) {
//           w->set_body("hello world");
//           w->headers()->Append("custom_header", "header_value");
//           w->set_status(tile::HttpStatus::OK);
//         }));
//     server_.AddHttpHandler(
//         "/timeout", tile::NewHttpGetHandler([](auto &&r, auto &&w, auto &&c)
//         {
//           this_fiber::SleepFor(200ms);
//           w->set_status(tile::HttpStatus::OK);
//         }));
//     server_.AddHttpHandler(
//         "/post", tile::NewHttpPostHandler([](auto &&r, auto &&w, auto &&c) {
//           if (*r.body() == "local buffer") {
//             w->set_status(tile::HttpStatus::OK);
//           } else {
//             w->set_status(tile::HttpStatus::BadRequest);
//           }
//         }));
//     server_.AddHttpHandler(
//         "/put", tile::NewHttpPutHandler([](auto &&r, auto &&w, auto &&c) {
//           if (*r.body() == "local buffer") {
//             w->set_status(tile::HttpStatus::OK);
//           } else {
//             w->set_status(tile::HttpStatus::BadRequest);
//           }
//         }));
//
//     auto &&endpoint = testing::PickAvailableEndpoint();
//     server_.ListenOn(endpoint);
//     site_url_ = "http://" + endpoint.ToString() + "/";
//     tile_CHECK(server_.Start());
//     auto endpoint_str = endpoint.ToString();
//     port_ =
//         *TryParse<int>(endpoint_str.substr(endpoint_str.find_last_of(":") +
//         1));
//   }
//
//   Server server_;
//   std::string site_url_;
//   int port_;
// };

// <html>
// <meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
// </html>
class HttpEngineTest : public ::testing::Test {
public:
    void SetUp() override { site_url_ = "http://example.com/"; }

    std::string site_url_;
};

TEST_F(HttpEngineTest, Basic)
{
    GTEST_SKIP_("Fix me.");
    HttpTask t;
    t.SetMethod(HttpMethod::Get);
    t.SetUrl(site_url_);
    t.SetTimeout(std::chrono::seconds(1));
    // t.SetUserAgent("curl/8.6.0");
    Latch l(1);
    HttpEngine::Instance()->StartTask(std::move(t), [&](std::expected<HttpTaskCompletion, Status> resp) {
        EXPECT_TRUE(resp);
        EXPECT_EQ(GetHeader(*resp->headers(), "Content-Type"), "text/html");
        EXPECT_EQ(GetHeader(*resp->headers(), "Connection"), "Keep-Alive");
        EXPECT_EQ(HttpStatus::OK, resp->status());
        EXPECT_EQ(HttpVersion::V_1_1, resp->version());
        l.CountDown();
    });
    l.Wait();
}

}// namespace internal
}// namespace tile
