package com.miya.hardware;

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

import com.miya.print.PrinterManager;
import com.miya.print.utils.CheckStatusUtils;

public class PrintActivity extends Activity implements View.OnClickListener {


    Button btnInit;

    Button btnRelease;

    Button btnPrintText;

    Button btnSetTextSize;

    Button btnCut;

    EditText etText;

    TextView tvResult;

    /**
     * 文字大小或放大倍数
     */
    int textSize;

    @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);
        etText = findViewById(R.id.etText);
        tvResult = findViewById(R.id.tvResult);
        btnCut = findViewById(R.id.btnCut);

        //set click listener
        btnInit.setOnClickListener(this);
        btnRelease.setOnClickListener(this);
        btnPrintText.setOnClickListener(this);
        btnSetTextSize.setOnClickListener(this);
        btnCut.setOnClickListener(this);
        findViewById(R.id.ivBack).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().getPrinter().release() == true) {
                        tvResult.setText("销毁成功");
                    } else {
                        tvResult.setText("没初始化过");
                    }
                }
                break;
            case R.id.btnPrintText:
                String content = etText.getText().toString();
                if (PrinterManager.getInstance().isConnected()) {
                    try {
                        int ret = PrinterManager.getInstance().getPrinter().printText(0, 0, content, true);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        tvResult.setText(ex.getMessage());
                    }
                }
                break;
            case R.id.btnSetTextSize:
                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.ivBack:
                finish();
                break;
        }
    }
}
