package com.siwei.recyclebox.ui.main;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.util.Log;

import com.siwei.recyclebox.BR;
import com.siwei.recyclebox.R;
import com.siwei.recyclebox.service.MyService;


import me.goldze.mvvmhabit.base.BaseActivity;


public class MainActivity extends BaseActivity<ViewDataBinding,MainViewModel> {

    private static final String TAG = "MainActivity";

    //页面接收的参数方法
    @Override
    public void initParam() {
        super.initParam();
        Intent service = new Intent(getApplication(), MyService.class);
        getApplication().startService(service);
    }

    //页面数据初始化方法。
    @Override
    public void initData() {
//        viewModel.btnClick.onClick();
//        viewModel.btnText.set("");
        openBlueTooth();
        viewModel.registerAliIoTListener();
//        viewModel.startJobScheduler();
//        viewModel.repeatTask();
        Log.i("repeatTask","*****************************************");
        viewModel.startBLEServer();
//        viewModel.netWorkInfo();
//        viewModel.openWeight();
//        System.out.println("serialPortUtil.readControllerStatus:"+ SerialPortUtil.getInstance().readRelayControllerStatus());
    }

    public void openBlueTooth(){
//      打开蓝牙（提示对话框）
//        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
//        startActivity(discoverableIntent);

//        打开蓝牙（静默，无提示）
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothAdapter.enable();//需要BLUETOOTH_ADMIN权限

    }
    public void closeBlueTooth(){
//      关闭蓝牙
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothAdapter.disable();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.requestFilePermissions.observe(this, aBoolean -> {
            //请求读写文件的权限。
            Log.d(TAG,"请求文件读写的权限");
            /*RxPermissions permissions = new RxPermissions(this);
            Disposable disposable =permissions.request(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE})
                .subscribe(granted->{
                    if(granted){
                        Log.d(TAG,"文件读写权限以获取！");
                        viewModel.otaListener();
                    }else{
                        ToastUtils.showShortSafe("请同意文件读写权限。");

                    }
            });*/

        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {

//        viewModel.closePort();
        viewModel.stopBleServer();
        viewModel.unRegisterIoTListener();
        Log.e("MainActivity.","unregisterIoTListener!!!");
        Intent mStartActivity = new Intent(getApplication(),MainActivity.class);
        int mPendingIntentId = 123456;
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplication(), mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0);
        super.onDestroy();

    }

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

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

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        System.out.println("onPointerCaptureChanged:"+hasCapture);
    }

    @Override
    public MainViewModel initViewModel() {
        //View持有ViewModel的引用，如果没有特殊业务处理，这个方法可以不重写
        return super.initViewModel();
    }
}
