package com.phlx.wool.ui.main;

import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.phlx.wool.BR;
import com.phlx.wool.R;
import com.phlx.wool.data.VMFactory;
import com.phlx.wool.databinding.ActivityMainBinding;
import com.phlx.wool.rfid.ModuleManager;
import com.phlx.wool.ui.base.BaseActivity;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/17
 *     desc   : 菜单页
 *     version: 1.0
 * </pre>
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, MainVM> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

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


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

    @Override
    public void initData() {
        super.initData();
        setSupportActionBar(binding.include.toolbar);
        viewModel.initToolbar();
        if (ModuleManager.openCom() != 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) {
                        ModuleManager.destroyLibSO();
                        finish();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                }
            }).show();
        }
    }

    @Override
    public void initViewObservable() {
        viewModel.drawerEvent.observe(this,
                aBoolean -> binding.drawerLeft.openDrawer(GravityCompat.START));
        viewModel.nullEvent.observe(this, aBoolean -> {
            new MaterialDialog.Builder(this).title("系统信息").content("请先选择养殖场!!!")
                    .cancelable(false).canceledOnTouchOutside(false)
                    .positiveText("确定").onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                    }
                }
            }).show();
        });
    }

    /**
     * 监听Back键按下事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new MaterialDialog.Builder(this).title("系统提示").content("是否退出程序?").positiveText("确定").negativeText("取消").onAny(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        ModuleManager.destroyLibSO();
                        finish();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                }
            }).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
