package com.phlx.wool.ui.main;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

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.Configs;
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;
import com.phlx.wool.ui.villus.BluetoothDeviceList;
import com.phlx.wool.util.TextUtil;
import com.printer.sdk.PrinterInstance;
import com.printer.sdk.utils.PrefUtils;
import com.printer.sdk.utils.XLog;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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

    protected static final String TAG = "MainActivity";


    @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 ("0".equals(Configs.FREQUENCY_CONFIG))
            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();
        });

        //弹出绒毛购买打印选择
        viewModel.printEvent.observe(this, aBoolean -> {

            new MaterialDialog.Builder(this).title("系统信息").content("请选择购买订单!!!")
                    .cancelable(false).canceledOnTouchOutside(false)
                    .positiveText("确定")
                    .items(viewModel.getVillusBuyList())
                    .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
                        @Override
                        public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                            viewModel.printVillusBuy(Configs.myPrinter, text);
                            return true;
                        }
                    }).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);
    }
}
