#include <Windows.h>
#include <iostream>
#include <string>
#include "win.h"
#include "func.h"

bool put_data_on_clipboard(const char* text) {
	size_t len = strlen(text);
	if (strnlen_s(text, MAX_STRING_SIZE) == 0) {
		printf_s("Text is empty\n");
		return false;
	}
	// Open the clipboard
	if (!OpenClipboard(nullptr)) {
		printf_s("Failed to open clipboard");
		return false;
	}
	// Allocate global memory for the text
	HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1);
	if (!hMem) {
		CloseClipboard();
		printf_s("Failed to allocate memory for text");
		return false;
	}
	// Copy the text to the allocated memory
	char* memData = static_cast<char*>(GlobalLock(hMem));
	if (!memData) {
		CloseClipboard();
		printf_s("Failed to lock memory for text");
		return false;
	}
	strcpy_s(memData, strlen(text) + 1, text);
	GlobalUnlock(hMem);
	// Set the data to the clipboard
	EmptyClipboard();
	SetClipboardData(CF_TEXT, hMem);
	// Clean up and close the clipboard
	CloseClipboard();
	return true;
}


std::string get_passwd()
{

	HANDLE hStdInput = GetStdHandle(STD_INPUT_HANDLE);
	DWORD mode = 0;

	// Create a restore point Mode
	// is know 503
	GetConsoleMode(hStdInput, &mode);

	// Enable echo input
	// set to 499
	SetConsoleMode(hStdInput, mode & (~ENABLE_ECHO_INPUT));

	// Take input
	std::string ipt;
	std::getline(std::cin, ipt);

	// Otherwise next cout will print
	// into the same line
	std::cout << std::endl;

	// Restore the mode
	SetConsoleMode(hStdInput, mode);

	return ipt;
}