<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Candy - Chats are not dead yet</title>
    <link rel="shortcut icon" href="res/img/favicon.png" type="image/gif"/>
    <link rel="stylesheet" type="text/css" href="libs.min.css"/>
    <link rel="stylesheet" type="text/css" href="res/default.css"/>
    <style>
        body {
            font-family: -apple-system, Arial, 'Source Sans Pro', "Microsoft YaHei", 'Microsoft JhengHei', "WenQuanYi Micro Hei", sans-serif;
        }
    </style>

    <script>delete module.exports</script>
    <script type="text/javascript" src="../node_modules/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="libs.min.js"></script>
    <script type="text/javascript" src="candy.min.js"></script>
</head>
<body>
<div id="candy"></div>
<script type="text/javascript">
    const {remote, ipcRenderer} = require('electron');
    //    remote.getCurrentWebContents().openDevTools();
    require('electron-cookies'); // https://github.com/hstove/electron-cookies

    ipcRenderer.on('join', (event, message) => {
        if (Candy.View.Pane.Chat.rooms[message]) {
            Candy.View.Pane.Room.show(message);
        } else {
            Candy.Core.Action.Jabber.Room.Join(message);
        }
    });

    // fix
    Base64.encode = (data) => new Buffer(data).toString('base64');
    Base64.decode = (data) => new Buffer(data, 'base64').toString();

    // candy init
    const params = new URLSearchParams(location.search);

    Candy.View.Template.Login.form = '<form method="post" id="login-form" class="login-form">' + '<input type="hidden" id="nickname" name="nickname" value="' + params.nickname + '"/>' + '{{#displayUsername}}<input type="hidden" id="username" name="username" value="' + params.jid + '"/>' + '{{#displayDomain}} <span class="at-symbol">@</span> ' + '<select id="domain" name="domain">{{#domains}}<option value="{{domain}}">{{domain}}</option>{{/domains}}</select>' + "{{/displayDomain}}" + "{{/displayUsername}}" + '{{#presetJid}}<input type="hidden" id="username" name="username" value="{{presetJid}}"/>{{/presetJid}}' + '{{#displayPassword}}<input type="hidden" id="password" name="password" value="' + params.password + '"/>{{/displayPassword}}' + '<input type="submit" class="button" value="{{_loginSubmit}}" /></form>';

    Candy.Util.setCookie('candy-nostatusmessages', '1', 365);
    Candy.init('wss://chat.mycard.moe:5280/websocket', {
        core: {
            debug: false,
            autojoin: params.get('autojoin') && [params.get('autojoin')],
            resource: 'mycard-' + Math.random().toString().split('.')[1]
        },
        view: {assets: 'res/', language: params.get('language')}
    });

    Candy.Core.connect(params.get('jid'), params.get('password'), params.get('nickname'));

</script>

</body>
</html>
