package com.miya.hardware;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.miya.print.IPrinter;
import com.miya.print.PrinterManager;
import com.miya.print.PrinterStatusEnum;
import com.miya.print.utils.Page80MmPrintUtils;

public class PrintActivity extends Activity implements View.OnClickListener {


    Button btnInit;

    Button btnRelease;

    Button btnPrintText;

    Button btnSetTextSize;

    Button btnCut;

    EditText etText;

    /**
     * 文字大小或放大倍数，取值0～8
     */
    int textSize;
    EditText etTextSize;

    TextView tvResult;

    /**
     * 一维条码打印
     */
    EditText etBarcode;
    Button btnBarcode;

    /**
     * 二维码打印
     */
    EditText etQrcode;
    Button btnQrcode;

    /**
     * 图片打印
     */
    ImageView ivImg;
    Button btnPrintImg;

    /**
     * 获取打印机状态
     */
    Button btnStatus;

    /**
     * 设置对齐方式
     */
    EditText etAlign;
    Button btnSetAlign;

    /**
     * 走纸
     */
    Button btnFeedPaper;

    /**
     * 打印测试订单
     */
    Button btnPrintTest;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print);

        //findView
        btnInit = findViewById(R.id.btnInit);
        btnRelease = findViewById(R.id.btnRelease);
        btnPrintText = findViewById(R.id.btnPrintText);
        btnSetTextSize = findViewById(R.id.btnSetTextSize);
        etTextSize = findViewById(R.id.etTextSize);
        etText = findViewById(R.id.etText);
        tvResult = findViewById(R.id.tvResult);
        btnCut = findViewById(R.id.btnCut);
        etBarcode = findViewById(R.id.etBarcode);
        btnBarcode = findViewById(R.id.btnBarcode);
        etQrcode = findViewById(R.id.etQrcode);
        btnQrcode = findViewById(R.id.btnQrcode);
        ivImg = findViewById(R.id.ivImg);
        btnPrintImg = findViewById(R.id.btnPrintImg);
        btnStatus = findViewById(R.id.btnStatus);
        btnSetAlign = findViewById(R.id.btnSetAlign);
        etAlign = findViewById(R.id.etAlign);
        btnFeedPaper = findViewById(R.id.btnFeedPaper);
        btnPrintTest = findViewById(R.id.btnPrintTest);

        //set click listener
        btnInit.setOnClickListener(this);
        btnRelease.setOnClickListener(this);
        btnPrintText.setOnClickListener(this);
        btnSetTextSize.setOnClickListener(this);
        btnCut.setOnClickListener(this);
        findViewById(R.id.ivBack).setOnClickListener(this);

        btnBarcode.setOnClickListener(this);
        btnQrcode.setOnClickListener(this);
        btnPrintImg.setOnClickListener(this);
        btnStatus.setOnClickListener(this);
        btnSetAlign.setOnClickListener(this);
        btnFeedPaper.setOnClickListener(this);
        btnPrintTest.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnInit:
                //初始化打印硬件
                boolean result = PrinterManager.getInstance().init(this);
                if (result) {
                    tvResult.setText("找到了打印机，机器名称：" + PrinterManager.getInstance().getPrinter().getPrinterName());
                } else {
                    tvResult.setText("没找到打印机");
                }
                break;
            case R.id.btnRelease:
                if (PrinterManager.getInstance().isConnected()) {
                    if (PrinterManager.getInstance().release() == true) {
                        tvResult.setText("销毁成功");
                    } else {
                        tvResult.setText("没初始化过");
                    }
                }
                break;
            case R.id.btnPrintText:
                String content = etText.getText().toString();
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        PrinterManager.getInstance().getPrinter().beginPrint();
                        int ret = PrinterManager.getInstance().getPrinter().printText(-1, textSize, content, true);
                        PrinterManager.getInstance().getPrinter().endPrint();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                }
                break;
            case R.id.btnSetTextSize:
                textSize = Integer.parseInt(etTextSize.getText().toString());
                break;
            case R.id.btnCut:
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        PrinterManager.getInstance().getPrinter().cutPaper();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                }
                break;
            case R.id.btnBarcode:
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        PrinterManager.getInstance().getPrinter().printBarcode(-1, etBarcode.getText().toString(), false);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                }
                break;
            case R.id.btnQrcode:
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        PrinterManager.getInstance().getPrinter().printQrcode(-1, etQrcode.getText().toString(), false);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                }
                break;
            case R.id.btnPrintImg:
                if (PrinterManager.getInstance().isConnected()) {
//                    Bitmap bm = BitmapUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo), 240, 0);
                    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo);
                    try {
                        PrinterManager.getInstance().getPrinter().printImage(-1, bm, false);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    } finally {
                        if (bm != null) {
                            bm.recycle();
                        }
                    }
                }
                break;
            case R.id.btnStatus:
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        int ret = PrinterManager.getInstance().getPrinter().getPrinterStatus();
                        tvResult.setText(PrinterStatusEnum.find(ret).getDesc());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                } else {
                    tvResult.setText("未连接打印机");
                }
                break;
            case R.id.btnSetAlign:
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        int ret = PrinterManager.getInstance().getPrinter().setAlign(Integer.parseInt(etAlign.getText().toString().trim()));
                        tvResult.setText(PrinterStatusEnum.find(ret).getDesc());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                } else {
                    tvResult.setText("未连接打印机");
                }
                break;
            case R.id.btnFeedPaper:
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        int ret = PrinterManager.getInstance().getPrinter().feedPaper();
                        tvResult.setText(PrinterStatusEnum.find(ret).getDesc());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                }
                break;
            case R.id.btnPrintTest:
                if (PrinterManager.getInstance().isConnected()) {
                    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo);
                    try {
                        //居中打印商户名称
                        IPrinter printer = PrinterManager.getInstance().getPrinter();
                        int ret = printer.beginPrint();
                        ret = printer.setAlign(IPrinter.ALIGN_CENTER);
                        //打印logo
                        ret = printer.printImage(-1, bm, true);
                        ret = printer.printText(-1, 0, "中百", true);
                        ret = printer.printText(-1, 0, "支付宝自助收银", true);
                        //分割线
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
                        //居左打印
                        ret = printer.setAlign(IPrinter.ALIGN_LEFT);
                        //订单号
                        ret = printer.printText(-1, 0, "订单号：0759201123908129487982739878354", true);
                        //流水号
                        ret = printer.printText(-1, 0, "流水号：2019011002200012312377857948982", true);
                        //小票号
                        ret = printer.printText(-1, 0, "小票号：1128-2876-532055", true);
                        //打印时间
                        ret = printer.printText(-1, 0, "打印时间：2019-01-10 15:16:27", true);
                        //分割线
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
                        //四条目打印
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printFourData("商品名称", "编码", "数量", "金额"), true);
                        //商品名称
                        ret = printer.printText(-1, 0, "中号购物袋520**(300+80)", true);
                        //三条目打印
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printFourData("6924160298876", "    ", "*1", "0.20元"), true);
                        ret = printer.feedPaper();
                        //分割线
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
                        //总件数
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printTwoData("总件数：", "1"), true);
                        //商品合计
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printTwoData("商品合计：", "0.20"), true);
                        //支付宝实付
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printTwoData("支付宝", "0.01"), true);
                        //分割线
                        ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
                        ret = printer.setAlign(IPrinter.ALIGN_CENTER);
                        //小票尾部
                        ret = printer.printText(-1, 0, "欢迎下次光临!!!", true);
                        //小票尾部条码
                        ret = printer.printBarcode(-1, "0759201123908129487982739878354", true);
                        //小票尾部二维码
                        ret = printer.printQrcode(-1, "2019011002200012312377857948982", false);
                        ret = printer.endPrint();
                        //剪纸
                        ret = printer.cutPaper();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    } finally {
                        if (bm != null) {
                            bm.recycle();
                        }
                    }
                }
                break;
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
