package com.icod.yk_printer_test;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.szsicod.print.escpos.PrinterAPI;
import com.szsicod.print.log.Logger;

public class DipSetActivity extends ActionBarActivity {

    private static int[] sw1 = {1, 1, 1, 1, 1, 1, 1, 1};
    private static int[] sw2 = {1, 1, 1, 1, 1, 1, 1, 2};
    byte[] cmd = new byte[14];
    private Switch sw1_1;
    private Switch sw1_2;
    private Switch sw1_3;
    private Switch sw1_4;
    private Switch sw1_5;
    private Switch sw1_6;
    private Switch sw1_7;
    private Switch sw1_8;
    private Switch sw2_1;
    private Switch sw2_2;
    private Switch sw2_3;
    private Switch sw2_4;
    private Switch sw2_5;
    private Switch sw2_6;
    private Switch sw2_7;
    private Switch sw2_8;
    private Button buttonSendOrder;
    private Button buttonZJ;
    private ProgressDialog progressDialog;
    private int serialFlowControl = 0;
    private Runnable runnable;
    private TextView tvSwitch1Value;//存储打印中原来的值
    private TextView tvSwitch2Value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dip_set);
        final Context context = this;
        setViews();
        getPrinterSwitchValue();
        setListeners();

    }

    private void getPrinterSwitchValue() {
        if(!PrinterAPI.getInstance().isConnect())return;
        int switch1Value = PrinterAPI.getInstance().getSwitch1Value();
        if (switch1Value<0){
            Logger.i("值获取错误");
        }
        tvSwitch1Value.setText(" "+switch1Value);
        int switch2Value = PrinterAPI.getInstance().getSwitch2Value();
        if (switch2Value<0){
            Logger.i("值获取错误");
        }
        tvSwitch2Value.setText(" "+switch2Value);
        setValue(switch1Value,1,sw1_1);
        setValue(switch1Value,1<<1,sw1_2);
        setValue(switch1Value,1<<2,sw1_3);
        setValue(switch1Value,1<<3,sw1_4);
        setValue(switch1Value,1<<4,sw1_5);
        setValue(switch1Value,1<<5,sw1_6);
        setValue(switch1Value,1<<6,sw1_7);
        setValue(switch1Value,1<<7,sw1_8);
        setValue(switch2Value,1,sw2_1);
        setValue(switch2Value,1<<1,sw2_2);
        setValue(switch2Value,1<<2,sw2_3);
        setValue(switch2Value,1<<3,sw2_4);
        setValue(switch2Value,1<<4,sw2_5);
        setValue(switch2Value,1<<5,sw2_6);
        setValue(switch2Value,1<<6,sw2_7);
        setValue(switch2Value,1<<7,sw2_8);
    }

    private void setValue(int switch1Value,int checkValue,Switch sw) {
        if ((switch1Value&checkValue)==0){
            sw.setChecked(true);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    progressDialog.dismiss();// 销毁对话框
                    Toast.makeText(DipSetActivity.this, "操作成功",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    progressDialog.dismiss();// 销毁对话框
                    Toast.makeText(DipSetActivity.this, "操作失败",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private void progressRun() {
        progressDialog = new ProgressDialog(DipSetActivity.this);
        progressDialog.setTitle("提示:");
        progressDialog.setMessage("Please waiting...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条对话框//样式（水平，旋转）
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    private void setViews() {
        tvSwitch1Value= (TextView) findViewById(R.id.tv_sw1_value);
        tvSwitch2Value= (TextView) findViewById(R.id.tv_sw2_value);
        findViewById(R.id.button_get_value).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPrinterSwitchValue();
            }
        });
        sw1_1 = (Switch) findViewById(R.id.switch1);
        sw1_2 = (Switch) findViewById(R.id.switch2);
        sw1_3 = (Switch) findViewById(R.id.switch3);
        sw1_4 = (Switch) findViewById(R.id.switch4);
        sw1_5 = (Switch) findViewById(R.id.switch5);
        sw1_6 = (Switch) findViewById(R.id.switch6);
        sw1_7 = (Switch) findViewById(R.id.switch7);
        sw1_8 = (Switch) findViewById(R.id.switch8);

        sw2_1 = (Switch) findViewById(R.id.switch2_1);
        sw2_2 = (Switch) findViewById(R.id.switch2_2);
        sw2_3 = (Switch) findViewById(R.id.switch2_3);
        sw2_4 = (Switch) findViewById(R.id.switch2_4);
        sw2_5 = (Switch) findViewById(R.id.switch2_5);
        sw2_6 = (Switch) findViewById(R.id.switch2_6);
        sw2_7 = (Switch) findViewById(R.id.switch2_7);
        sw2_8 = (Switch) findViewById(R.id.switch2_8);
        buttonSendOrder = (Button) findViewById(R.id.button_send_order);
        buttonZJ = (Button) findViewById(R.id.button_zj);
    }

    private void setListeners() {
        buttonSendOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchCheck();
            }
        });
        buttonZJ.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (PrinterAPI.getInstance().isConnect())
                PrinterAPI.getInstance().selfTestPage();
            }
        });
    }

    protected void switchCheck() {

        if (!PrinterAPI.getInstance().isConnect())return;
        //BM sensor
        progressRun();
        if (sw1_1.isChecked()) {
            //Enable
            sw1[0] = 0;
        } else {
            //Disable
            sw1[0] = 1;
        }

        if (sw1_2.isChecked()) {
            //Enable
            sw1[1] = 0;
        } else {
            //Disable
            sw1[1] = 1;
        }
        if (sw1_3.isChecked()) {
            //Enable
            sw1[2] = 0;
        } else {
            //Disable
            sw1[2] = 1;
        }

        //PE signal OutPut
        if (sw1_4.isChecked()) {
            //Disable
            sw1[3] = 0;
        } else {
            //Enable
            sw1[3] = 1;
        }

        //PNE Status
        if (sw1_5.isChecked()) {
            //Reverse
            sw1[4] = 0;
        } else {
            //Normal
            sw1[4] = 1;
        }
        //PNE Status
        if (sw1_6.isChecked()) {
            //Reverse
            sw1[5] = 0;
        } else {
            //Normal
            sw1[5] = 1;
        }

        if (sw1_7.isChecked()) {//Transmission speed
            sw1[6] = 0;
        } else {
            //Normal
            sw1[6] = 1;
        }
        if (sw1_8.isChecked()) {//Transmission speed
            sw1[7] = 0;
        } else {
            //Normal
            sw1[7] = 1;
        }


        if (sw2_1.isChecked()) {
            sw2[0] = 0;
        } else {
            sw2[0] = 1;
        }

        if (sw2_2.isChecked()) {
            sw2[1] = 0;
        } else {
            sw2[1] = 1;
        }

        if (sw2_3.isChecked()) {
            sw2[2] = 0;
        } else {
            sw2[2] = 1;
        }

        if (sw2_4.isChecked()) {
            sw2[3] = 0;
        } else {
            sw2[3] = 1;
        }
        if (sw2_5.isChecked()) {
            sw2[4] = 0;
        } else {
            sw2[4] = 1;
        }

        if (sw2_6.isChecked()) {
            sw2[5] = 0;
        } else {
            sw2[5] = 1;
        }

        if (sw2_7.isChecked()) {
            sw2[6] = 0;
        } else {
            sw2[6] = 1;
        }
        if (sw2_8.isChecked()) {
            sw2[7] = 0;
        } else {
            sw2[7] = 1;
        }


        runnable = new Runnable() {

            @Override
            public void run() {

                byte swt1 = (byte) (sw1[7] * 128 + sw1[6] * 64 + sw1[5] * 32 + sw1[4] * 16 + sw1[3] * 8 + sw1[2] * 4 + sw1[1] * 2 + sw1[0] * 1);
                byte swt2 = (byte) (sw2[7] * 128 + sw2[6] * 64 + sw2[5] * 32 + sw2[4] * 16 + sw2[3] * 8 + sw2[2] * 4 + sw2[1] * 2 + sw2[0] * 1);
                cmd[0] = 0x1d;
                cmd[1] = 0x28;
                cmd[2] = 0x42;
                cmd[3] = 0x02;
                cmd[4] = 0x00;
                cmd[5] = 0x00;
                cmd[6] = (byte) swt1;
                cmd[7] = 0x1d;
                cmd[8] = 0x28;
                cmd[9] = 0x42;
                cmd[10] = 0x02;
                cmd[11] = 0x00;
                cmd[12] = 0x01;
                cmd[13] = (byte) swt2;
                Log.i("info", "cmd[6]" + cmd[6]);
                Log.i("info", "cmd[13]" + cmd[13]);
                if (PrinterAPI.SUCCESS == PrinterAPI.getInstance().sendOrder(cmd)) {
                    handler.sendEmptyMessage(0);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        };
        new Thread(runnable).start();
    }


}


