package com.phlx.anchorcollect_p.ui.setting;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;

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.anchorcollect_p.BR;
import com.phlx.anchorcollect_p.R;
import com.phlx.anchorcollect_p.data.VMFactory;
import com.phlx.anchorcollect_p.databinding.ActivitySettingBinding;
import com.phlx.anchorcollect_p.ui.base.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import cn.wandersnail.commons.util.ToastUtils;
import io.reactivex.functions.Consumer;

public class SettingActivity extends BaseActivity<ActivitySettingBinding, SettingVM> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_setting;
    }

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

    @Override
    public void initParam() {
        super.initParam();
    }

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

    @Override
    public void initData() {
        super.initData();

        viewModel.initBt(this);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.btRequestEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
            }
        });

        viewModel.btParamEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                initialize();
            }
        });

        viewModel.errorEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                showTip(s);
            }
        });

        viewModel.closeEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                finish();
            }
        });
    }


    private void initialize() {

        //请求打开定位权限
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(getNeedPermissions().get(0))
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
//                            ToastUtils.showShort("权限已经打开");
                        } else {
                            ToastUtils.showShort("权限被拒绝");
                        }
                    }
                });

    }

    //需要进行检测的权限
    private List<String> getNeedPermissions() {
        List<String> list = new ArrayList<>();
        if (getApplicationInfo().targetSdkVersion >= 29) {//target sdk版本在29以上的需要精确定位权限才能搜索到蓝牙设备
            list.add(Manifest.permission.ACCESS_FINE_LOCATION);
        } else {
            list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        //Android 12需要
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
//            list.add(Manifest.permission.BLUETOOTH_SCAN);
//            list.add(Manifest.permission.BLUETOOTH_CONNECT);
//        }
        return list;
    }

    private void showTip(String content) {
        new MaterialDialog.Builder(this).title("系统信息").content(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();
                    finish();
                }
            }
        }).show();
    }
}
