#include "of_util.h"
#include <chrono>
#include <iomanip>
#include <sstream>

#ifdef _WIN32
#include <windows.h>
#endif

namespace ofen {
void CMutBuffer::push(const char* data, int len)
{
    std::lock_guard<std::mutex> lock(mutex_);
    buffer_.insert(buffer_.end(), data, data + len);
}
int CMutBuffer::index_of(const char* data, int len, int start_pos)
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
        return -1;
    }
    auto it = std::search(buffer_.begin() + start_pos, buffer_.end(), data, data + len);
    if (it != buffer_.end()) {
        return std::distance(buffer_.begin(), it);
    }
    return -1;
}
void CMutBuffer::remove_of(int start_pos, int len)
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
        return;
    }
#ifdef _WIN32
#if defined(__MINGW32__) || defined(__MINGW64__)
    auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
#else
    auto end_pos = min(start_pos + len, static_cast<int>(buffer_.size()));
#endif
#else
    auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
#endif
    buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos);
}

const char* CMutBuffer::get_data() const
{
    return buffer_.data();
}

int CMutBuffer::get_len() const
{
    return static_cast<int>(buffer_.size());
}

void CMutBuffer::clear()
{
    std::lock_guard<std::mutex> lock(mutex_);
    buffer_.clear();
}
OfUtil::OfUtil()
{
}
OfUtil::~OfUtil()
{
}
ofString OfUtil::now_time()
{
    auto now = std::chrono::system_clock::now();
    auto time_t_now = std::chrono::system_clock::to_time_t(now);
    std::tm tm_now;
#if defined(_WIN32) || defined(_WIN64)
    localtime_s(&tm_now, &time_t_now);
#else
    localtime_r(&time_t_now, &tm_now);
#endif
#ifdef UNICODE_OFSTR
    std::wostringstream oss;
    oss << std::put_time(&tm_now, ofT("%Y-%m-%d %H:%M:%S"));
    return oss.str();
#else
    std::ostringstream oss;
    oss << std::put_time(&tm_now, ofT("%Y-%m-%d %H:%M:%S"));
    return oss.str();
#endif
}

#ifdef _WIN32
std::string CCodec::u8ToGBK(const std::string& str)
{
    int wideCharLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
    if (wideCharLen <= 0) {
        return "";
    }
    std::wstring wideStr(wideCharLen, L'\0');
    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wideStr[0], wideCharLen);
    int gbkLen = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
    if (gbkLen <= 0) {
        return "";
    }
    std::string gbkStr(gbkLen, '\0');
    WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &gbkStr[0], gbkLen, nullptr, nullptr);

    gbkStr.resize(gbkLen - 1);
    return gbkStr;
}
std::string CCodec::GBKTou8(const std::string& str)
{
    int wideCharLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
    if (wideCharLen <= 0) {
        return "";
    }
    std::wstring wideStr(wideCharLen, L'\0');
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wideStr[0], wideCharLen);

    int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
    if (utf8Len <= 0) {
        return "";
    }
    std::string utf8Str(utf8Len, '\0');
    WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Len, nullptr, nullptr);

    utf8Str.resize(utf8Len - 1);
    return utf8Str;
}
#endif
CThreadSleep::CThreadSleep()
{
    is_stop_sleep_ = false;
    timeout_ = 10;
}
void CThreadSleep::sleep(int milsec)
{
    int sleep_time = timeout_;
    if (milsec > 0)
        sleep_time = milsec;

    is_stop_sleep_ = false;
    std::unique_lock<std::mutex> lock(mutex_);
    std::chrono::system_clock::time_point nowtime = std::chrono::system_clock::now();
    if (cv_.wait_until(lock, nowtime + (std::chrono::milliseconds(sleep_time)), [this] { return get_status(); })) {
        // 手动
    } else {
        // 超时
    }
}
void CThreadSleep::contiune()
{
    is_stop_sleep_ = true;
    cv_.notify_all();
}
void CThreadSleep::set_timeout(int milsec)
{
    if (milsec <= 0)
        timeout_ = default_timeout_;
    else
        timeout_ = milsec;
}
bool CThreadSleep::get_status() const
{
    return is_stop_sleep_;
}
}   // namespace ofen