package com.chinapnr.pos.pay.demo.hw;

import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.chinapnr.pos.config.PnrResponseKey;
import com.chinapnr.pos.pay.demo.R;
import com.chinapnr.pos.printer.VectorFormat;
import com.chinapnr.pos.printer.line.BarCodeLine;
import com.chinapnr.pos.printer.line.DivisionLine;
import com.chinapnr.pos.printer.line.ImgLine;
import com.chinapnr.pos.printer.line.Line;
import com.chinapnr.pos.printer.line.QRLine;
import com.chinapnr.pos.printer.line.TextDivisionLine;
import com.chinapnr.pos.printer.line.TextLine;
import com.chinapnr.pos.printer.line.ThreeColumnLine;
import com.chinapnr.pos.printer.line.TwoColumnLine;
import com.chinapnr.pos.trans.PnrService;
import com.chinapnr.pos.trans.PnrTransListener;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

public class PrintActivity extends AppCompatActivity implements View.OnClickListener {

    private List<Line> lineList;
    private TextView tv_request, tv_result;
    private View view_line;

    private StringBuffer info = new StringBuffer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print);
        lineList = new ArrayList<>();

        findViewById(R.id.btn_img).setOnClickListener(this);
        findViewById(R.id.btn_text_div).setOnClickListener(this);
        findViewById(R.id.btn_text).setOnClickListener(this);
        findViewById(R.id.btn_two_text).setOnClickListener(this);
        findViewById(R.id.btn_three_text).setOnClickListener(this);
        findViewById(R.id.btn_div).setOnClickListener(this);
        findViewById(R.id.btn_div_real).setOnClickListener(this);
        findViewById(R.id.btn_qr).setOnClickListener(this);
        findViewById(R.id.btn_bar).setOnClickListener(this);
        findViewById(R.id.btn_empty).setOnClickListener(this);
        findViewById(R.id.btn_small_text).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        tv_request = findViewById(R.id.tv_request);
        view_line = findViewById(R.id.view_line);
        tv_result = findViewById(R.id.tv_result);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_img:

                lineList.add(new ImgLine(openAsset("logo.bmp")));//打印一张图片
                info.append("添加一个[logo图片] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_text_div:
                lineList.add(new TextDivisionLine("标准带横线文本"));
                lineList.add(new TextDivisionLine("小字体带横线文本", new VectorFormat().setSize(16)));
                info.append("添加一行[标准带横线文本] \n").append("添加一行[小字体带横线文本] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_text:
                lineList.add(new TextLine("标准文本"));
                lineList.add(new TextLine("居右加粗大字体文本", new VectorFormat().setAlignment(Layout.Alignment.ALIGN_OPPOSITE).setSize(30).setBold(true)));
                info.append("添加一行[标准文本] \n").append("添加一行[居右加粗大字体文本] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_two_text:
                lineList.add(new TwoColumnLine("姓名：", "张三"));
                lineList.add(new TwoColumnLine("姓名：", new VectorFormat().setSize(30), "张三", new VectorFormat(20)));
                info.append("添加一行[标准两列文本] \n").append("添加一行[左侧加大，右侧缩小的两列文本] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_three_text:
                lineList.add(new ThreeColumnLine("名称", "数量", "单价"));
                lineList.add(new ThreeColumnLine("瓜子", "10", "￥20.5", new VectorFormat(18)));
                lineList.add(new ThreeColumnLine("花生", "2", "￥30.5").setLeftWidth(50).setRightWidth(100).setRightWidth(50));
                info.append("添加一行[标准三列文本] \n").append("添加一行[缩小三列文本] \n").append("添加一行[放大三列文本] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_div:
                lineList.add(new DivisionLine());//虚线
                lineList.add(new DivisionLine(5));
                info.append("添加一行[虚线] \n").append("添加一行[加粗的虚线] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_div_real:
                lineList.add(new DivisionLine(true));//实线
                info.append("添加一行[实线] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_qr:
                lineList.add(new QRLine("这是一个测试二维码"));
                info.append("添加一个[二维码] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_bar:
                lineList.add(new BarCodeLine("20210313165232564855"));//条码
                lineList.add(new TwoColumnLine("订单号：", "20210313165232564855"));
                info.append("添加一行[条码] \n").append("添加一行[条码说明文本] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_empty:
                lineList.add(new TextLine(" "));//空行
                lineList.add(new TextLine(" "));
                lineList.add(new TextLine(" "));
                info.append("添加一行[空行] \n").append("添加一行[空行] \n").append("添加一行[空行] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_small_text:
                lineList.add(new TextLine("本人确认以上交易", new VectorFormat(Layout.Alignment.ALIGN_CENTER).setSize(16)));
                info.append("添加一行[小字体居中文本] \n");
                tv_request.setText(info.toString());
                break;
            case R.id.btn_confirm:
                doPrint();
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
    }

    private void doPrint(){
        info.append("开始打印："  + "\n" + "总数：" + lineList.size());
        tv_request.setText(info.toString());
        view_line.setVisibility(View.VISIBLE);
        PnrService.getInstance(getApplicationContext()).doPrint(lineList, new PnrTransListener() {
            @Override
            public void onResult(String result) {
                Timber.e(result);
                info = new StringBuffer();
                lineList.clear();
                Map resultMap = new Gson().fromJson(result, Map.class);
                String responseCode = resultMap.get(PnrResponseKey.KEY_RESPONSECODE).toString();
                if ("00".equals(responseCode)){
                    tv_result.setText("打印成功："+result);
                }else {
                    tv_result.setText("打印失败：" + result);
                }
            }
        });

    }

    private InputStream openAsset(String fileName) {
        InputStream is = null;
        try {
            is = getAssets().open(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }
}