package com.phlx.wool.ui.inventory;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.phlx.wool.App;
import com.phlx.wool.BR;
import com.phlx.wool.R;
import com.phlx.wool.data.VMFactory;
import com.phlx.wool.databinding.ActivityInventoryBinding;
import com.phlx.wool.rfid.ModuleManager;
import com.phlx.wool.ui.base.BaseActivity;
import com.uhf.structures.InventoryParams;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/26
 *     desc   : 盘点标签页面
 *     version: 1.0
 * </pre>
 */
public class InventoryActivity extends BaseActivity<ActivityInventoryBinding, InventoryVM> {



    private String type = null;

    @Override
    public void initParam() {
        super.initParam();
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null) {
            type = mBundle.getString("type");
        }
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_inventory;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public InventoryVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(InventoryVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        setSupportActionBar(binding.include.toolbar);
        viewModel.initToolbar(type);
        binding.setAdapter(new BindingRecyclerViewAdapter());
        if (ModuleManager.powerOn() != 0) {
            new MaterialDialog.Builder(this).title("系统提示").content("模块连接失败").cancelable(false).canceledOnTouchOutside(false).positiveText("退出").onAny(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        App.getInstance().getLinkage().stopInventory();
                        ModuleManager.destroyLibSO();
                        finish();
                    }
                }
            }).show();
        }
        //设置盘点内容：Epc + Tid
        InventoryParams inventoryParams = new InventoryParams();
        inventoryParams.address = 0;
        inventoryParams.inventoryArea = 1;
        inventoryParams.len = 6;
        App.getInstance().getLinkage().Radio_SetInventoryParams(inventoryParams);



    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.inventoryBtnEvent.observe(this, aBoolean -> {
            if (binding.inventory.getText().equals("盘点")) {
                binding.inventory.setText("停止");
                binding.progressBar.setVisibility(View.VISIBLE);
                binding.inventory.setBackground(getResources().getDrawable(R.drawable.inventory_stop_btn_bg));
                App.getInstance().getLinkage().startInventory(1, 0);
            } else {
                binding.inventory.setText("盘点");
                binding.inventory.setBackground(getResources().getDrawable(R.drawable.inventory_start_btn_bg));
                App.getInstance().getLinkage().stopInventory();
                binding.progressBar.setVisibility(View.GONE);
            }

        });

        viewModel.labelCheckEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String count) {
                new MaterialDialog.Builder(InventoryActivity.this).title("系统提示").content(
                        "标签中含有" + count + "个未记录的标签，是否继续？").positiveText("确定").negativeText("取消").onAny(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog,
                                        @NonNull DialogAction which) {
                        if (which == DialogAction.POSITIVE) {
                            viewModel.jumpToActivity();
                        }
                    }
                }).show();
            }
        });


    }
}
