package com.phlx.anchorcollect_p.ui.home;

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

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

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.phlx.anchorcollect_p.BR;
import com.phlx.anchorcollect_p.R;
import com.phlx.anchorcollect_p.data.VMFactory;
import com.phlx.anchorcollect_p.databinding.ActivityHomeBinding;
import com.phlx.anchorcollect_p.rfid.ModuleManager;
import com.phlx.anchorcollect_p.ui.base.BaseActivity;
import com.phlx.anchorcollect_p.util.StatusBarUtils;

import me.jessyan.autosize.utils.ScreenUtils;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/17
 *     desc   : 菜单页
 *     version: 1.0
 * </pre>
 */
public class HomeActivity extends BaseActivity<ActivityHomeBinding, HomeVM> {

    protected static final String TAG = "MainActivity";


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

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


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

    @Override
    public void initData() {
        super.initData();
        int[] size = ScreenUtils.getRawScreenSize(this);
        Log.e(TAG, "initData: ---w---" + size[0] + "---h---" + size[1]);
        Log.e(TAG, "initData: ---StatusBar------" + StatusBarUtils.getStatusBarHeight(this));
        Log.e(TAG, "initData: ---ActionBar------" + StatusBarUtils.getActionBarHeight(this));


    }

    @Override
    public void initViewObservable() {


    }


    /**
     * 监听Back键按下事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            MessageDialog.show("系统提示", "是否退出程序?", "确定", "取消")
//                    .setOkButton(new OnDialogButtonClickListener<MessageDialog>() {
//                        @Override
//                        public boolean onClick(MessageDialog baseDialog, View v) {
//                            ModuleManager.destroyLibSO();
//                            finish();
//                            android.os.Process.killProcess(android.os.Process.myPid());
//                            return false;
//                        }
//                    })
//            .setCancelButton(new OnDialogButtonClickListener<MessageDialog>() {
//                @Override
//                public boolean onClick(MessageDialog baseDialog, View v) {
//                    return false;
//                }
//            })
//            ;


            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);
    }
}
