#include "utils.h"

#include <flutter_windows.h>
#include <io.h>
#include <stdio.h>
#include <windows.h>

#include <iostream>

void CreateAndAttachConsole()
{
  if (::AllocConsole())
  {
    FILE *unused;
    if (freopen_s(&unused, "CONOUT$", "w", stdout))
    {
      _dup2(_fileno(stdout), 1);
    }
    if (freopen_s(&unused, "CONOUT$", "w", stderr))
    {
      _dup2(_fileno(stdout), 2);
    }
    std::ios::sync_with_stdio();
    FlutterDesktopResyncOutputStreams();
  }
}

std::vector<std::string> GetCommandLineArguments()
{
  // Convert the UTF-16 command line arguments to UTF-8 for the Engine to use.
  int argc;
  wchar_t **argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
  if (argv == nullptr)
  {
    return std::vector<std::string>();
  }

  std::vector<std::string> command_line_arguments;

  // Skip the first argument as it's the binary name.
  for (int i = 1; i < argc; i++)
  {
    command_line_arguments.push_back(Utf8FromUtf16(argv[i]));
  }

  ::LocalFree(argv);

  return command_line_arguments;
}

std::string Utf8FromUtf16(const wchar_t *utf16_string)
{
  if (utf16_string == nullptr)
  {
    return std::string();
  }
  int target_length = ::WideCharToMultiByte(
      CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
      -1, nullptr, 0, nullptr, nullptr);
  std::string utf8_string;
  if (target_length == 0 || target_length > utf8_string.max_size())
  {
    return utf8_string;
  }
  utf8_string.resize(target_length);
  int converted_length = ::WideCharToMultiByte(
      CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
      -1, utf8_string.data(),
      target_length, nullptr, nullptr);
  if (converted_length == 0)
  {
    return std::string();
  }
  return utf8_string;
}

std::wstring Utf16FromUtf8(const char *utf8_string)
{
  if (utf8_string == nullptr)
  {
    return std::wstring();
  }
  int target_length = ::MultiByteToWideChar(CP_UTF8, 0, utf8_string, static_cast<int>(strlen(utf8_string)), nullptr, 0);
  std::wstring utf16_string;
  if (target_length == 0 || target_length > utf16_string.max_size())
  {
    return utf16_string;
  }
  utf16_string.resize(target_length);
  int converted_length = ::MultiByteToWideChar(CP_UTF8, 0, utf8_string, static_cast<int>(strlen(utf8_string)), utf16_string.data(), target_length);
  if (converted_length == 0)
  {
    return std::wstring();
  }
  return utf16_string;
}

std::string utf8_encode(const std::wstring &wstr)
{
  if (wstr.empty())
    return std::string();
  int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
  std::string strTo(size_needed, 0);
  WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
  return strTo;
}

std::wstring utf8_decode(const std::string &str)
{
  if (str.empty())
    return std::wstring();
  int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
  std::wstring wstrTo(size_needed, 0);
  MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
  return wstrTo;
}

bool _isntspace(const char &ch)
{
  return !isspace(ch);
}

const std::string ltrim(const std::string &s)
{
  std::string::const_iterator iter = find_if(s.begin(), s.end(), _isntspace);
  return std::string(iter, s.end());
}

const std::string rtrim(const std::string &s)
{
  std::string::const_iterator iter = find_if(s.rbegin(), s.rend(), _isntspace).base();
  return std::string(s.begin(), iter);
}

const std::string trim(const std::string &s)
{
  std::string::const_iterator iter1 = find_if(s.begin(), s.end(), _isntspace);
  std::string::const_iterator iter2 = find_if(s.rbegin(), s.rend(), _isntspace).base();

  return iter1 < iter2 ? std::string(iter1, iter2) : std::string("");
}
