package com.example.blu.toys;

import android.Manifest;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.example.blu.toys.activity.AddDeviceActivity;
import com.example.blu.toys.activity.BaseActivity;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

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

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

	@BindView(R.id.stoplight_but)
	LinearLayout stoplightBut;

	String[] permissions = new String[]{
			Manifest.permission.ACCESS_COARSE_LOCATION,
			Manifest.permission.ACCESS_FINE_LOCATION,
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_PHONE_STATE
	};
	// 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
	List<String> mPermissionList = new ArrayList<>();

	private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
	private static final int MY_PERMISSIONS_REQUEST_CALL_CAMERA = 2;
	private BluetoothHelper bluetoothHelper;


	@Override
	public int getLayout() {
		return R.layout.activity_main;
	}

	@Override
	public void init(Bundle savedInstanceState) {

//		for (int i = 0; i < permissions.length; i++) {
//			if (ContextCompat.checkSelfPermission(MainActivity.this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
//				mPermissionList.add(permissions[i]);
//			}
//		}
//		if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
//			Toast.makeText(MainActivity.this, "已经授权", Toast.LENGTH_LONG).show();
//		} else {//请求权限方法
//			String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
//			ActivityCompat.requestPermissions(MainActivity.this, permissions, MY_PERMISSIONS_REQUEST_CALL_CAMERA);
//		}


	}

	private void checkPermission() {
		XXPermissions.with(this) // 申请安装包权限
				.permission(Permission.CAMERA, Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_FINE_LOCATION) // 申请多个权限
				.request(new OnPermission() {
							 @Override
							 public void hasPermission(List<String> granted, boolean all) {
								 if (!all) {
									 checkPermission();
								 }

							 }

							 @Override
							 public void noPermission(List<String> denied, boolean never) {
								 if (never) {
									 XXPermissions.startPermissionActivity(MainActivity.this, denied);
								 } else {
									 checkPermission();
								 }

							 }
						 }

				);
	}


	@Override
	public void initData() {
		checkPermission();




	}

	@OnClick(R.id.stoplight_but)
	public void onViewClicked() {
		goActivity(AddDeviceActivity.class);
	}


//	@Override
//	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//		if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
//			if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//				showToast("权限已申请");
//			} else {
//				showToast("权限已拒绝");
//			}
//		} else if (requestCode == MY_PERMISSIONS_REQUEST_CALL_CAMERA) {
//			for (int i = 0; i < grantResults.length; i++) {
//				if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
//					//判断是否勾选禁止后不再询问
//					boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permissions[i]);
//					if (showRequestPermission) {
//						showToast("权限未申请");
//					}
//				}
//			}
//		}
//		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//	}
//
//	private void showToast(String string) {
//		Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
//	}


//	private void checkBluetoothPermission() {
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//			//校验是否已具有模糊定位权限
//			if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//				PermissionManager.instance().with(this).request(new OnPermissionCallback() {
//					@Override
//					public void onRequestAllow(String permissionName) {
//						enableBluetooth();
//					}
//
//					@Override
//					public void onRequestRefuse(String permissionName) {
//						finish();
//					}
//
//					@Override
//					public void onRequestNoAsk(String permissionName) {
//						finish();
//					}
//				}, Manifest.permission.ACCESS_COARSE_LOCATION);
//			} else {
//				enableBluetooth();
//			}
//		} else {
//			enableBluetooth();
//		}
//	}
//
//	private void enableBluetooth() {
//		if (!BleUtil.isBleEnable(this)) {
//			BleUtil.enableBluetooth(this, 1);
//		} else {
//			boolean isSupport = BleUtil.isSupportBle(this);
//			boolean isOpenBle = BleUtil.isBleEnable(this);
//			if (isSupport) {
//				supportTv.setText(getString(R.string.supported));
//			} else {
//				supportTv.setText(getString(R.string.not_supported));
//			}
//			if (isOpenBle) {
//				statusTv.setText(getString(R.string.on));
//			} else {
//				statusTv.setText(getString(R.string.off));
//			}
//			invalidateOptionsMenu();
//			updateConnectedDevice();
//		}
//	}


}