#include "CCalendarBK.h"
#include "resource.h"
#include "CMainEditor.h"
IMPLEMENT_DYNAMIC(CCalendarBK, CDialog)

BEGIN_MESSAGE_MAP(CCalendarBK, CDialog)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_ALPHA, &CCalendarBK::OnNMCustomdrawSliderAlpha)
	ON_BN_CLICKED(IDC_DIR, &CCalendarBK::OnBnClickedDir)
	ON_BN_CLICKED(IDOK, &CCalendarBK::OnBnClickedOk)
END_MESSAGE_MAP()

CCalendarBK::CCalendarBK(CWnd* pParent)
	: CDialog(IDD_CMAINEDITOR_BK, pParent)
{
}

CCalendarBK::~CCalendarBK()
{
}

void CCalendarBK::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DIR, edit_dir);
	DDX_Control(pDX, IDC_SLIDER_ALPHA, slider_alpha);
	label_alpha_value = (CStatic*)GetDlgItem(IDC_STATIC_VALUE);
}

BOOL CCalendarBK::OnInitDialog()
{
	CDialog::OnInitDialog();
	//设置控件初始状态
	slider_alpha.SetRangeMax(255);
	slider_alpha.SetRangeMin(0);
	auto mainEditor = (CMainEditor*)pMainEditor;
	CString calendar_bk_file = mainEditor->GetDBFieldValue(DBTABLENAME_OPTION, L"field", L"calendar_bk_filename", L"value").GetString();
	edit_dir.SetWindowTextW(calendar_bk_file.GetString());
	CString calendar_bk_alpha = mainEditor->GetDBFieldValue(DBTABLENAME_OPTION, L"field", L"calendar_bk_alpha", L"value").GetString();
	if (calendar_bk_alpha.GetLength() == 0)
		calendar_bk_alpha = L"255";
	label_alpha_value->SetWindowTextW(calendar_bk_alpha.GetString());
	int pos;
	swscanf_s(calendar_bk_alpha.GetString(), L"%d", &pos);
	slider_alpha.SetPos(pos);
	return 0;
}


void CCalendarBK::OnNMCustomdrawSliderAlpha(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	
	CString str;
	str.Format(L"%d", slider_alpha.GetPos());
	label_alpha_value->SetWindowTextW(str.GetString());
	*pResult = 0;
	auto mainEditor = (CMainEditor*)pMainEditor;
	mainEditor->alpha = mainEditor->alpha_bkup = (BYTE)slider_alpha.GetPos();
	mainEditor->Draw();
}


void CCalendarBK::OnBnClickedDir()
{
	
	CFileDialog fd(true, TEXT(""), TEXT(""), OFN_FILEMUSTEXIST, TEXT("PNG图像文件(*.png)|*.png|BMP图像文件(*.bmp)|*.bmp|JPEG图像文件(*.jpg)|*.jpg|所有文件(*.*)|*.*||"), this);
	if (fd.DoModal() != IDOK)
		return;
	edit_dir.SetWindowTextW(fd.GetPathName().GetString());
	//修改了背景，在日历窗口立即显示绘制效果
	auto mainEditor = (CMainEditor*)pMainEditor;
	std::vector<char> filename_utf8;
	UTF16ToUTF8(fd.GetPathName().GetString(), &filename_utf8);
	if (mainEditor->pTextureBk_bkup)
	{
		mainEditor->pTextureBk_bkup->Release();
		mainEditor->pTextureBk_bkup = nullptr;
	}
	if (mainEditor->pSRV_bkup)
	{
		mainEditor->pSRV_bkup->Release();
		mainEditor->pSRV_bkup = nullptr;
	}
	mainEditor->pRenderer.MyLoadTexture(&mainEditor->pTextureBk_bkup, filename_utf8.data());
	mainEditor->pRenderer.MyCreateShaderResourceView(&mainEditor->pSRV_bkup, mainEditor->pTextureBk_bkup);
	mainEditor->Draw();
}

void CCalendarBK::OnBnClickedOk()
{
	
	//点击确定 检查本地png目录里有没有同名文件
	//先取本地png文件的目录
	auto mainEditor = (CMainEditor*)pMainEditor;
	auto pngPath = mainEditor->GetBasePath();
	pngPath.Append(L"data\\png\\");
	CString otherPngPath;
	edit_dir.GetWindowTextW(otherPngPath);
	CString otherPngName = otherPngPath.GetString() + otherPngPath.ReverseFind(L'\\') + 1;
	CString localPngPath = pngPath;
	localPngPath.Append(otherPngName);
	//设置输出字段
	outFileName = otherPngName;
	outAlpha.Format(L"%d", slider_alpha.GetPos());
	//检查选择的图像文件是不是就是本地png目录
	if (otherPngPath == localPngPath)
	{
		//如果是同一个目录则直接设置
		CDialog::OnOK();
		return;
	}
	else
	{
		CString thisPngPath = pngPath;
		thisPngPath.Append(otherPngPath);
		if (thisPngPath == localPngPath)
		{
			//如果是同一个目录则直接设置
			CDialog::OnOK();
			return;
		}
	}
	//不是同一个目录
	//检查有没有存在同名文件
	FILE* fp;
	_wfopen_s(&fp, localPngPath.GetString(), L"r");
	if (fp)
	{
		fclose(fp);
		//存在同名文件
		CString info;
		info.Format(L"本地png目录下存在同名文件：%s\n是否替换？", otherPngName.GetString());
		switch (MessageBoxW(info.GetString(), L"是否替换文件？", MB_ICONINFORMATION | MB_YESNO))
		{
		case IDYES://覆盖原本的文件
			CopyFileW(otherPngPath.GetString(), localPngPath.GetString(), false);
			CDialog::OnOK();
			return;
		case IDCANCEL://不设置
		case IDNO:
			return;
		}
	}
	//不存在同名文件 直接复制
	CopyFileW(otherPngPath.GetString(), localPngPath.GetString(), false);
	CDialog::OnOK();
}
