import { PluginType } from 'constants/pluginType';
import { ServerConnections } from 'lib/jellyfin-apiclient';
import screenSaverManager from 'scripts/screensavermanager';
import * as userSettings from 'scripts/settings/userSettings';

export default class PhotoPlayer {
    constructor() {
        this.name = 'Photo Player';
        this.type = PluginType.MediaPlayer;
        this.id = 'photoplayer';
        this.priority = 1;
    }

    play(options) {
        return new Promise(function (resolve) {
            import('../../components/slideshow/slideshow').then(({ default: Slideshow }) => {
                const index = options.startIndex || 0;

                const apiClient = ServerConnections.currentApiClient();
                apiClient.getCurrentUser().then(function(result) {
                    const newSlideShow = new Slideshow({
                        showTitle: false,
                        cover: false,
                        items: options.items,
                        startIndex: index,
                        interval: 11000,
                        interactive: true,
                        // playbackManager.shuffle has no options. So treat 'shuffle' as a 'play' action
                        autoplay: {
                            delay: userSettings.slideshowInterval() * 1000
                        },
                        user: result,
                        onClose: () => screenSaverManager.unblock()
                    });

                    screenSaverManager.block();
                    newSlideShow.show();
                    resolve();
                });
            });
        });
    }

    canPlayMediaType(mediaType) {
        return (mediaType || '').toLowerCase() === 'photo';
    }
}
