package com.casic.dcms.view;

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

import androidx.annotation.NonNull;

import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityPermssionBinding;
import com.casic.dcms.utils.LocaleConstant;
import com.gyf.immersionbar.ImmersionBar;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.hub.IntHub;
import com.pengxh.androidx.lite.utils.ImmerseStatusBarManager;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.EasyPermissions.PermissionCallbacks;

public class PermissionActivity extends AndroidxBaseActivity<ActivityPermssionBinding> implements PermissionCallbacks {

    @Override
    protected void setupTopBarLayout() {
        ImmerseStatusBarManager.setColor(this, IntHub.convertColor(this, R.color.white));
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initData() {
        //判断是否有权限，如果版本大于5.1才需要判断（即6.0以上），其他则不需要判断。
        if (EasyPermissions.hasPermissions(this, LocaleConstant.USER_PERMISSIONS)) {
            startSplashScreenActivity();
        } else {
            viewBinding.enterMainButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EasyPermissions.requestPermissions(PermissionActivity.this, "需要获取相关权限", LocaleConstant.PERMISSIONS_CODE, LocaleConstant.USER_PERMISSIONS);
                }
            });
        }
    }

    @Override
    protected void initEvent() {

    }

    private void startSplashScreenActivity() {
        startActivity(new Intent(this, SplashScreenActivity.class));
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        startSplashScreenActivity();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

    }
}