/* eslint-disable indent */
/**
* Module for itemidentifier media item.
* @module components/itemidentifier/itemidentifier
*/
import escapeHtml from 'escape-html';
import dialogHelper from '../dialogHelper/dialogHelper';
import loading from '../loading/loading';
import globalize from '../../scripts/globalize';
import scrollHelper from '../../scripts/scrollHelper';
import layoutManager from '../layoutManager';
import focusManager from '../focusManager';
import browser from '../../scripts/browser';
import '../../elements/emby-input/emby-input';
import '../../elements/emby-checkbox/emby-checkbox';
import '../../elements/emby-button/paper-icon-button-light';
import '../formdialog.scss';
import 'material-design-icons-iconfont';
import '../cardbuilder/card.scss';
import ServerConnections from '../ServerConnections';
import toast from '../toast/toast';
import template from './itemidentifier.template.html';
import datetime from '../../scripts/datetime';
const enableFocusTransform = !browser.slow && !browser.edge;
let currentItem;
let currentItemType;
let currentServerId;
let currentResolve;
let currentReject;
let hasChanges = false;
let currentSearchResult;
function getApiClient() {
return ServerConnections.getApiClient(currentServerId);
}
function searchForIdentificationResults(page) {
let lookupInfo = {
ProviderIds: {}
};
let i;
let length;
const identifyField = page.querySelectorAll('.identifyField');
let value;
for (i = 0, length = identifyField.length; i < length; i++) {
value = identifyField[i].value;
if (value) {
if (identifyField[i].type === 'number') {
value = parseInt(value, 10);
}
lookupInfo[identifyField[i].getAttribute('data-lookup')] = value;
}
}
let hasId = false;
const txtLookupId = page.querySelectorAll('.txtLookupId');
for (i = 0, length = txtLookupId.length; i < length; i++) {
value = txtLookupId[i].value;
if (value) {
hasId = true;
}
lookupInfo.ProviderIds[txtLookupId[i].getAttribute('data-providerkey')] = value;
}
if (!hasId && !lookupInfo.Name) {
toast(globalize.translate('PleaseEnterNameOrId'));
return;
}
lookupInfo = {
SearchInfo: lookupInfo
};
if (currentItem && currentItem.Id) {
lookupInfo.ItemId = currentItem.Id;
} else {
lookupInfo.IncludeDisabledProviders = true;
}
loading.show();
const apiClient = getApiClient();
apiClient.ajax({
type: 'POST',
url: apiClient.getUrl(`Items/RemoteSearch/${currentItemType}`),
data: JSON.stringify(lookupInfo),
contentType: 'application/json',
dataType: 'json'
}).then(results => {
loading.hide();
showIdentificationSearchResults(page, results);
});
}
function showIdentificationSearchResults(page, results) {
const identificationSearchResults = page.querySelector('.identificationSearchResults');
page.querySelector('.popupIdentifyForm').classList.add('hide');
identificationSearchResults.classList.remove('hide');
page.querySelector('.identifyOptionsForm').classList.add('hide');
page.querySelector('.dialogContentInner').classList.remove('dialog-content-centered');
let html = '';
let i;
let length;
for (i = 0, length = results.length; i < length; i++) {
const result = results[i];
html += getSearchResultHtml(result, i);
}
const elem = page.querySelector('.identificationSearchResultList');
elem.innerHTML = html;
function onSearchImageClick() {
const index = parseInt(this.getAttribute('data-index'), 10);
const currentResult = results[index];
if (currentItem != null) {
showIdentifyOptions(page, currentResult);
} else {
finishFindNewDialog(page, currentResult);
}
}
const searchImages = elem.querySelectorAll('.card');
for (i = 0, length = searchImages.length; i < length; i++) {
searchImages[i].addEventListener('click', onSearchImageClick);
}
if (layoutManager.tv) {
focusManager.autoFocus(identificationSearchResults);
}
}
function finishFindNewDialog(dlg, identifyResult) {
currentSearchResult = identifyResult;
hasChanges = true;
loading.hide();
dialogHelper.close(dlg);
}
function showIdentifyOptions(page, identifyResult) {
const identifyOptionsForm = page.querySelector('.identifyOptionsForm');
page.querySelector('.popupIdentifyForm').classList.add('hide');
page.querySelector('.identificationSearchResults').classList.add('hide');
identifyOptionsForm.classList.remove('hide');
page.querySelector('#chkIdentifyReplaceImages').checked = true;
page.querySelector('.dialogContentInner').classList.add('dialog-content-centered');
currentSearchResult = identifyResult;
const lines = [];
lines.push(escapeHtml(identifyResult.Name));
if (identifyResult.ProductionYear) {
lines.push(datetime.toLocaleString(identifyResult.ProductionYear, { useGrouping: false }));
}
let resultHtml = lines.join('
');
if (identifyResult.ImageUrl) {
resultHtml = `