package com.casic.dcms.view;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.amap.api.services.core.ServiceSettings;
import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityPermssionBinding;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.ViewGroupKit;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.ObjectKit;

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

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

public class PermissionActivity extends AndroidxBaseActivity<ActivityPermssionBinding> implements PermissionCallbacks {
    private static final String TAG = "PermissionActivity";
    public List<String> COMMON_PERMISSIONS = Arrays.asList(
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.CAMERA
    );

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        List<String> USER_PERMISSIONS = new ArrayList<>(COMMON_PERMISSIONS);
        //针对不同的系统版本进行授权
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            USER_PERMISSIONS.add(Manifest.permission.READ_MEDIA_IMAGES);
            USER_PERMISSIONS.add(Manifest.permission.READ_MEDIA_VIDEO);
        } else {
            USER_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            USER_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        String[] permissions = new String[USER_PERMISSIONS.size()];
        for (int i = 0; i < USER_PERMISSIONS.size(); i++) {
            permissions[i] = USER_PERMISSIONS.get(i);
        }

        if (EasyPermissions.hasPermissions(this, permissions)) {
            startSplashScreenActivity();
        } else {
            binding.enterMainButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EasyPermissions.requestPermissions(PermissionActivity.this, "需要获取相关权限", LocaleConstant.PERMISSIONS_CODE, permissions);
                }
            });
        }
    }

    @Override
    protected void initEvent() {

    }

    private void startSplashScreenActivity() {
        //先把导航隐私政策声明，后面导航会用到
        ServiceSettings.updatePrivacyShow(this, true, true);
        ServiceSettings.updatePrivacyAgree(this, true);
        ContextKit.navigatePageTo(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) {
        //授权后台定位
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            int checkSelfPermission = ContextCompat.checkSelfPermission(
                    this, Manifest.permission.ACCESS_BACKGROUND_LOCATION
            );
            if (checkSelfPermission == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
                        LocaleConstant.PERMISSIONS_CODE
                );
            }
        }

        startSplashScreenActivity();
        Log.d(TAG, "onPermissionsGranted: " + ObjectKit.getJson(perms));
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Log.d(TAG, "onPermissionsDenied: " + ObjectKit.getJson(perms));
    }
}