package com.philisense.rdgo.ui.Query;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.philisense.rdgo.R;
import com.philisense.rdgo.databinding.ActivityPutInBinding;
import com.philisense.rdgo.databinding.ActivityQueryBinding;
import com.philisense.rdgo.ui.putIn.PutInOrderActivity;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

public class QueryActivity extends AppCompatActivity {

    public ActivityQueryBinding binding;
    private ScanDecode scanDecode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_query);

        Toolbar toolbar = binding.include.toolbar;
        toolbar.setTitle("扫码查询");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finish();
            }
        });

        // 初始化扫描
        try {
            scanDecode = new ScanDecode(this);
            scanDecode.initService("true");
        } catch (Exception e) {
            Snackbar.make(binding.putInRootLayout, "本机没有扫码功能", Snackbar.LENGTH_LONG).show();
        }

        scanDecode.getBarCode(new ScanInterface.OnScanListener() {
            @Override
            public void getBarcode(String s) {
                scanDecode.stopScan();
                QueryNextActivity.start(QueryActivity.this, s);
                finish();
            }
        });


        binding.determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = binding.edit.getText().toString().trim();
                QueryNextActivity.start(QueryActivity.this, s);
                finish();

            }
        });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            scanDecode.onDestroy();
        } catch (Exception e) {
        }
    }


    /**
     * @param context activity
     */
    public static void start(Activity context) {
        Intent intent = new Intent(context, QueryActivity.class);
        ActivityCompat.startActivity(context, intent, null);
    }
}
