package com.philisense.rdgo.ui.replace;

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.ActivityReplaceNewBinding;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

public class ReplaceNewActivity extends AppCompatActivity {

    public ActivityReplaceNewBinding mBinding;
    private ScanDecode scanDecode;
    private String oldBoxId;

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

        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("扫码替换新箱");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                finish();
            }
        });

        oldBoxId = getIntent().getStringExtra("oldBoxId");

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

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


        mBinding.determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=mBinding.edit.getText().toString().trim();
                ReplaceReviewActivity.start(ReplaceNewActivity.this, oldBoxId,s);
                finish();
            }
        });

    }

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


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

}
