define(['dialogHelper', 'loading', 'jQuery', 'emby-input', 'emby-checkbox', 'paper-icon-button-light'], function (dialogHelper, loading, $) {
var currentItem;
var currentItemType;
var currentDeferred;
var hasChanges = false;
var currentSearchResult;
function searchForIdentificationResults(page) {
var lookupInfo = {
ProviderIds: {}
};
$('.identifyField', page).each(function () {
var value = this.value;
if (value) {
if (this.type == 'number') {
value = parseInt(value);
}
lookupInfo[this.getAttribute('data-lookup')] = value;
}
});
var hasId = false;
$('.txtLookupId', page).each(function () {
var value = this.value;
if (value) {
hasId = true;
}
lookupInfo.ProviderIds[this.getAttribute('data-providerkey')] = value;
});
if (!hasId && !lookupInfo.Name) {
require(['toast'], function (toast) {
toast(Globalize.translate('MessagePleaseEnterNameOrId'));
});
return;
}
if (currentItem && currentItem.GameSystem) {
lookupInfo.GameSystem = currentItem.GameSystem;
}
lookupInfo = {
SearchInfo: lookupInfo,
IncludeDisabledProviders: true
};
loading.show();
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Items/RemoteSearch/" + currentItemType),
data: JSON.stringify(lookupInfo),
contentType: "application/json",
dataType: 'json'
}).then(function (results) {
loading.hide();
showIdentificationSearchResults(page, results);
});
}
function showIdentificationSearchResults(page, results) {
$('.popupIdentifyForm', page).hide();
$('.identificationSearchResults', page).show();
$('.identifyOptionsForm', page).hide();
$('.btnIdentifyBack', page).show();
var html = '';
for (var i = 0, length = results.length; i < length; i++) {
var result = results[i];
html += getSearchResultHtml(result, i);
}
var elem = $('.identificationSearchResultList', page).html(html);
$('.searchImage', elem).on('click', function () {
var index = parseInt(this.getAttribute('data-index'));
var currentResult = results[index];
if (currentItem != null) {
showIdentifyOptions(page, currentResult);
} else {
finishFindNewDialog(page, currentResult);
}
});
}
function finishFindNewDialog(dlg, identifyResult) {
currentSearchResult = identifyResult;
hasChanges = true;
loading.hide();
dialogHelper.close(dlg);
}
function showIdentifyOptions(page, identifyResult) {
$('.popupIdentifyForm', page).hide();
$('.identificationSearchResults', page).hide();
$('.identifyOptionsForm', page).show();
$('.btnIdentifyBack', page).show();
$('#chkIdentifyReplaceImages', page).checked(true);
currentSearchResult = identifyResult;
var lines = [];
lines.push(identifyResult.Name);
if (identifyResult.ProductionYear) {
lines.push(identifyResult.ProductionYear);
}
if (identifyResult.GameSystem) {
lines.push(identifyResult.GameSystem);
}
var resultHtml = lines.join('
');
if (identifyResult.ImageUrl) {
var displayUrl = getSearchImageDisplayUrl(identifyResult.ImageUrl, identifyResult.SearchProviderName);
resultHtml = '
' + resultHtml;
}
$('.selectedSearchResult', page).html(resultHtml);
}
function getSearchResultHtml(result, index) {
var html = '';
var cssClass = "card";
if (currentItemType == "Episode") {
cssClass += " backdropCard";
}
else if (currentItemType == "MusicAlbum" || currentItemType == "MusicArtist") {
cssClass += " squareCard";
}
else {
cssClass += " portraitCard";
}
html += '