package com.gto.ethernet_sample_code;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gainscha.gtspl_sdk.GTSPLWIFIActivity;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button mReadUhfBT,mConnectNet, mDisconnectNet, mPrinterSetup, mPrintBarcode,
            mPrintFontCN, mPrintFontTW, mPrintBmp, mPrintBlock, mGetStatus, mGetSDKVersion,
            mWriteRfid,mWriteAndLockRfid,mSetGjbPwdBtn,mWriteGjbBtn,mReadGjbBtn,mPirntMonoBmpBtn,
            mQueryUhfBT,mCalibrationRfid,mPrintQRcode,mOthersettings,mSetupRfid,mWifiFrequency,mNewPrintBmp,
            mPrintBmpCompress,mWritePCBT,mRWBeforePrint,mRWWithPrint,mSetBicolor;
    EditText mReadUhfET,mIPTxt, mPortTxt, mPrinterStatusTxt, mSDKVersionTxt,mGJBDataET,mQueryUhfET;
    GTSPLWIFIActivity mGtsplWIFICmdTest = new GTSPLWIFIActivity();

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

        mReadUhfET = findViewById(R.id.et_read_uhf);
        mReadUhfBT = findViewById(R.id.bt_read_uhf);
        mQueryUhfET = findViewById(R.id.et_query_uhf);
        mQueryUhfBT = findViewById(R.id.bt_query_uhf);
        mConnectNet = findViewById(R.id.connect);
        mDisconnectNet = findViewById(R.id.disconnect);
        mPrinterSetup = findViewById(R.id.setup);
        mPrintBarcode = findViewById(R.id.print_barcode);
        mPrintFontCN = findViewById(R.id.print_font_cn);
        mPrintFontTW = findViewById(R.id.print_font_tw);
        mPrintBmp = findViewById(R.id.print_bmp);
        mPirntMonoBmpBtn=findViewById(R.id.print_monochrome_bmp);
        mCalibrationRfid = findViewById(R.id.calibration_rfid);
        mPrintQRcode = findViewById(R.id.print_qrcode);
        mOthersettings =findViewById(R.id.othersettings);
        mWifiFrequency = findViewById(R.id.wifi_frequency);
        mSetupRfid =findViewById(R.id.setup_rfid);
        mNewPrintBmp =findViewById(R.id.print_bmp_new);
        mPrintBmpCompress = findViewById(R.id.print_bmp_compress);
        mSetBicolor = findViewById(R.id.set_bicolor);
        mPrintBlock = findViewById(R.id.print_block);
        mGetStatus = findViewById(R.id.get_printer_status);
        mGetSDKVersion = findViewById(R.id.get_sdk_version);
        mWriteRfid =findViewById(R.id.write_rfid);
        mWriteAndLockRfid =findViewById(R.id.write_and_lock_rfid);
        mSetGjbPwdBtn=findViewById(R.id.set_gjb_status_btn);
        mWriteGjbBtn=findViewById(R.id.write_gjb_btn);
        mReadGjbBtn=findViewById(R.id.read_gjb_btn);
        mWritePCBT= findViewById(R.id.bt_pc_uhf);
        mRWBeforePrint= findViewById(R.id.bt_readWrite_before_print_uhf);
        mRWWithPrint= findViewById(R.id.bt_readWritePrint_uhf);

        mReadUhfBT.setOnClickListener(this);
        mQueryUhfBT.setOnClickListener(this);
        mConnectNet.setOnClickListener(this);
        mDisconnectNet.setOnClickListener(this);
        mPrinterSetup.setOnClickListener(this);
        mPrintBarcode.setOnClickListener(this);
        mPrintFontCN.setOnClickListener(this);
        mPrintFontTW.setOnClickListener(this);
        mPrintBmp.setOnClickListener(this);
        mPirntMonoBmpBtn.setOnClickListener(this);
        mPrintBlock.setOnClickListener(this);
        mGetStatus.setOnClickListener(this);
        mGetSDKVersion.setOnClickListener(this);
        mCalibrationRfid.setOnClickListener(this);
        mPrintQRcode.setOnClickListener(this);
        mOthersettings.setOnClickListener(this);
        mWifiFrequency.setOnClickListener(this);
        mSetupRfid.setOnClickListener(this);
        mNewPrintBmp.setOnClickListener(this);
        mPrintBmpCompress.setOnClickListener(this);
        mSetBicolor.setOnClickListener(this);
        mWriteRfid.setOnClickListener(this);
        mWriteAndLockRfid.setOnClickListener(this);
        mSetGjbPwdBtn.setOnClickListener(this);
        mWriteGjbBtn.setOnClickListener(this);
        mReadGjbBtn.setOnClickListener(this);
        mWritePCBT.setOnClickListener(this);
        mRWBeforePrint.setOnClickListener(this);
        mRWWithPrint.setOnClickListener(this);

        mIPTxt = findViewById(R.id.ipaddress);
        mPortTxt = findViewById(R.id.port);
        mPrinterStatusTxt = findViewById(R.id.printer_status_txt);
        mSDKVersionTxt = findViewById(R.id.sdk_version_txt);
        mGJBDataET=findViewById(R.id.ed_read_gjb);

        //set to print mode
        mGtsplWIFICmdTest.GTSPL_setCmdSendMode("P");

        //save CIRCLE.BMP to loacal file
        getAssetsFile();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.connect:
                if (mIPTxt.getText().toString().isEmpty()) {
                    Toast.makeText(this, "The IP column is empty", Toast.LENGTH_SHORT).show();
                } else {
                    if (mPortTxt.getText().toString().isEmpty()) {
                        Toast.makeText(this, "The Port column is empty", Toast.LENGTH_SHORT).show();
                    } else {
                        String ip = mIPTxt.getText().toString().trim();
                        String port = mPortTxt.getText().toString().trim();
                        boolean isConnect = mGtsplWIFICmdTest.GTSPL_openPort(ip, Integer.parseInt(port),4000);
                        if (isConnect) {
                            Toast.makeText(this, "Connect to NET succesfully.", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "Fail to connect to NET.", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                break;
            case R.id.disconnect:
                boolean isDisconnect = mGtsplWIFICmdTest.GTSPL_closePort();
                if (isDisconnect) {
                    Toast.makeText(this, "Disconnect succesfully.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Fail to disconnect.", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.setup:
                mGtsplWIFICmdTest.GTSPL_setup(54, 45, 2, 6, 0, 3, 0, this);
                mGtsplWIFICmdTest.GTSPL_sendCommand(this, "DIRECTION 1\n\n");
                Toast.makeText(this, "Setting Complete!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.print_barcode:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_barcode(30, 30, "128", 100, 1, 0, 2, 2, "barcode1234567", this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.print_font_cn:
                //print simplified chinese font 24*24
                String cnTest = "默认简体\"中文\"测试：海天米醋白米醋1瓶450ml";
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_printerFont(100, 10, "5", 0, 1, 1, "Print \\[\"]Font\\[\"]  123456 ", this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.print_font_tw://print simplified chinese font 16*16
                String twTest = "預設繁體中文測試：海天米醋白米醋1瓶450ml";
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_printerFont(100, 10, "TST24.BF2", 0, 1, 1, twTest, this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.print_bmp:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_downloadBMP("CIRCLE.BMP", this);
                mGtsplWIFICmdTest.GTSPL_sendCommand(this, "PUTBMP 150,30,\"CIRCLE.BMP\"\r\n");
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.print_monochrome_bmp:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_download_Not1BitDepthBMP("penguin.jpg", this);
                mGtsplWIFICmdTest.GTSPL_sendCommand(this, "PUTBMP 10,10,\"penguin.jpg\"\r\n");
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.calibration_rfid:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_labelCalibration(80.0,55.0,0,4.0,1,this);
                break;
            case R.id.print_qrcode:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_qrcode(300, 100, "H", 4, "A", 0, "ABCabc123", this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.othersettings:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_genericDefault(this);
                mGtsplWIFICmdTest.GTSPL_sensorDefault(this);
                mGtsplWIFICmdTest.GTSPL_rfidSetupDefault(this);
                mGtsplWIFICmdTest.GTSPL_setDirectionAndMirror(1, 1, this);
                mGtsplWIFICmdTest.GTSPL_setShift(00, this);
                mGtsplWIFICmdTest.GTSPL_printReverse(160, 160, 160, 160, this);
                mGtsplWIFICmdTest.GTSPL_setOffset(20, this);
                mGtsplWIFICmdTest.GTSPL_setCutMode(0, 2, this);
                mGtsplWIFICmdTest.GTSPL_setAfterPrintAction(3, this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.wifi_frequency:
                mGtsplWIFICmdTest.GTSPL_WifiFrequency("5G",this);
                break;
            case R.id.setup_rfid:
                mGtsplWIFICmdTest.GTSPL_Set_RFIDPorcedure(5,40,32,5,"N",5,5,"300",this);
                Toast.makeText(this, "Setting Complete!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.set_bicolor:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_setBicolor("B",0, this);
                break;
            case R.id.print_block:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                String sBlock = "block 測試 -- 默认简体中文测试：海天米醋白米醋1瓶450ml --";
                mGtsplWIFICmdTest.GTSPL_printFontBlock("15", "15", "790", "90",
                        "TSS24.BF2", "0", "1", "1",
                        "1", "1",sBlock,this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.write_rfid:
                mGtsplWIFICmdTest.GTSPL_writeUHF("H", 2, 12, "E", "11223344556677889900AABB",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                Toast.makeText(this,"Wrote Data：Hex format,block no.= 2, write 12 bytes, EPC Bank, data=11223344556677889900AABB",Toast.LENGTH_LONG).show();
                break;
            case R.id.write_and_lock_rfid:
                mGtsplWIFICmdTest.GTSPL_writeUHF("H", 2, 12, "E", "11223344556677889900AABB",this);
                mGtsplWIFICmdTest.GTSPL_AccessPWD_Action("S","12345678",this);
                mGtsplWIFICmdTest.GTSPL_AccessPWD_Action("L","12345678",this);
                mGtsplWIFICmdTest.GTSPL_EPCPWD_Action("L","12345678",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                Toast.makeText(this,"Wrote Data：as above cmd. Wrote Access PWD:12345678",Toast.LENGTH_LONG).show();
                break;
            case R.id.bt_read_uhf:
                mReadUhfET.setText("");
                final String ans_read = mGtsplWIFICmdTest.GTSPL_readUHF(
                        "H",2,12,"E",MainActivity.this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mReadUhfET.setTextColor(getResources().getColor(
                            R.color.colorAccent, null));
                }

                mReadUhfET.setText("GEN2 EPC: " + ans_read);
                break;
            case R.id.bt_query_uhf:
                mQueryUhfET.setText("");
                final String ans_query = mGtsplWIFICmdTest.GTSPL_readUHFQ(
                        "H",1,0,MainActivity.this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mQueryUhfET.setTextColor(getResources().getColor(
                            R.color.colorAccent, null));
                }
                mQueryUhfET.setText("GEN2 EPC: " + ans_query);
                break;
            case R.id.set_gjb_status_btn: //set GJB Status
                mGtsplWIFICmdTest.GTSPL_Set_GJB_Pwd("S","S","11112222","22221111",this);
                mGtsplWIFICmdTest.GTSPL_Set_GJB_Status("E","A","11112222",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                Toast.makeText(this,"Set EPC Status:R & W",Toast.LENGTH_LONG).show();
                break;
            case R.id.write_gjb_btn: //write gjb epc data
                mGtsplWIFICmdTest.GTSPL_writeGJB("H",2,12,"E","404041414242434344444545","12345678",this);
                mGtsplWIFICmdTest.GTSPL_Set_GJB_Pwd("W","S","87654321","12345678",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                Toast.makeText(this,"Wrote Data：404041414242434344444545",Toast.LENGTH_LONG).show();
                break;
            case R.id.read_gjb_btn: // read gjb epc data
                mGJBDataET.setText("");
                final String result = mGtsplWIFICmdTest.GTSPL_readGJB(
                        "H",2,12,"E","33334444",MainActivity.this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mGJBDataET.setTextColor(getResources().getColor(
                            R.color.colorAccent, null));
                }
                mGJBDataET.setText("GJB EPC : " + result);
                break;
            case R.id.get_printer_status:
                String status = mGtsplWIFICmdTest.GTSPL_printersStatus(1000);
                Log.d("test",status);
                setPrinterStatusToUI(status);

                break;
            case R.id.get_sdk_version:
                String version = mGtsplWIFICmdTest.GTSPL_getSDKVersion(0, this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mSDKVersionTxt.setTextColor(getResources().getColor(R.color.colorAccent, null));
                }
                mSDKVersionTxt.setText("SDK Version:" + version);
                break;
            case R.id.print_bmp_new:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_printBMP(-500,30,300,400,1,"penguin.jpg",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.print_bmp_compress:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_printBMP_Compression(-500,30,300,100,"penguin.jpg",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.bt_pc_uhf:
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_writeUHF("H", 1, 2, "E", "3000",this);
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                break;
            case R.id.bt_readWrite_before_print_uhf:
                mReadUhfET.setText("");
                String RWB_result = mGtsplWIFICmdTest.GTSPL_readUHF(
                        "H",2,12,"E",MainActivity.this);
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_writeUHF("H", 2, 12, "E", "112233445566778800010204",this);
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_sendCommand(this, "TEXT 100,160,\"3\",0,1,1,\"SHIFT 0 CHARACTERS\"\r\n");
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                mReadUhfET.setText("GEN2 EPC: " + RWB_result);
                break;
            case R.id.bt_readWritePrint_uhf:
                mReadUhfET.setText("");
                String RW_result = mGtsplWIFICmdTest.GTSPL_readUHF(
                        "H",2,12,"E",MainActivity.this);
                mGtsplWIFICmdTest.GTSPL_clearBuffer(this);
                mGtsplWIFICmdTest.GTSPL_writeUHF("H", 2, 12, "E", "112233445566778800010204",this);
                mGtsplWIFICmdTest.GTSPL_sendCommand(this, "TEXT 100,160,\"3\",0,1,1,\"SHIFT 0 CHARACTERS\"\r\n");
                mGtsplWIFICmdTest.GTSPL_printLabel(1, 1, this);
                mReadUhfET.setText("GEN2 EPC: " + RW_result);
                break;
        }
    }


    //將assets中檔案存入手機
    private void getAssetsFile() {
        AssetManager assetManager = getAssets();
        InputStream inputStream;
        BufferedInputStream bis;
        ByteArrayOutputStream bos = null;
        try {
            inputStream = assetManager.open("CIRCLE.BMP");
            int dataLength = inputStream.available();
            bis = new BufferedInputStream(inputStream);
            byte[] buffer = new byte[dataLength];
            int len = 0;
            bos = new ByteArrayOutputStream();
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }


            File dir = new File(String.valueOf(getExternalFilesDir("")));
            String pathName = dir.getAbsolutePath() + "/" + "CIRCLE" + ".BMP";
            File file = new File(pathName);
            if (!file.exists()) {
                FileOutputStream output = new FileOutputStream(file);
                output.write(bos.toByteArray());
                output.close();
            }
            bis.close();
            bos.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //印表機狀態
    private void setPrinterStatusToUI(String status) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mPrinterStatusTxt.setTextColor(getResources().getColor(R.color.colorAccent, null));
        }
        switch (status) {
            case "00":
                mPrinterStatusTxt.setText("Printer Status : Normal");
                break;
            case "01":
                mPrinterStatusTxt.setText("Printer Status : Head opened");
                break;
            case "02":
                mPrinterStatusTxt.setText("Printer Status : Paper Jam");
                break;
            case "03":
                mPrinterStatusTxt.setText("Printer Status : Paper Jam and head opened");
                break;
            case "04":
                mPrinterStatusTxt.setText("Printer Status : Out of paper");
                break;
            case "05":
                mPrinterStatusTxt.setText("Printer Status : Out of paper and head opened");
                break;
            case "08":
                mPrinterStatusTxt.setText("Printer Status : Out of ribbon");
                break;
            case "09":
                mPrinterStatusTxt.setText("Printer Status : Out of ribbon and head opened");
                break;
            case "0A":
                mPrinterStatusTxt.setText("Printer Status : Out of ribbon and paper jam");
                break;
            case "0B":
                mPrinterStatusTxt.setText("Printer Status : Out of ribbon, paper jam and head opened");
                break;
            case "0C":
                mPrinterStatusTxt.setText("Printer Status : Out of ribbon and out of paper");
                break;
            case "0D":
                mPrinterStatusTxt.setText("Printer Status : Out of ribbon, out of paper and head opened");
                break;
            case "10":
                mPrinterStatusTxt.setText("Printer Status : Pause");
                break;
            case "20":
                mPrinterStatusTxt.setText("Printer Status : Printing");
                break;
            case "80":
                mPrinterStatusTxt.setText("Printer Status : Other error");
                break;
        }


    }

}
