<script>
  volantis.css("<%- theme.cdn.aplayer_css %>");

  <% if (theme.plugins.aplayer.source == "meting") { %>
    (async () => {
      // APlayer 需要在  MetingJS 之前加载
      await volantis.js("<%- theme.cdn.aplayer_js %>")
      await volantis.js("<%- theme.cdn.meting %>")
    <% if (theme.rightmenus.enable && theme.rightmenus.order.includes('music')) { %>
      // 右键 music 需要在 APlayer  MetingJS 之后加载
      await volantis.js('<%- theme.cdn.volantis_aplayer %>')
    <% } %>
    })();

    function SetAPlayerPlugin(){
      let Metings = document.querySelectorAll('meting-js');
      if (Metings.length === 0) {return;};
      if (Metings[0].aplayer && Metings[0].aplayer.on) {
        // improve the accessibility https://web.dev/button-name/
        document.querySelectorAll(".aplayer-icon-menu").forEach(e=>{
          e.setAttribute("aria-label","Aplayer Menu")
        })
        // message see: /layout/_plugins/message/script.ejs
        <% if (theme.plugins.message.enable && theme.plugins.message.aplayer.enable) { %>
          try {
            setTimeout(() => {
              Metings.forEach((item, index) => {
                const aplayerItem = item.aplayer; if(!aplayerItem) return;
                const rightAplayerCheck = '<%= theme.rightmenus.order.includes("music") %>' === 'true'
                  && item.meta.id === '<%= theme.plugins.aplayer.id %>';
                if(rightAplayerCheck && typeof RightMenuAplayer !="undefined") RightMenuAplayer.checkAPlayer();
                if(aplayerItem.events.events.play.every(item => {return item.name !== 'messagePlay'})) {
                  aplayerItem.on('play', function messagePlay() {
                    let index = aplayerItem.list.index;
                    let title = aplayerItem.list.audios[index].title;
                    let artist = aplayerItem.list.audios[index].artist;
                    setTimeout(() => {
                      VolantisApp.message('音乐通知', title + ' - ' + artist, {
                        icon: '<%- theme.plugins.message.aplayer.play %>',
                        transitionIn: 'flipInX',
                        transitionOut: 'flipOutX'
                      });
                    }, 100)
                  });
                }
                if(aplayerItem.events.events.pause.every(item => {return item.name !== 'messagePause'})) {
                  aplayerItem.on('pause', function messagePause() {
                    let index = aplayerItem.list.index;
                    let title = aplayerItem.list.audios[index].title;
                    let artist = aplayerItem.list.audios[index].artist;
                    setTimeout(() => {
                      // 歌曲播放结束也会触发 pause 事件，为了避免错误提示，等待一会儿
                      if(aplayerItem.paused) {
                        VolantisApp.message('音乐通知', title + ' - ' + artist, {
                          icon: '<%- theme.plugins.message.aplayer.pause %>',
                          transitionIn: 'flipInX',
                          transitionOut: 'flipOutX'
                        });
                      }
                    }, 100)
                  });
                }
              });
            }, 500)
          } catch (error) { console.error(error); }
        <% } %>
      }else{
        volantis.requestAnimationFrame(SetAPlayerPlugin)
      }
    }

    document.addEventListener("DOMContentLoaded", ()=>{
      SetAPlayerPlugin();
    });
    volantis.pjax.push(SetAPlayerPlugin,"SetAPlayerPlugin");
  <% } else { %>
    volantis.js("<%- theme.cdn.aplayer_js %>").then(()=>{
      document.querySelectorAll('.aplayer-local').forEach(e=>{
        new APlayer({
          container: e,
          fixed: <%- theme.plugins.aplayer.fixed %>,
          order: "<%-theme.plugins.aplayer.order %>",
          volume: "<%-theme.plugins.aplayer.volume %>",
          autoplay: <%-theme.plugins.aplayer.autoplay %>,
          loop: "<%-theme.plugins.aplayer.loop %>",
          theme: "<%-theme.plugins.aplayer.theme %>",
          listMaxHeight: "<%-theme.plugins.aplayer.list_max_height %>",
          listFolded: "<%-theme.plugins.aplayer.list_folded %>",
          preload: 'auto',
          lrcType: 3,
          audio: <%- JSON.stringify(theme.plugins.aplayer.audio) %>
        });
      })

      // post.music 迷你模式 pjax
      function aplayerMin(){
        if(!document.querySelector('.aplayer-local-min')) return;
        document.querySelectorAll('.aplayer-local-min').forEach(e=>{
          new APlayer(Object.assign({
            container: e,
            mini: true,
            order: "<%-theme.plugins.aplayer.order %>",
            volume: "<%-theme.plugins.aplayer.volume %>",
            autoplay: <%-theme.plugins.aplayer.autoplay %>,
            loop: "<%-theme.plugins.aplayer.loop %>",
            theme: "<%-theme.plugins.aplayer.theme %>",
            listMaxHeight: "<%-theme.plugins.aplayer.list_max_height %>",
            listFolded: "<%-theme.plugins.aplayer.list_folded %>",
            preload: 'auto',
            lrcType: 3,
            audio: <%- JSON.stringify(theme.plugins.aplayer.audio) %>
          },JSON.parse(e.querySelector('.aplayer-local-min-conf').innerText)));
        })
      };
      aplayerMin();
      volantis.pjax.push(aplayerMin,"aplayerMin");
      // 右键 todo...
    })
  <% }  %>
</script>
