'use strict';

const playerBar = '.Root__now-playing-bar';

const artistSelector = `${playerBar} [dir="auto"]:last-child a`;
const spotifyConnectSelector = '[aria-live="polite"] span';

Connector.useMediaSessionApi();

Connector.playerSelector = playerBar;

Connector.artistSelector = [
	artistSelector,
	// For local files
	`${playerBar} [data-testid="track-info-artists"]`,
];

Connector.trackSelector = [
	`${playerBar} [dir="auto"]:first-child a`,
	// For local files
	`${playerBar} [data-testid="track-info-name"]`,
];

Connector.trackArtSelector = '.NavBarFooter .cover-art-image';

Connector.currentTimeSelector = `${playerBar} [data-testid=playback-position]`;

Connector.durationSelector = `${playerBar} [data-testid=playback-duration]`;

Connector.pauseButtonSelector = `${playerBar} [data-testid=control-button-pause]`;

Connector.applyFilter(MetadataFilter.getSpotifyFilter());

Connector.isScrobblingAllowed = () => isMusicPlaying() && isMainTab();

Connector.isPodcast = () => isPodcastPlaying();

function isMusicPlaying() {
	return artistUrlIncludes('/artist/', '/show/') || isLocalFilePlaying();
}

function artistUrlIncludes(...strings) {
	const artistUrl = Util.getAttrFromSelectors(artistSelector, 'href');

	if (artistUrl) {
		for (const str of strings) {
			if (artistUrl.includes(str)) {
				return true;
			}
		}
	}

	return false;
}

function isPodcastPlaying() {
	if (isLocalFilePlaying()) {
		return false;
	}

	return artistUrlIncludes('/show/');
}

function isLocalFilePlaying() {
	// Local files has no links
	// TODO Use better detection
	return document.querySelector(artistSelector) === null;
}

function isMainTab() {
	if (hasMultipleSources()) {
		const deviceName = getActiveDeviceName();
		return deviceName && !deviceName.includes('Web Player');
	}

	return true;
}

function hasMultipleSources() {
	return document.body.classList.contains('qualaroo--connect-bar-visible');
}

function getActiveDeviceName() {
	const spotifyConnectEl = document.querySelector(spotifyConnectSelector);
	return spotifyConnectEl && spotifyConnectEl.textContent;
}

@@ -149,17 +149,30 @@
 ector = 
-'
+%60$%7BplayerBar%7D 
 %5Baria-li
@@ -187,14 +187,9 @@
 te%22%5D
- span'
+%60
 ;%0A%0AC

Fix spotify connector