/**
 * name:	AddDate
 * class:	CAnnivEditCtrl
 * desc:	Add a new item to the array of dates
 * param:	mda		- the date to add
 * return:	0 on success, -1 on failure, 1 if the item to change was edited before and the new item was not set
 **/
INT_PTR CAnnivEditCtrl::AddDate(MAnnivDate &mda)
{
	MAnnivDate *pmda, **pmd;

	// if a date with wID exists, replace it
	if ((pmda = FindDateById(mda.Id())) != NULL) {
		BYTE bChanged = pmda->IsChanged(),
			bRemindChanged = pmda->IsReminderChanged();

		if (!bChanged) {
			pmda->Set(mda);
			pmda->Module(mda.Module());
			pmda->Description(mda.Description());
			pmda->Flags(mda.Flags());
		}
		if (!bRemindChanged) {
			pmda->RemindOption(mda.RemindOption());
			pmda->RemindOffset(mda.RemindOffset());
		}
		return bChanged || bRemindChanged;
	}
	if (mda.Id() == ANID_NONE)
		mda.Id(_numDates - 1);

	if (pmd = (MAnnivDate **)mir_realloc(_pDates, (_numDates + 1) * sizeof(pmda))) {
		_pDates = pmd;
		if (_pDates[_numDates] = new MAnnivDate(mda)) {
			_numDates++;
			return 0;
		}
	}
	return 1;
}
/**
 * name:	Set
 * class:	MAnnivDate
 * desc:	duplicates the given anniversary date class
 * param:	none
 * return:	nothing
 **/ 
VOID MAnnivDate::SetDate(MAnnivDate &mda)
{
	SetDate(mda.SystemTime());
	_wID = mda.Id();
	_strDesc = mda.Description();
	_strModule = mda.Module();
	_wFlags = mda.Flags();
	_bRemind = mda.RemindOption();
	_wDaysEarlier = mda.RemindOffset();
}
/**
 * name:	EnableCurrentItem
 * class:	CAnnivEditCtrl
 * desc:	make control readonly if required
 * param:	none
 * return:	nothing
 **/
void CAnnivEditCtrl::EnableCurrentItem()
{
	MAnnivDate *pCurrent = Current();

	if (pCurrent) {
		MCONTACT hContact;
	
		PSGetContact(_hwndDlg, hContact);

		const BYTE bEnabled
			= !hContact ||
				(pCurrent->Flags() & CTRLF_HASCUSTOM) || 
				!(pCurrent->Flags() & (CTRLF_HASPROTO|CTRLF_HASMETA)) ||
				!db_get_b(NULL, MODNAME, SET_PROPSHEET_PCBIREADONLY, 0);

		EnableWindow(_hBtnEdit, bEnabled);
		EnableWindow(_hBtnDel, bEnabled);
		EnableWindow(_hwndDate, bEnabled);
	}
}
/**
 * name:	DBWriteBirthDay
 * class:	CAnnivEditCtrl
 * desc:	writes the birthday for a contact to database
 * param:	hContact - the contact to write the anniversaries to
 * return:	0 on success 1 otherwise
 **/
INT_PTR CAnnivEditCtrl::DBWriteBirthDay(MCONTACT hContact)
{
	MAnnivDate *pmdb;

	if ((pmdb = FindDateById(ANID_BIRTHDAY)) == NULL)
		return 1;
		
	if (pmdb->IsChanged()) {
		// save birthday, to mBirthday module by default
		if (pmdb->Flags() & pmdb->MADF_HASCUSTOM)
			pmdb->DBWriteBirthDate(hContact);
		else
			pmdb->DBDeleteBirthDate(hContact);
	}

	if (pmdb->IsReminderChanged()) {
		pmdb->DBWriteReminderOpts(hContact);
	}
	pmdb->RemoveFlags(MAnnivDate::MADF_CHANGED|MAnnivDate::MADF_REMINDER_CHANGED);
	return 0;
}
INT_PTR CALLBACK PSPProcAnniversary(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CAnnivEditCtrl *pDateCtrl = CAnnivEditCtrl::GetObj(hDlg, EDIT_ANNIVERSARY_DATE);

	switch (uMsg) {
	case WM_INITDIALOG:
		{
			CCtrlList *pCtrlList = CCtrlList::CreateObj(hDlg);
			if (pCtrlList) {
				HFONT hBoldFont;
				PSGetBoldFont(hDlg, hBoldFont);
				SendDlgItemMessage(hDlg, IDC_PAGETITLE, WM_SETFONT, (WPARAM)hBoldFont, 0);

				TranslateDialogDefault(hDlg);

				pCtrlList->insert(CEditCtrl::CreateObj(hDlg, EDIT_AGE, SET_CONTACT_AGE, DBVT_BYTE));
				pCtrlList->insert(CAnnivEditCtrl::CreateObj(hDlg, EDIT_ANNIVERSARY_DATE, NULL));

				// hContact == NULL or reminder disabled
				CAnnivEditCtrl::GetObj(hDlg, EDIT_ANNIVERSARY_DATE)->EnableReminderCtrl(lParam != NULL);

				SendDlgItemMessage(hDlg, EDIT_AGE, EM_LIMITTEXT, 3, 0);
				SendDlgItemMessage(hDlg, SPIN_AGE, UDM_SETRANGE32, 0, 200);
			}
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_ICONCHANGED:
				{
					const ICONCTRL idIcon[] = {
						{ ICO_COMMON_BIRTHDAY, STM_SETIMAGE, ICO_BIRTHDAY },
						{ ICO_BTN_ADD, BM_SETIMAGE, BTN_ADD },
						{ ICO_BTN_DELETE, BM_SETIMAGE, BTN_DELETE }
					};
					IcoLib_SetCtrlIcons(hDlg, idIcon, _countof(idIcon));
				}
				return FALSE;
			}
			break; /* case 0 */

		case EDIT_ANNIVERSARY_DATE:
			if (!PspIsLocked(hDlg) && PtrIsValid(pDateCtrl)) {
				LPNMHDR lpNmhdr = (LPNMHDR)lParam;
				switch (lpNmhdr->code) {
				case DTN_DATETIMECHANGE:
					pDateCtrl->OnDateChanged((LPNMDATETIMECHANGE)lParam);
					break;
				case DTN_DROPDOWN:
					HWND hMonthCal = DateTime_GetMonthCal(lpNmhdr->hwndFrom);
					SetWindowLongPtr(hMonthCal, GWL_STYLE, GetWindowLongPtr(hMonthCal, GWL_STYLE) | MCS_WEEKNUMBERS);
					InvalidateRect(hMonthCal, NULL, TRUE);
				}
			}
			return FALSE;
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case EDIT_REMIND:
			if (!PspIsLocked(hDlg) && HIWORD(wParam) == EN_UPDATE)
			if (PtrIsValid(pDateCtrl))
				pDateCtrl->OnRemindEditChanged();
			return FALSE;

		/**
		* name:	BTN_MENU
		* desc:	the button to dropdown the list to show all items is pressed
		**/
		case BTN_MENU:
			if (HIWORD(wParam) == BN_CLICKED) {
				if (PtrIsValid(pDateCtrl))
					pDateCtrl->OnMenuPopup();
			}
			return FALSE;

		/**
		* name:	BTN_ADD
		* desc:	creates a new dialog to add a new anniversary
		**/
		case BTN_ADD:
			if (HIWORD(wParam) == BN_CLICKED && PtrIsValid(pDateCtrl)) {
				MAnnivDate Date;
				if (IDOK == DialogBoxParam(ghInst, MAKEINTRESOURCE(IDD_ANNIVERSARY_EDITOR), hDlg, DlgProc_AnniversaryEditor, (LPARAM)&Date)) {
					SendMessage(GetParent(hDlg), PSM_CHANGED, NULL, NULL);
					if (!pDateCtrl->AddDate(Date))
						pDateCtrl->SetCurSel(pDateCtrl->NumDates() - 1);
				}
			}
			return FALSE;

		/**
		* name:	BTN_EDIT
		* desc:	edit the currently selected anniversary
		**/
		case BTN_EDIT:
			if (HIWORD(wParam) == BN_CLICKED && PtrIsValid(pDateCtrl)) {
				MAnnivDate *pDate = pDateCtrl->Current();
				if (!pDate)
					MsgErr(hDlg, LPGENT("No valid date selected for editing!"));
				else if (
					IDOK == DialogBoxParam(ghInst, MAKEINTRESOURCE(IDD_ANNIVERSARY_EDITOR), hDlg, DlgProc_AnniversaryEditor, (LPARAM)pDate) &&
					(pDate->Flags() & (MAnnivDate::MADF_CHANGED | MAnnivDate::MADF_REMINDER_CHANGED)))
				{
					SendMessage(GetParent(hDlg), PSM_CHANGED, NULL, NULL);
					pDateCtrl->SetCurSel(pDateCtrl->CurrentIndex());
				}
			}
			return FALSE;

		/**
		* name:	BTN_DELETE
		* desc:	user wants to delete an anniversary
		**/
		case BTN_DELETE:
			if (HIWORD(wParam) == BN_CLICKED && PtrIsValid(pDateCtrl)) {
				MAnnivDate *pCurrent = pDateCtrl->Current();
				if (pCurrent) {
					int rc = MsgBox(hDlg, MB_YESNO | MB_ICON_QUESTION | MB_NOPOPUP, LPGENT("Delete"), NULL,
						LPGENT("Do you really want to delete the %s?"), pCurrent->Description());

					if (rc == IDYES)
						pDateCtrl->DeleteDate(pDateCtrl->CurrentIndex());
				}
			}
			return FALSE;

		/**
		* name:	CHECK_REMIND
		* desc:	state of reminder checkbox is changed
		**/
		case RADIO_REMIND1:
		case RADIO_REMIND2:
		case RADIO_REMIND3:
			CAnnivEditCtrl *pDateCtrl = CAnnivEditCtrl::GetObj(hDlg, EDIT_ANNIVERSARY_DATE);
			if (PtrIsValid(pDateCtrl) && HIWORD(wParam) == BN_CLICKED)
				pDateCtrl->OnReminderChecked();
			return FALSE;
		} /* switch (LOWORD(wParam)) */
		break;
	}
	return PSPBaseProc(hDlg, uMsg, wParam, lParam);
}