package com.casic.br.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.casic.br.R
import com.casic.br.utils.LocaleConstant
import com.pengxh.kt.lite.extensions.navigatePageTo
import pub.devrel.easypermissions.EasyPermissions
import pub.devrel.easypermissions.EasyPermissions.PermissionCallbacks

class PermissionActivity : AppCompatActivity(), PermissionCallbacks {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //判断是否有权限，如果版本大于5.1才需要判断（即6.0以上），其他则不需要判断。
        if (EasyPermissions.hasPermissions(this, *LocaleConstant.USER_PERMISSIONS)) {
            startSplashScreenActivity()
        } else {
            EasyPermissions.requestPermissions(
                this@PermissionActivity,
                resources.getString(R.string.app_name) + "需要获取存储相关权限",
                LocaleConstant.PERMISSIONS_CODE,
                *LocaleConstant.USER_PERMISSIONS
            )
        }
    }

    private fun startSplashScreenActivity() {
        //TODO 不要开屏页
//        navigatePageTo<SplashScreenActivity>()
        navigatePageTo<MainActivity>()
        finish()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<String>, grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
        startSplashScreenActivity()
    }

    override fun onPermissionsDenied(requestCode: Int, perms: List<String>) {

    }
}