package com.genlot.ninglottery.module.cash;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.genlot.devicesdk.Printer;
import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.model.GameCashTicket;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseMVPActivity;
import com.genlot.ninglottery.base.InjectPresenter;
import com.genlot.ninglottery.game.GameManager;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.toolkit.GameUtil;
import com.genlot.ninglottery.toolkit.StringTool;
import com.genlot.ninglottery.toolkit.TimeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 兑奖界面
 * @date 2019/8/20
 */
public class CashActivity extends BaseMVPActivity implements ICashView{

    @InjectPresenter
    CashPresenter mPresenter;

    @OnClick(R.id.icon_close)
    public void close() {
        finish();
    }

    @OnClick(R.id.bt_cash)
    public void cash() {
        if (GameManager.isTrainMode) {
            onCashSuccess(System.currentTimeMillis() / 1000);
        } else {
            mPresenter.t101009Cash(mFeatureCode);
        }
    }

    private GameCashTicket mCashTicket;
    private String mFeatureCode;
    private List<WinDetailBean> mDetailList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = 864;
        lp.height = 564;
        //设置对话框置顶显示
        lp.gravity = Gravity.CENTER;
        win.setAttributes(lp);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_cash;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        mCashTicket = getIntent().getParcelableExtra(Constant.KEY_TICKET);
        mFeatureCode = getIntent().getStringExtra(Constant.KEY_CODE);
        mDetailList = new ArrayList<>();
        if (mCashTicket != null) {
            tvGameName.setText(GameManager.getGameName(mCashTicket.mGameID));
            tvIssue.setText(String.valueOf(mCashTicket.mCashIssue));
            tvPrize.setText(String.format(Locale.getDefault(),"%.2f",mCashTicket.mPrizeMoney));
            tvTax.setText(String.format(Locale.getDefault(),"%.2f",mCashTicket.mTaxMoney));
            tvStation.setText(String.valueOf(mCashTicket.mTerminalID));
            tvSerial.setText(String.valueOf(mCashTicket.mTicketSN));
            tvEndTime.setText(TimeUtils.getDetailTime(mCashTicket.mCashEndTime));
            initWinDetail();
            String statusResult = GameUtil.parseStatus(mCashTicket.mStatus);
            if (!"有效".equals(statusResult)) {
                btCash.setEnabled(false);
            }
        }
        WinDetailAdapter adapter = new WinDetailAdapter(this, mDetailList);
        lvDetail.setAdapter(adapter);
    }

    private void initWinDetail() {
        for (int i = 0; i < mCashTicket.mTotalCount; i++) {
            WinDetailBean bean = new WinDetailBean();
            bean.setBetSN(String.valueOf(mCashTicket.mBetSN[i]));
            bean.setWinLevel(String.valueOf(mCashTicket.mWinLevelName[i]));
            bean.setBetCount(String.valueOf(mCashTicket.mWinLevelCount[i]));
            bean.setMultiTimes(String.valueOf(mCashTicket.mMultiTimes[i]));
            bean.setPrizeMoney(StringTool.format1(mCashTicket.mPrizeBet[i]));
            bean.setPrizeTax(StringTool.format1(mCashTicket.mTaxBet[i]));
            mDetailList.add(bean);
        }
    }

    @Override
    public void onCashSuccess(long cashTime) {
        dismissLoading();
        runOnUiThread(() -> {
            btCash.setEnabled(false);
            btCash.setText("已兑奖");
        });
        if (printCashTicket(this, cashTime)) {
            finish();
        }
    }

    private boolean printCashTicket(Context context, long cashTime) {
        Printer.preload(Printer.DEVICE_YK_T91, Printer.INTERFACE_USB);
        Printer.preloadUSBAttr(Constant.PRINT_VID, Constant.PRINT_PID);
        Printer.openPrinter(context);
        Printer.initPrinter();
        //打印兑奖单
        Printer.setLeftMargin(20);
        Printer.lineFeed(2);
        Printer.setFontBold(1);
        Printer.setFontSize(17);
        String str = "==兑奖单==";
        Printer.printString(str);
        Printer.lineFeed(2);
        //打印游戏信息
        Printer.setFontSize(0);
        Printer.setFontBold(0);
        Printer.printString("--------------------------------------");
        Printer.lineFeed(1);
        str = "游  戏: " + tvGameName.getText().toString();
        Printer.printString(str);
        Printer.lineFeed(1);
        str = "站  号: " + mCashTicket.mTerminalID;
        Printer.printString(str);
        Printer.lineFeed(1);
        str = "期  号: " + mCashTicket.mCashIssue;
        Printer.printString(str);
        Printer.lineFeed(1);
        str = "流水号: " + mCashTicket.mTicketSN;
        Printer.printString(str);
        Printer.lineFeed(1);
        //打印兑奖信息
        Printer.printString("--------------------------------------");
        Printer.lineFeed(1);
        Printer.printString("开奖期号");
        Printer.setAbsX(200);
        Printer.printString("兑奖金额");
        Printer.setAbsX(400);
        Printer.printString("税金");
        Printer.lineFeed(1);
        for (int i = 0; i < mDetailList.size(); i++) {
            WinDetailBean bean = mDetailList.get(i);
            Printer.setAbsX(200);
            float prizeMoney = Float.parseFloat(bean.getPrizeMoney()) * Float.parseFloat(bean.getBetCount()) * Float.parseFloat(bean.getMultiTimes());
            Printer.printString(prizeMoney + "元");
            Printer.setAbsX(400);
            float taxMoney = Float.parseFloat(bean.getPrizeTax()) * Float.parseFloat(bean.getBetCount()) * Float.parseFloat(bean.getMultiTimes());
            Printer.printString(taxMoney + "元");
            Printer.lineFeed(1);
        }
        Printer.printString("合计");
        Printer.setAbsX(200);
        Printer.printString(mCashTicket.mPrizeMoney + "元");
        Printer.setAbsX(400);
        Printer.printString(mCashTicket.mTaxMoney + "元");
        Printer.lineFeed(1);
        str = "兑奖站号: " + SystemNI.getTerminalID();
        Printer.printString(str);
        Printer.lineFeed(1);
        str = "操作员  : " + GameManager.getUser();
        Printer.printString(str);
        Printer.lineFeed(1);
        str = "兑奖时间: " + TimeUtils.getDetailTime(cashTime);
        Printer.printString(str);
        Printer.lineFeed(4);

        int ret = Printer.cutPaper();
        Printer.closePrinter();
        return 0 == ret;
    }

    @BindView(R.id.tv_cashResult_name)
    TextView tvGameName;
    @BindView(R.id.tv_cashResult_issue)
    TextView tvIssue;
    @BindView(R.id.tv_cashResult_endAwardTime)
    TextView tvEndTime;
    @BindView(R.id.tv_cashResult_Prize)
    TextView tvPrize;
    @BindView(R.id.tv_cashResult_tax)
    TextView tvTax;
    @BindView(R.id.tv_cashResult_station)
    TextView tvStation;
    @BindView(R.id.tv_cashResult_serial)
    TextView tvSerial;
    @BindView(R.id.lv_cashResult_winDetail)
    ListView lvDetail;
    @BindView(R.id.bt_cash)
    Button btCash;
}
