import { showError, Cookie } from "../../util/util";

var BridgePlugin = {};
BridgePlugin.install = function(Vue, store) {
    let state = store.state;
    Vue.prototype.gameIframe = function() {
        return document.getElementById("__gameIframe__");
    };
    //公钥登录
    Vue.prototype.$scatterLogin = async function() {
        await store.dispatch("doScatterLogin").then(accounts => {
            this.gameIframe().contentWindow.postMessage(
                { login: accounts },
                "*"
            );
        });
    };

    Vue.prototype.$isLogin = function() {
        return Cookie.getCookie("account") && Cookie.getCookie("token");
    };
    Vue.prototype.$isScatterLogin = function() {
        return state.account != null && state.account != "NO_LOGINING";
    };
    //获取用戶信息
    Vue.prototype.$getEosInfo = function() {
        if (this.$isScatterLogin()) {
            this.gameIframe().contentWindow.postMessage(
                { login: state.account },
                "*"
            );
        } else {
            this.gameIframe().contentWindow.postMessage({ logout: true }, "*");
        }
    };

    Vue.prototype.$getCoinData = function() {
        if (this.$isScatterLogin()) {
            this.gameIframe().contentWindow.postMessage(
                {
                    coin: {
                        balance: state.balance,
                        money: state.money,
                        gt: state.gt
                    }
                },
                "*"
            );
        }
    };

    Vue.prototype.$getCoin = function(data) {
        if (this.$isScatterLogin()) {
            this.gameIframe().contentWindow.postMessage(data, "*");
        }
    };

    //下注交易
    Vue.prototype.$doDeposit = function(gameId, amount, memo) {
        let player = state.account.name;
        let toAccount = state.toAccount;
        let chargeAmount = amount.toFixed(4) + " EOS";
        let identity = {
            authorization: [`${player}@${state.account.authority}`]
        };
        let invite = state.invitee || 0;
        let fullmemo = `baccarat|${gameId}|${invite}${memo}`;
        // 发出合约调用请求
        window.EOS.transfer(player, toAccount, chargeAmount, fullmemo, identity)
            .then(trx => {
                let param = {
                    doDeposit: true,
                    transaction_id: trx.transaction_id,
                    block_num: trx.processed.block_num
                        ? trx.processed.block_num
                        : "",
                    player: player,
                    amount: amount,
                    appid: state.appid, //暂时写死
                    fullmemo: fullmemo,
                    gameId: gameId,
                    memo: memo
                };
                this.gameIframe().contentWindow.postMessage(param, "*");
            })
            .catch(msg => {
                this.gameIframe().contentWindow.postMessage(
                    { doDepositFail: true },
                    "*"
                );
                if (msg.type == "signature_rejected") return;
                let json = JSON.parse(msg);
                let details = json.error.details;
                showError(details[0].message);
            });
    };

    Vue.prototype.$getNewGame = function() {
        store.dispatch("getNewGame");
    };

    Vue.prototype.$gameLoaded = function() {
        function __getOptions() {
            var options = {
                group_id: 10001
            };

            if (
                /Android|webOS|iPhone|iPod|BlackBerry/i.test(
                    navigator.userAgent
                )
            ) {
                // 移动端的情况
                if (window.innerHeight >= window.innerWidth) {
                    // 竖屏
                    options.bottom = 0;
                    options.left = 0;
                    options.height = "66%";
                    options.width = "100%";
                    options.show = false;
                } else {
                    // 横屏
                    options.bottom = 0;
                    options.left = 0;
                    options.height = "100%";
                    options.width = "60%";
                    options.show = false;
                }
            } else {
                // PC 端的情况
                options.bottom = 10;
                options.right = 10;
                options.height = 500;
                options.width = 300;
                options.show = true;
            }

            return options;
        }

        function resize() {
            var options = __getOptions();
            delete options.show;
            window.__MeeChat.resize(options);
        }

        console.log("init chat");
        window.__MeeChat.init(__getOptions());
        /*************************** 【可选】响应式网站可以加入以下代码 ************************/
        resize();
        window.addEventListener("orientationchange", resize);
        window.addEventListener("resize", resize);
        // ***********************************************************************************
    };
};

export default BridgePlugin;
