package com.prttech.barcodedemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.prttech.sdl.SDLHelper;
import com.prttech.sdl.listener.ISDLListener;

public class MainActivity extends Activity {

    private  static  final String TAG="MainActivity";

    private boolean beepMode 		= true;
    private boolean ISDEBUG 		= true;
    // ui
    private TextView tvStat = null;
    private TextView tvData = null;
    private EditText edPnum = null;
    private EditText edPval = null;
    private CheckBox chBeep = null;
    private CheckBox chBeep_RP 				= null;

    private static final int MSG_DECODE_STATE=1001;
    private static final int MSG_DECODE_RESULT=1002;
    private static final int MSG_DECODE_PROP=1003;
    private static final int MSG_DECODE_PARAM=1004;
    private static final int MSG_DECODE_IMAGEPATH=1005;
    private Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_DECODE_STATE:
                    tvStat.setText((String) msg.obj);
                    break;
                case MSG_DECODE_RESULT:
                    tvData.setText((String) msg.obj);
                    break;
                case MSG_DECODE_PROP:
                    AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                    if (dlg != null)
                    {
                        dlg.setTitle("SDL Properties");
                        dlg.setMessage((String) msg.obj);
                        dlg.setPositiveButton("ok", null);
                        dlg.show();
                    }
                    break;
                case MSG_DECODE_PARAM:
                    edPval.setText(Integer.toString(msg.arg1));
                    break;
                case MSG_DECODE_IMAGEPATH:
                    Log.d(TAG, "MSG_DECODE_IMAGEPATH "+(String) msg.obj);
                    break;
                default:
                    Log.d("liuy=default=", "handler");
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainScreen();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        SDLHelper.init(this);
        listenResult();
    }

    @Override
    protected void onResume() {
        SDLHelper.getInstance().bindService(this);
        super.onResume();
    }

    private void sendHandler(int msgDecodeState, String state) {
        Message msg = mhandler.obtainMessage();
        msg.what=msgDecodeState;
        msg.obj=state;
        mhandler.sendMessage(msg);
    }
    private void sendHandlerint(int msgDecodeState, int val) {
        Message msg = mhandler.obtainMessage();
        msg.what=msgDecodeState;
        msg.arg1=val;
        mhandler.sendMessage(msg);
    }
    @Override
    protected void onPause() {
        SDLHelper.getInstance().unBindService(this);
        super.onPause();
    }
    // create main screen
    private void mainScreen()
    {
        setContentView(R.layout.activity_main);
        ((Button) findViewById(R.id.buttonDec)).setOnClickListener(mDecodeListener);
        ((Button) findViewById(R.id.buttonHF)).setOnClickListener(mHandsFreeListener);
        ((Button) findViewById(R.id.buttonGet)).setOnClickListener(mGetParamListener);
        ((Button) findViewById(R.id.buttonSet)).setOnClickListener(mSetParamListener);
        ((Button) findViewById(R.id.buttonDfl)).setOnClickListener(mDflParamListener);
        ((Button) findViewById(R.id.buttonProp)).setOnClickListener(mPropListener);
        ((Button) findViewById(R.id.buttonEnable)).setOnClickListener(mEnableAllListener);
        ((Button) findViewById(R.id.buttonDisable)).setOnClickListener(mDisableAllListener);
        ((Button) findViewById(R.id.buttonDecImage)).setOnClickListener(mGetDecodedImageListener);

        ((CheckBox) findViewById(R.id.checkBeep)).setOnClickListener(mCheckBeepListener);
        ((CheckBox) findViewById(R.id.checkReadingPane)).setOnClickListener(mCheckReadingPaneListener);

        // ui items
        tvStat = (TextView) findViewById(R.id.textStatus);
        tvData = (TextView) findViewById(R.id.textDecode);
        edPnum = (EditText) findViewById(R.id.editPnum);
        edPval = (EditText) findViewById(R.id.editPval);
        chBeep = (CheckBox) findViewById(R.id.checkBeep);
        chBeep.setChecked(beepMode);

        chBeep_RP = (CheckBox) findViewById(R.id.checkReadingPane);
        chBeep_RP.setChecked(false);

    }

    private void listenResult(){
        SDLHelper.getInstance().setIsdlListener(new ISDLListener() {
            @Override
            public void SDLResult(String result) {
                sendHandler(MSG_DECODE_RESULT,result);
            }

            @Override
            public void SDLState(String state) {
                sendHandler(MSG_DECODE_STATE,state);
            }

            @Override
            public void SDLProp(String Prop) {
                sendHandler(MSG_DECODE_PROP,Prop);
            }

            @Override
            public void SDLParam(int val) {
                sendHandlerint(MSG_DECODE_PARAM,val);
            }

            @Override
            public void SDLImagepath(String path) {
                sendHandler(MSG_DECODE_IMAGEPATH,path);
            }
        });
    }

    // callback for beep checkbox
    OnClickListener mCheckBeepListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().setBeepMode(((CheckBox) v).isChecked());
        }
    };

    // ------------------------------------------------------
    // callback for beep checkbox
    OnClickListener mCheckReadingPaneListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().setReadingPane(((CheckBox) v).isChecked());
        }

    };

    // ------------------------------------------------------
    // callback for decode button press
    OnClickListener mDecodeListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().doDecode();
        }
    };

    // ------------------------------------------------------
    // callback for HandsFree button press
    OnClickListener mHandsFreeListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().doHandsFree();
        }
    };

    // ------------------------------------------------------
    // callback for properties button press
    OnClickListener mPropListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().doGetProp();
        }
    };

    // ------------------------------------------------------
    // callback for decode button press
    OnClickListener mDflParamListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().doDefaultParams();
        }
    };

    // ------------------------------------------------------
    // callback Get Param for button press
    OnClickListener mGetParamListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            try {
                SDLHelper.getInstance().getParam(edPnum.getText().toString());
            }catch (Exception e){
                tvStat.setText("value ERROR");
            }
        }
    };

    // ------------------------------------------------------
    // callback enable all parameters for button press
    OnClickListener mEnableAllListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().EnableAll();
        }
    };

    // ------------------------------------------------------
    // callback Disable all parameters for button press
    OnClickListener mDisableAllListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().DisableAll();
        }
    };

    // ------------------------------------------------------
    // callback Get Last Decoded image for button press
    OnClickListener mGetDecodedImageListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            SDLHelper.getInstance().getDecodedImage();

        }
    };

    // ------------------------------------------------------
    // callback for Set Param button press
    OnClickListener mSetParamListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            try {
                SDLHelper.getInstance().setParam(edPnum.getText().toString(), edPval.getText().toString());
            }catch (Exception e){
                tvStat.setText("value ERROR");
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_F12||keyCode == KeyEvent.KEYCODE_F11 || keyCode == 286){
            if((event.getRepeatCount() == 0)) {
                SDLHelper.getInstance().doDecode();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
