// Initializes one credential with the field information passed in.
HRESULT Credential::Initialize(
    __in CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
    __in const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
    __in const FIELD_STATE_PAIR* rgfsp
)
{
    HRESULT hr = S_OK;

    _cpus = cpus;

    // Copy the field descriptors for each field. This is useful if you want to vary the field
    // descriptors based on what Usage scenario the credential was created for.
    for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
    {
        _rgFieldStatePairs[i] = rgfsp[i];
        hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
    }

    // Initialize the String value of all the fields.
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Reboot to Mac OS X", &_rgFieldStrings[SFI_LARGE_TEXT]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Reboot to Mac OS X", &_rgFieldStrings[SFI_COMMAND_LINK]);
    }

    return S_OK;
}
// Initializes one credential with the field information passed in.
// Set the value of the SFI_USERNAME field to pwzUsername.
HRESULT CardUnlockCredential::Initialize(
    CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
    const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
    const FIELD_STATE_PAIR* rgfsp,
    RfidListener * rfidListener
)
{
    HRESULT hr = S_OK;

    _cpus = cpus;
    _rfidListener = rfidListener;

    // Copy the field descriptors for each field. This is useful if you want to vary the field
    // descriptors based on what Usage scenario the credential was created for.
    for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
    {
        _rgFieldStatePairs[i] = rgfsp[i];
        hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
    }

    // Initialize the String value of the message field.
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"RFID", &(_rgFieldStrings[SFI_MESSAGE]));
    }

    return S_OK;
}
// Initializes one credential with the field information passed in.
// Set the value of the SFI_LARGE_TEXT field to pwzUsername.
HRESULT CSampleCredential::Initialize(
    __in CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
    __in const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
    __in const FIELD_STATE_PAIR* rgfsp
    )
{
    HRESULT hr = S_OK;

    _cpus = cpus;

    // Copy the field descriptors for each field. This is useful if you want to vary the field
    // descriptors based on what Usage scenario the credential was created for.
    for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
    {
        _rgFieldStatePairs[i] = rgfsp[i];
        hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
    }

    // Initialize the String value of all the fields. 
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Large Text", &_rgFieldStrings[SFI_LARGE_TEXT]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Small Text", &_rgFieldStrings[SFI_SMALL_TEXT]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Edit Text", &_rgFieldStrings[SFI_EDIT_TEXT]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"", &_rgFieldStrings[SFI_PASSWORD]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Submit", &_rgFieldStrings[SFI_SUBMIT_BUTTON]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Checkbox", &_rgFieldStrings[SFI_CHECKBOX]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Combobox", &_rgFieldStrings[SFI_COMBOBOX]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Command Link", &_rgFieldStrings[SFI_COMMAND_LINK]);
    }

    return S_OK;
}
// Initializes one credential with the field information passed in.
// Set the value of the SFI_USERNAME field to pwzUsername.
HRESULT CCustomCredential::Initialize(
                                      CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
                                      const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
                                      const FIELD_STATE_PAIR* rgfsp,
                                      DWORD dwFlags
                                      )
{
    HRESULT hr = S_OK;
    _cpus = cpus;
    _dwFlags = dwFlags;
    // Copy the field descriptors for each field. This is useful if you want to vary the 
    // field descriptors based on what Usage scenario the credential was created for.
    for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
    {
        _rgFieldStatePairs[i] = rgfsp[i];
        hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
    }

    // Initialize the String values of all the fields.
    /*if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(pwzUsername, &FieldStrings[SFI_USERNAME].c_str());
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(pwzPassword ? pwzPassword : L"", &FieldStrings[SFI_PASSWORD].c_str());
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Submit", &FieldStrings[SFI_SUBMIT_BUTTON].c_str());
    }*/
	FieldStrings[SFI_USERNAME].clear();
	FieldStrings[SFI_USERNAME].push_back(0);
	FieldStrings[SFI_PASSWORD].clear();
	FieldStrings[SFI_PASSWORD].push_back(0);

	auto submitstr = std::wstring(L"Submit!");
	FieldStrings[SFI_SUBMIT_BUTTON] = std::vector<wchar_t>(submitstr.begin(), submitstr.end());
	FieldStrings[SFI_SUBMIT_BUTTON].push_back(0);

    return S_OK;
}
//
// Initializes one credential with the field information passed in.
// Set the value of the SFI_USERNAME field to pwzUsername.
//
HRESULT CMessageCredential::Initialize(
    __in const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
    __in const FIELD_STATE_PAIR* rgfsp,
    __in PWSTR szMessage
    )
{
    HRESULT hr = S_OK;

    // Copy the field descriptors for each field. This is useful if you want to vary the field
    // descriptors based on what Usage scenario the credential was created for.
    for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
    {
        _rgFieldStatePairs[i] = rgfsp[i];
        hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
    }

    // Initialize the String value of the message field.
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(szMessage, &(_rgFieldStrings[SMFI_MESSAGE]));
    }

    return S_OK;
}
// Initializes one credential with the field information passed in.
// Set the value of the SFI_USERNAME field to pwzUsername.
// Optionally takes a password for the SetSerialization case.
HRESULT COpenOTPCredential::Initialize(
	__in CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus, 
    __in const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
    __in const FIELD_STATE_PAIR* rgfsp,
	__in_opt PWSTR user_name,
	__in_opt PWSTR domain_name
    )
{
    HRESULT hr = S_OK;

	_cpus = cpus;

	if (user_name)
		_user_name = user_name;

	if (domain_name)
		_domain_name = domain_name;

    // Copy the field descriptors for each field. This is useful if you want to vary the 
    // field descriptors based on what Usage scenario the credential was created for.
    for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
    {
        _rgFieldStatePairs[i] = rgfsp[i];
        hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
    }

    // Initialize the String values of all the fields.
	if (SUCCEEDED(hr))
    {
		//if (_openotp_login_text[0] == NULL)
		//	hr = SHStrDupW(OPENOTP_DEFAULT_LOGIN_TEXT, &_rgFieldStrings[SFI_OTP_LARGE_TEXT]);
		//else
		//{
			wchar_t large_text[sizeof(_openotp_login_text)];

			int size = MultiByteToWideChar(CP_ACP, 0, _openotp_login_text, -1, large_text, 0);
			MultiByteToWideChar(CP_ACP, 0, _openotp_login_text, -1, large_text, size);

			hr = SHStrDupW(large_text, &_rgFieldStrings[SFI_OTP_LARGE_TEXT]);
		//}

		//hr = SHStrDupW(L"", &_rgFieldStrings[SFI_OTP_LARGE_TEXT]);
    }
	if (SUCCEEDED(hr))
    {
		if (_cpus == CPUS_UNLOCK_WORKSTATION)
			hr = SHStrDupW(WORKSTATION_LOCKED, &_rgFieldStrings[SFI_OTP_SMALL_TEXT]);
		else
			hr = SHStrDupW(L"", &_rgFieldStrings[SFI_OTP_SMALL_TEXT]);
	}
    if (SUCCEEDED(hr))
    {
		if (_cpus == CPUS_UNLOCK_WORKSTATION && _user_name)
		{
			hr = SHStrDupW(_user_name, &_rgFieldStrings[SFI_OTP_USERNAME]);
		}
		else
			hr = SHStrDupW(L"", &_rgFieldStrings[SFI_OTP_USERNAME]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"", &_rgFieldStrings[SFI_OTP_LDAP_PASS]);
    }
	if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"", &_rgFieldStrings[SFI_OTP_PASS]);
    }
	if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"", &_rgFieldStrings[SFI_OTP_CHALLENGE]);
    }
    if (SUCCEEDED(hr))
    {
        hr = SHStrDupW(L"Submit", &_rgFieldStrings[SFI_OTP_SUBMIT_BUTTON]);
    }

    return hr;
}