/**
 * This module includes code available for both the public website and
 * administration interface)
 */
//-- vendor
import '@fortawesome/fontawesome-free/css/all.min.css';


//-- aircox
import App, {PlayerApp} from './app'
import VueLoader from './vueLoader'
import Sound from './sound'
import {Set} from './model'

import './styles/common.scss'


window.aircox = {
    // main application
    loader: null,
    get app() { return this.loader.app  },

    // player application
    playerLoader: null,
    get playerApp() { return this.playerLoader && this.playerLoader.app },
    get player() { return this.playerLoader.vm && this.playerLoader.vm.$refs.player },

    Set, Sound,


    /**
     * Initialize main application and player.
     */
    init(props=null, {hotReload=false, el=null,
                      config=null, playerConfig=null,
                      initApp=true, initPlayer=true,
                      loader=null, playerLoader=null}={})
    {
        if(initPlayer) {
            playerConfig = playerConfig || PlayerApp
            playerLoader = playerLoader || new VueLoader(playerConfig)
            playerLoader.enable(false)
            this.playerLoader = playerLoader

            document.addEventListener("keyup", e => this.onKeyPress(e), false)
        }

        if(initApp) {
            config = config || window.App || App
            config.el = el || config.el
            loader = loader || new VueLoader({el, props, ...config})
            loader.enable(hotReload)
            this.loader = loader
        }
    },

    onKeyPress(/*event*/) {
        /*
        if(event.key == " ") {
            this.player.togglePlay()
            event.stopPropagation()
        }
        */
    },

    /**
     * Filter navbar dropdown menu items
     */
    filter_menu(event) {
        var filter = new RegExp(event.target.value, 'gi');
        var container = event.target.closest('.navbar-dropdown');

        if(event.target.value)
            for(let item of container.querySelectorAll('a.navbar-item'))
                item.style.display = item.innerHTML.search(filter) == -1 ? 'none' : null;
        else
            for(let item of container.querySelectorAll('a.navbar-item'))
                item.style.display = null;
    },

    pickDate(url, date) {
        url = `${url}?date=${date.id}`
        this.loader.pageLoad.load(url)
    }
}
