package com.sjkj.appthreefloor_tsgz.activity;

import android.content.Intent;
import android.os.Build;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.viewModel.MainViewModel;
import com.sjkj.appthreefloor_tsgz.adapter.PermissionListAdapter;
import com.sjkj.appthreefloor_tsgz.bean.PermissionBean;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityPermissionListBinding;
import com.sjkj.appthreefloor_tsgz.login.LoginActivity;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;

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

public class PermissionListActivity extends BaseActivity {
    private MainViewModel viewModel;
    private ActivityPermissionListBinding binding;
    private PermissionListAdapter adapter;
    private List<PermissionBean> list = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_permission_list;
    }

    @Override
    public void initBinding() {
        super.initBinding();
        binding = (ActivityPermissionListBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void initView() {
        super.initView();
        getBackView().setVisibility(View.GONE);
        getTitleView().setText("权限列表");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        binding.recyclerView.setLayoutManager(manager);
        adapter = new PermissionListAdapter(this);
        binding.recyclerView.setAdapter(adapter);
        binding.settingTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                XXPermissions.startPermissionActivity(PermissionListActivity.this);
            }
        });
        binding.settingFinishTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (adapter.getList() != null && adapter.getList().size() > 0) {
                    for (int i = 0; i < adapter.getList().size(); i++) {
                        if (!adapter.getList().get(i).isGranted()) {
                            ToastUtils.show("请授予全部权限");
                            return;
                        }
                    }
                }
                Intent intent = new Intent(PermissionListActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        list.clear();
        PermissionBean bean = new PermissionBean();
        bean.setName("摄像头权限");
        bean.setKey(Permission.CAMERA);
        list.add(bean);
        PermissionBean bean1 = new PermissionBean();
        bean1.setName("拨打电话权限");
        bean1.setKey(Permission.CALL_PHONE);
        list.add(bean1);
        PermissionBean bean5 = new PermissionBean();
        bean5.setName("位置信息权限");
        bean5.setKey(Permission.ACCESS_FINE_LOCATION);
        list.add(bean5);
        PermissionBean bean2 = new PermissionBean();
        bean2.setName("麦克风权限");
        bean2.setKey(Permission.RECORD_AUDIO);
        list.add(bean2);
        PermissionBean bean22 = new PermissionBean();
        bean22.setName("通知权限");
        bean22.setKey(Permission.NOTIFICATION_SERVICE);
        list.add(bean22);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            PermissionBean bean3 = new PermissionBean();
            bean3.setName("存储权限");
            bean3.setKey(Permission.MANAGE_EXTERNAL_STORAGE);
            list.add(bean3);
        } else {
            PermissionBean bean3 = new PermissionBean();
            bean3.setName("存储权限");
            bean3.setKey(Permission.WRITE_EXTERNAL_STORAGE);
            list.add(bean3);
            PermissionBean bean33 = new PermissionBean();
            bean33.setName("存储权限");
            bean33.setKey(Permission.READ_EXTERNAL_STORAGE);
            list.add(bean33);
        }

        adapter.refreshList(list);

    }

}
