package com.genlot.ninglottery.game;

import android.os.Bundle;

import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.model.GameTicket;
import com.genlot.nativesdk.sender.EventToken;
import com.genlot.nativesdk.sender.TCPSender;
import com.genlot.ninglottery.application.LotteryApplication;
import com.genlot.ninglottery.base.BasePresenter;
import com.genlot.ninglottery.toolkit.PrinterTool;

import java.util.Objects;

/**
 * @author ZhuYongKang
 * @description GamePresenter
 * @date 2019/8/29
 */
public class GamePresenter extends BasePresenter<IGameView> {

    /**
     * 投注
     */
    public void t101200Bet(GameTicket ticket) {
        getView().showLoading();
        TCPSender.t101200Betting(EventToken.T_BETTING, 1, ticket, new TCPSender.SenderCallback() {
            @Override
            public void onSenderSuccess(Bundle bundle) {
                GameTicket ticket = bundle.getParcelable(EventToken.EVENT_KEY_RESULT_OBJECT);
                //调用投注接口，后台返回投注时间，根据mTerminalID，mGameIssue，mTicketSN，mFeatureCode，
                //mBettingTime，mBettingMoney，mGameBetData 生成验票码
                assert ticket != null;
                ticket.mVerifyCode = SystemNI.verifyCode(ticket);
                boolean printSuccess = PrinterTool.printTicket(LotteryApplication.getInstance(), ticket);
                Objects.requireNonNull(GameManager.getGameRuntime(ticket.mGameID)).mGameSN = ticket.mTicketSN;
                getView().dismissLoading();
                if (printSuccess) {
                    getView().onBetSuccess(ticket.mBettingMoney, ticket.mBets);
                } else {
                    t101999SpecialCancel(GameManager.getLastTicket());
                }
            }

            @Override
            public void onSenderFail(int code, String message) {
                getView().dismissLoading();
                getView().showTips(message);
            }
        });
    }

    /**
     * 冲正
     */
    public void t101999SpecialCancel(GameTicket ticket) {
        if (ticket.mGameID != 0) {
            getView().showLoading();
            String str = SystemNI.featureCode(ticket);
            TCPSender.t101999SpecialCancel(EventToken.T_SPECIAL_CANCEL, 1, str, new TCPSender.SenderCallback() {
                @Override
                public void onSenderSuccess(Bundle bundle) {
                    PrinterTool.printRevertTicket(LotteryApplication.getInstance(), ticket);
                    getView().dismissLoading();
                }

                @Override
                public void onSenderFail(int code, String message) {
                    getView().dismissLoading();
                    getView().showTips(message);
                }

            });
        }
    }

}
