"use strict";

function init(params) {
    let wnd = this;    
    wnd.resizeable = false;
    wnd.title = _('Edit custom node');

    let edtType = qid('edtType');
    edtType.controlClass.addValue( _('Tracks'));
    edtType.controlClass.addValue( _('Albums'));
    
    if (params.item.type == 'albums') {
        edtType.controlClass.focusedIndex = 1;
    } else {
        edtType.controlClass.focusedIndex = 0;
    }

    let edtName = qid('edtName');
    edtName.controlClass.value = params.item.name;

    let edtSQL = qid('edtSQL');
    edtSQL.controlClass.value = params.item.sql;
    
    let btnOK = qid('btnOK');
    edtName.focus();

    let btnOKUpdate = function () {
        if (!window._cleanUpCalled)
            btnOK.controlClass.disabled = (edtName.controlClass.value === '') || (edtSQL.controlClass.value === '');
    };

    window.localListen(edtName, 'change', btnOKUpdate);
    window.localListen(edtSQL, 'change', btnOKUpdate);
    window.localListen(edtName, 'keyup', btnOKUpdate);
    window.localListen(edtSQL, 'keyup', btnOKUpdate);
    btnOKUpdate();

    window.localListen(btnOK, 'click', () => {

        if (edtType.controlClass.focusedIndex == 1) {
            params.item.type = 'albums'
        } else {
            params.item.type = 'tracks'
        }
            
        params.item.name = edtName.controlClass.value;            
        params.item.sql = edtSQL.controlClass.value;
     
        modalResult = 1;
        closeWindow();
    });
}
