package demo.printdemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.sr.SrPrinter;

import recieptservice.com.recieptservice.PSAMCallback;


public class MainActivity extends AppCompatActivity {

    int textSize = 24;

    public static String bytes2hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        String tmp = null;
        for (byte b : bytes) {
            tmp = Integer.toHexString(0xFF & b);
            if (tmp.length() == 1) {
                tmp = "0" + tmp;
            }
            sb.append(tmp);
        }
        return sb.toString();

    }

    public static byte[] StrToHexByte(String str) {
        if (str == null)
            return null;
        else if (str.length() < 2)
            return null;
        else {
            int len = str.length() / 2;
            byte[] buffer = new byte[len];
            for (int i = 0; i < len; i++)
                buffer[i] = (byte) Integer.parseInt(
                        str.substring(i * 2, i * 2 + 2), 16);
            return buffer;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


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

        MainActivity.this.findViewById(R.id.test2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printText(((EditText) findViewById(R.id.test1)).getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.gh));
                    SrPrinter.getInstance(getApplicationContext()).printText(((EditText) findViewById(R.id.test1)).getText().toString() + "\n");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printQRCode("123456", 4, 3);
                    SrPrinter.getInstance(getApplicationContext()).printText(((EditText) findViewById(R.id.test1)).getText().toString() + "\n");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
//                        print128BarCode("12345678901234567890123", 3, 80, 2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).setTextBold(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).setTextSize(--textSize);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test12).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).setTextSize(++textSize);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printText("\n");
                    SrPrinter.getInstance(getApplicationContext()).setAlignment(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printText("\n");
                    SrPrinter.getInstance(getApplicationContext()).setAlignment(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test10).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printText("\n");
                    SrPrinter.getInstance(getApplicationContext()).setAlignment(2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test11).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).nextLine(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test13).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printTableText(new String[]{"宫保鸡丁", "一份", "20元"}, new int[]{1, 1, 1}, new int[]{1, 1, 1});
                    SrPrinter.getInstance(getApplicationContext()).printTableText(new String[]{"鸡丁", "一份", "10元"}, new int[]{1, 1, 1}, new int[]{1, 1, 1});
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainActivity.this.findViewById(R.id.test14).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SrPrinter.getInstance(getApplicationContext()).checkPSAMCard(2000, new PSAMCallback.Stub() {
                    @Override
                    public void success(byte[] data) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (data[0] == 0x59) {
                                    Toast.makeText(getApplicationContext(), "has PSAM card", Toast.LENGTH_LONG).show();
                                } else if (data[0] == 0x4E) {
                                    Toast.makeText(getApplicationContext(), "no PSAM card", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void error(int errorCode, String errorMsg) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });

            }
        });
        MainActivity.this.findViewById(R.id.test15).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                SrPrinter.getInstance(getApplicationContext()).activatePSAMCard(2000, new PSAMCallback.Stub() {
                    @Override
                    public void success(byte[] data) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), bytes2hex(data), Toast.LENGTH_LONG).show();
                            }
                        });
                    }

                    @Override
                    public void error(int errorCode, String errorMsg) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });

            }
        });
        MainActivity.this.findViewById(R.id.test16).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                SrPrinter.getInstance(getApplicationContext()).transmitPSAMCard(2000, StrToHexByte("00a4040005010203040500"), new PSAMCallback.Stub() {
                    @Override
                    public void success(byte[] data) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), bytes2hex(data), Toast.LENGTH_LONG).show();
                            }
                        });
                    }

                    @Override
                    public void error(int errorCode, String errorMsg) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });

            }
        });
        MainActivity.this.findViewById(R.id.test17).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                SrPrinter.getInstance(getApplicationContext()).deactivatePSAMCard(2000, new PSAMCallback.Stub() {
                    @Override
                    public void success(byte[] data) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (data[0] == 0x59) {
                                    Toast.makeText(getApplicationContext(), "deactivatePSAMCard success", Toast.LENGTH_LONG).show();
                                } else if (data[0] == 0x4E) {
                                    Toast.makeText(getApplicationContext(), "deactivatePSAMCard failed", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void error(int errorCode, String errorMsg) throws RemoteException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });
            }
        });
    }

//    void print128BarCode(String data, int type, int height, int width) throws RemoteException {
//        String tmp = new String();
//        if (type == 1) {
//            tmp += "{A" + data;
//        } else if (type == 2) {
//            tmp += "{B" + data;
//        } else if (type == 3) {
//            tmp += "{C";
//            for (int i = 0; i < data.length(); i += 2) {
//                if (i + 1 < data.length()) {
//                    tmp += new String(new byte[]{Byte.parseByte(data.substring(i, i + 2), 10)});
//                } else {
//                    tmp += "{B" + data.substring(i, i + 1);
//                }
//            }
//        }
//        SrPrinter.getInstance(getApplicationContext()).printBarCode(tmp, 73, height, width);
//    }
}
