/**
 * string_utils.cpp: Useful functions for manipulating strings
 *
 * Every function in here *should* have a working wstring equivalent.
**/
#include <vector>
#include <string>
#include <codecvt>
#include <locale>
#include "string_utils.h"

/* It's actually pretty insane how the standard library still doesn't
   have a function for this. Look at how simple this is. */
std::string StringUtils::Implode(const std::vector<std::string>& vector,
                                 const std::string& delimiter) {
	std::string out = "";
	for (int i = 0; i < vector.size(); i++) {
		out.append(vector.at(i));
		if (i < vector.size()-1)
			out.append(delimiter);
	}
	return out;
}

std::wstring StringUtils::Implode(const std::vector<std::wstring>& vector,
                                  const std::wstring& delimiter) {
	std::wstring out = L"";
	for (int i = 0; i < vector.size(); i++) {
		out.append(vector.at(i));
		if (i < vector.size()-1)
			out.append(delimiter);
	}
	return out;
}

std::string StringUtils::WstrToUtf8(const std::wstring& string) {
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
	return convert.to_bytes(string);
}

std::wstring StringUtils::Utf8ToWstr(const std::string& string) {
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
	return convert.from_bytes(string);
}

std::string StringUtils::ReplaceAll(const std::string& string,
                                    const std::string& find,
									const std::string& replace) {
    std::string result;
    size_t pos, find_len = find.size(), from = 0;
    while ((pos=string.find(find,from)) != std::string::npos) {
        result.append(string, from, pos - from);
        result.append(replace);
        from = pos + find_len;
    }
    result.append(string, from, std::string::npos);
    return result;
}

std::wstring StringUtils::ReplaceAll(const std::wstring& string,
                                     const std::wstring& find,
									 const std::wstring& replace) {
    std::wstring result;
    size_t pos, find_len = find.size(), from = 0;
    while ((pos=string.find(find,from)) != std::wstring::npos) {
        result.append(string, from, pos - from);
        result.append(replace);
        from = pos + find_len;
    }
    result.append(string, from, std::wstring::npos);
    return result;
}

/* this function probably fucks your RAM but whatevs */
std::string StringUtils::SanitizeLineEndings(const std::string& string) {
	std::string result(string);
	result = ReplaceAll(result, "\r\n",   "\n");
	result = ReplaceAll(result, "<br>",   "\n");
	result = ReplaceAll(result, "\n\n\n", "\n\n");
	return result;
}

std::wstring StringUtils::SanitizeLineEndings(const std::wstring& string) {
	std::wstring result(string);
	result = ReplaceAll(result, L"\r\n",   L"\n");
	result = ReplaceAll(result, L"<br>",   L"\n");
	result = ReplaceAll(result, L"\n\n\n", L"\n\n");
	return result;
}

std::string StringUtils::RemoveHtmlTags(const std::string& string) {
	std::string html(string);
    while (html.find("<") != std::string::npos)
    {
        auto startpos = html.find("<");
        auto endpos = html.find(">") + 1;

        if (endpos != std::string::npos)
        {
            html.erase(startpos, endpos - startpos);
        }
    }
	return html;
}

std::wstring StringUtils::RemoveHtmlTags(const std::wstring& string) {
	std::wstring html(string);
    while (html.find(L"<") != std::wstring::npos)
    {
        auto startpos = html.find(L"<");
        auto endpos = html.find(L">") + 1;

        if (endpos != std::wstring::npos)
        {
            html.erase(startpos, endpos - startpos);
        }
    }
	return html;
}

std::string StringUtils::TextifySynopsis(const std::string& string) {
	std::string result = SanitizeLineEndings(string);
	result = RemoveHtmlTags(string);
	return result;
}

std::wstring StringUtils::TextifySynopsis(const std::wstring& string) {
	std::wstring result = SanitizeLineEndings(string);
	result = RemoveHtmlTags(string);
	return result;
}
