#include "CSetSnapshot.h"
#include "resource.h"
#include <ShlObj_core.h>

IMPLEMENT_DYNAMIC(CSetSnapshot, CDialog)

BEGIN_MESSAGE_MAP(CSetSnapshot, CDialog)
	ON_BN_CLICKED(IDOK, &CSetSnapshot::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_ASEAPP, &CSetSnapshot::OnBnClickedButtonBrowseAseapp)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_SAVEAS, &CSetSnapshot::OnBnClickedButtonBrowseSaveas)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_PATH, &CSetSnapshot::OnBnClickedButtonBrowsePath)
END_MESSAGE_MAP()

CSetSnapshot::CSetSnapshot(CWnd* pParent)
	: CDialog(IDD_CMAPEDITOR_SETSNAPSHOT, pParent)
{
}

CSetSnapshot::~CSetSnapshot()
{
}

void CSetSnapshot::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_ASEAPP, edit_aseApp);
	DDX_Control(pDX, IDC_EDIT_SAVEAS, edit_saveAs);
	DDX_Control(pDX, IDC_EDIT_PATH, edit_path);
	DDX_Control(pDX, IDC_EDIT_FILENAME, edit_filename);
	DDX_Control(pDX, IDC_BUTTON_BROWSE_ASEAPP, button_aseApp);
	DDX_Control(pDX, IDC_BUTTON_BROWSE_SAVEAS, button_saveAs);
	DDX_Control(pDX, IDC_BUTTON_BROWSE_PATH, button_path);
}

std::wstring GetDesktopPath()
{
	PWSTR path = nullptr;
	HRESULT hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, nullptr, &path);
	if (SUCCEEDED(hr)) {
		std::wstring desktopPath(path);
		CoTaskMemFree(path); // 释放内存
		return desktopPath;
	}
	return L""; // 失败返回空字符串
}

BOOL CSetSnapshot::OnInitDialog()
{
	CDialog::OnInitDialog();
	//写入AsepriteExe路径
	wchar_t buff[MAX_PATH];
	CString inipath;
	GetModuleFileNameW(nullptr, buff, _countof(buff));
	inipath = buff;
	auto index = inipath.ReverseFind(L'\\');
	inipath.Delete(index, inipath.GetLength() - index);
	inipath.Append(L"\\ocstore.ini");

	DWORD result = GetPrivateProfileStringW(
		L"OCSTORE",
		L"AsepriteExePath",
		L"",  // 默认值，如果键不存在则返回这个值
		buff,
		_countof(buff),
		inipath.GetString()
	);
	if (result > 0 && buff[0] != '\0')
	{
		//写入最后一次的Aseprite路径
		edit_aseApp.SetWindowTextW(buff);
	}
	else
	{
		//写入默认路径
		edit_aseApp.SetWindowTextW(LR"(C:\Program Files (x86)\Steam\steamapps\common\Aseprite\Aseprite.exe)");
	}

	//写入中间文件路径
	result = GetPrivateProfileStringW(
		L"OCSTORE",
		L"IntermediateFilePath",
		L"",  // 默认值，如果键不存在则返回这个值
		buff,
		_countof(buff),
		inipath.GetString()
	);
	if (result > 0 && buff[0] != '\0')
	{
		//写入最后一次的Aseprite路径
		edit_path.SetWindowTextW(buff);
	}
	else
	{
		//写入默认路径
		edit_path.SetWindowTextW(GetDesktopPath().c_str());
	}

	//写入输出文件目录
	result = GetPrivateProfileStringW(
		L"OCSTORE",
		L"SnapshotOutputPath",
		L"",  // 默认值，如果键不存在则返回这个值
		buff,
		_countof(buff),
		inipath.GetString()
	);
	if (result > 0 && buff[0] != '\0')
	{
		//写入最后一次的Aseprite路径
		edit_saveAs.SetWindowTextW(buff);
	}
	else
	{
		//写入默认路径
		edit_saveAs.SetWindowTextW(GetDesktopPath().c_str());
	}
	edit_filename.SetWindowTextW(inFilename.GetString());
	return 0;
}
void CSetSnapshot::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	edit_aseApp.GetWindowTextW(outAseApp);
	if (_taccess(outAseApp.GetString(), 0) != 0)
	{
		MessageBoxW(L"应用程序文件路径不存在", L"填写信息错误", MB_ICONINFORMATION);
		return;
	}
	edit_saveAs.GetWindowTextW(outSaveAs);
	if (!PathIsDirectoryW(outSaveAs.GetString()))
	{
		MessageBoxW(L"输出目录不存在", L"填写信息错误", MB_ICONINFORMATION);
		return;
	}
	edit_path.GetWindowTextW(outPath);
	if (!PathIsDirectoryW(outPath.GetString()))
	{
		MessageBoxW(L"中间目录不存在", L"填写信息错误", MB_ICONINFORMATION);
		return;
	}
	edit_filename.GetWindowTextW(outFilename);
	//保存到ini文件
	wchar_t buff[MAX_PATH];
	CString inipath;
	GetModuleFileNameW(nullptr, buff, _countof(buff));
	inipath = buff;
	auto index = inipath.ReverseFind(L'\\');
	inipath.Delete(index, inipath.GetLength() - index);
	inipath.Append(L"\\ocstore.ini");

	WritePrivateProfileStringW(L"OCSTORE", L"AsepriteExePath", outAseApp.GetString(), inipath.GetString());
	WritePrivateProfileStringW(L"OCSTORE", L"IntermediateFilePath", outPath.GetString(), inipath.GetString());
	WritePrivateProfileStringW(L"OCSTORE", L"SnapshotOutputPath", outSaveAs.GetString(), inipath.GetString());
	//输出到变量
	CDialog::OnOK();
}

void CSetSnapshot::OnBnClickedButtonBrowseAseapp()
{
	CFileDialog fd(false, TEXT(""), TEXT("Aseprite.exe"), OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT, TEXT("应用程序(*.exe)|*.exe|所有文件(*.*)|*.*||"), this);
	if (fd.DoModal() != IDOK)
		return;
	edit_aseApp.SetWindowText(fd.GetPathName());
}

void CSetSnapshot::OnBnClickedButtonBrowseSaveas()
{
	CFolderPickerDialog dlgFolder;

	// 设置对话框标题
	dlgFolder.m_ofn.lpstrTitle = _T("请选择导出文件目录");

	// 显示对话框并获取用户选择
	if (dlgFolder.DoModal() != IDOK)
		return;
	// 获取选择的文件夹路径
	edit_saveAs.SetWindowTextW(dlgFolder.GetFolderPath());
}

void CSetSnapshot::OnBnClickedButtonBrowsePath()
{
	CFolderPickerDialog dlgFolder;

	// 设置对话框标题
	dlgFolder.m_ofn.lpstrTitle = _T("请选择中间文件目录");

	// 显示对话框并获取用户选择
	if (dlgFolder.DoModal() != IDOK)
		return;
	// 获取选择的文件夹路径
	edit_path.SetWindowTextW(dlgFolder.GetFolderPath());
}
