/*
 * @Author: Rindy
 * @Date: 2020-02-24 11:43:44
 * @LastEditors: Rindy
 * @LastEditTime: 2020-06-19 15:34:05
 * @Description: 判断加载平台
 */
// import bus from './eventbus'
import browser from './browser'
// import { backgroundMusicPlayer } from './sounds'

const ua = navigator.userAgent || navigator.vendor || window.opera

function isIOS() {
    if (/iPad|iPhone|iPod/.test(ua) && !window.MSStream) {
        return true
    } else {
        return false
    }
}

function isAndroid() {
    return /android/i.test(ua)
}

var androidNative = browser.urlHasValue('android')

function sendMessageToUnity(message) {
    try {
        if (isIOS()) {
            window.webkit.messageHandlers.inappbrowserbridge.postMessage(message)
        } else if (isAndroid()) {
            if (androidNative) {
                switch (message) {
                    case 'Back':
                        AndroidNative.Back()
                        break
                    case 'Share':
                        AndroidNative.Share()
                        break
                }
            } else {
                UnityInAppBrowser.sendMessageFromJS(message)
            }
        }
    } catch (e) {
        console.warn(e)
    }
}

// 注册App发送消息方法
window.appSendMsgBack = function (name, data) {
    console.log('appSendMsgBack: ' + name, data)

    // bus.emit('app-' + name, data)
}

/** 背景音乐控制 */

// bus.on('app-onResume', () => {
//     console.log('appMusicPlay')
//     backgroundMusicPlayer.resume()
// })
// bus.on('app-onStop', () => {
//     console.log('appMusicPause')
//     if (backgroundMusicPlayer.isPlaying) {
//         backgroundMusicPlayer.pause(true)
//     }
// })
/** 背景音乐控制  end */

/**
 * APP
 */
export const IsApp = browser.urlHasValue('app') && !browser.urlHasValue('appname')

/**
 * 微信
 */
export const IsWechat = /MicroMessenger/gi.test(window.navigator.userAgent)

/**
 * 刘海高度
 */
export const NotchHeight = browser.urlHasValue('notch', true)

/**
 * 发送消息到APP
 * @param {*} message
 */
export function MessageToApp(message) {
    console.log('SendToApp')
    console.log(message)
    sendMessageToUnity(message)
}
