package com.philisense.rdgo.ui.remove;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
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.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.databinding.ActivityRemoveBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.viewModel.RemoveViewModel;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

public class RemoveActivity extends AppCompatActivity {

    ActivityRemoveBinding mBinding;
    private ScanDecode scanDecode;
    private RemoveViewModel viewModel;

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

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

        viewModel = new RemoveViewModel(this);

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

        scanDecode.getBarCode(new ScanInterface.OnScanListener() {
            @Override
            public void getBarcode(String s) {
                scanDecode.stopScan();
               String ss = s.substring(s.lastIndexOf("/") + 1);
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(RemoveActivity.this);
                dialogBuilder.setTitle("提示");
                dialogBuilder.setMessage("您确定要删除该箱（" + ss + "）吗？");
                dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mBinding.edit.setText("");
                    }
                });
                dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        check(ss);
                    }
                });
                dialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mBinding.edit.setText("");
                    }
                });
                dialogBuilder.show();

            }
        });




        mBinding.determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a=mBinding.edit.getText().toString().trim();
                String s = a.substring(a.lastIndexOf("/") + 1);
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(RemoveActivity.this);
                dialogBuilder.setTitle("提示");
                dialogBuilder.setMessage("您确定要删除该箱（" + s + "）吗？");
                dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mBinding.edit.setText("");
                    }
                });
                dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        check(s);
                    }
                });
                dialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mBinding.edit.setText("");
                    }
                });
                dialogBuilder.show();



            }
        });

    }

    /**
     * 确认信息
     */
    public void check(String boxId){

        mBinding.edit.setText("");

        new AlertDialog.Builder(this).setTitle("确认要剔除吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Remove(boxId);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    public void Remove(String boxId) {
        final ProgressDialog dialog = ProgressDialog.show(RemoveActivity.this,"提示","正在录入..");
        viewModel.removeAction(boxId, new RequestImpl<String>() {
            @Override
            public void loadSuccess(String bean) {
                dialog.dismiss();
                Toast.makeText(RemoveActivity.this, "剔除成功",Toast.LENGTH_LONG).show();
                finish();
            }

            @Override
            public void loadFailed(OError error) {
                dialog.dismiss();
                Snackbar.make(mBinding.removeLayout,error.getErrorMsg(),Snackbar.LENGTH_LONG).show();
            }
        });
    }

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

    public static void start(Activity context) {
        Intent intent = new Intent(context, RemoveActivity.class);
        ActivityCompat.startActivity(context, intent,null);
    }
}
