"use strict";

/**
@module UI
*/

requirejs('controls/baseVis');
requirejs('controls/embeddedWindow');

/**
UI Visualizer element - Milkshake.

@class Vis_winamp
@constructor
@extends BaseVis
*/

class Vis_winamp extends BaseVis {

    initialize(parentel, params) {
        this.waPluginIndex = 0; // passed index of the Winamp visualization plugin
        this.waModuleIndex = 0; // passed index of the Winamp visualization module
        super.initialize(parentel, params);
        // no need to register, it will be handled directly by the engine 
        //app.player.visualization.registerVisTypes(['oscilloscope']);
        var _this = this;
        this._visCanvas = document.createElement('canvas');
        this._visCanvas.classList.add('textColor'); // adds also background
        this.container.appendChild(this._visCanvas);
        this._title = "Winamp";
        this._artist = "Visualization";
/*        this.visWindow = qeid(this.container, 'visWindow');
        if (!this.visWindow) {
            this.visWindow = document.createElement('div');
            this.visWindow.setAttribute('data-id', 'visWindow');
            this.visWindow.classList.add('fill');
            this.visWindow.setAttribute('data-control-class', 'EmbeddedWindow');
            this.visWindow.setAttribute('data-init-params', '{type: "empty"}');
            this.visWindow.setAttribute('data-contentType', 'empty');
        }
        this.container.appendChild(this.visWindow);
        initializeControls(this.container);*/
    }

    onResizeHandler(w, h) {
        if (this._visCanvas) {
            if ((this._visCanvas.width === w) && (this._visCanvas.height === h))
                return;
            this._visCanvas.width = w;
            this._visCanvas.height = h;
            this._visCanvasCtx = this._visCanvas.getContext('2d');
            var time = Date.now();            
            this.defaultRenderText(time, this._visCanvasCtx);
        }
    }

    cleanUp() {
        this.clearVis();
        //app.player.visualization.unregisterVisTypes(['oscilloscope']);
/*        if(this.visWindow) {
            cleanElement(this.visWindow);
            this.visWindow = undefined;
        }*/
        super.cleanUp();
    }

    clearVis() {
        // nothing to clear
    }

    onStopHandler() {
        this.clearVis();
    }

    keyHandler (evt) {
        // no key handling
    }

    handle_dblclick(evt) {
        // no dblclick handling
    }

    prepareVis () {
        let vis = app.player.visualization;
        if (!vis.active) {
            ODS('--- Winamp vis not active, skipping prepare');
            return;
        }
        if (!this._visPluginPrepared) {
            let waPlugins = vis.getWAVisPlugins();
            waPlugins.locked(() => {
                let wp = waPlugins.getValue(this.waPluginIndex);
                ODS('--- preparing Winamp vis plugin index: ' + this.waPluginIndex + ', module index: ' + this.waModuleIndex);
                wp.init(this.waModuleIndex);
                this._visPluginPrepared = true;
/*                if(wp) {
                    if(this.visWindow.controlClass.windowHandle) {
                        ODS('--- setWindowHandle for Winamp vis plugin, handle: ' + this.visWindow.controlClass.windowHandle);
                        wp.setWindowHandle(this.visWindow.controlClass.windowHandle);
                        wp.init(this.waModuleIndex);
                    } else {
                        ODS('--- visWindow handle not ready yet for Winamp vis plugin');
                        let listener = app.listen(this.visWindow, 'created', function() {
                            app.unlisten(this.visWindow, 'created', listener);
                            listener = undefined
                            ODS('--- setWindowHandle for Winamp vis plugin (delayed), handle: ' + this.visWindow.controlClass.windowHandle);
                            wp.setWindowHandle(this.visWindow.controlClass.windowHandle);
                            wp.init(this.waModuleIndex);
                        }.bind(this));
                    }
                }*/
            });
        } else {
            ODS('--- Winamp vis already prepared');
        }
        super.prepareVis();
    }
    exitVis () {
        let vis = app.player.visualization;
        if (this._visPluginPrepared) {
            let waPlugins = vis.getWAVisPlugins();
            waPlugins.locked(() => {
                let wp = waPlugins.getValue(this.waPluginIndex);
                if(wp) {
                    ODS('--- exiting Winamp vis plugin index: ' + this.waPluginIndex + ', module index: ' + this.waModuleIndex);
                    wp.quit();
                }
            });
            this._visPluginPrepared = false;
        };
        super.exitVis();
    }
}
registerClass(Vis_winamp);
