var maxReconnectCount = 10
var websocket
var channelId
var connectUrl

function send(message) {
    if (websocket && websocket.readyState === WebSocket.OPEN) {
        websocket.send(JSON.stringify(message))
    }
}
function connect() {
    websocket = new WebSocket('ws://' + location.host + '/page/entry')

    if (location.hash === '#new') {
        //new page disable get channelId from sessionStorage
        location.hash = ''
    }
    else {
        channelId = sessionStorage.getItem('channelId')
        connectUrl = sessionStorage.getItem('connectUrl')
    }

    websocket.onopen = function () {

        if (channelId) {
            initQrcode()
        }
        else {
            send({method: 'AtomDebug.applyChannelId'})
        }
    }
    websocket.onmessage = function (event) {
        var message = JSON.parse(event.data)
        if (message.method == 'AtomDebug.pushChannelId') {
            channelId = message.params.channelId
            connectUrl = message.params.connectUrl
            sessionStorage.setItem('channelId', channelId)
            sessionStorage.setItem('connectUrl', connectUrl)
            initQrcode()
        }
        else if (message.method == 'AtomDebug.startDebugger') {
            if (channelId === message.params) {
                sessionStorage.removeItem('channelId')
                sessionStorage.removeItem('connectUrl')
                location.href = '/debug.html?channelId=' + message.params
            }
        }
        else if (message.method == 'AtomDebug.prompt'&&channelId === message.params.channelId) {
            var delayed = 5000 + message.params.messageText.length / 6 * 1000
            removeClassName($('.qrcode-wrap'), 'loading-state')
            toast(translateI18n(message.params.messageText), delayed)
        }
    }

    websocket.onclose = function () {
        sessionStorage.removeItem('channelId')
        sessionStorage.removeItem('connectUrl')
        document.getElementById('qrcode').innerHTML = ''
        $('.qrcode-wrap').className += '  loading-state'
        if (maxReconnectCount-- > 0) {
            setTimeout(connect, 3000)
        }
    }
}

function createQRCode(channelId, connectUrl) {
    document.getElementById('qrcode').innerHTML = ''
    new QRCode(document.getElementById('qrcode'), {
        text: connectUrl || `http:\/\/${location.host}/index.html?_atom_devtool=ws:\/\/${location.host}/debugProxy/native/` + channelId,
        width: 200,
        height: 200,
        colorDark: "#000000",
        colorLight: "#E0E0E0",
        correctLevel: QRCode.CorrectLevel.L
    })
}

var loadingSimulator=false

function hasClassName(selector, classname) {
    return $(selector).className.indexOf(classname) > -1;
}
function replaceClassName(selector, from, to) {
    var $selector = $(selector);
    $selector.className = $selector.className.replace(from, to)
}

connect()

function initQrcode() {
    createQRCode(channelId, connectUrl)
    setTimeout(function(){
        $('.qrcode-wrap').className='qrcode-wrap'
    },800)
}