package com.sjkj.appthreefloor_tsgz.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.login.LoginActivity;
import com.sjkj.base_lib.permission.PermissionInterceptor;
import com.sjkj.base_lib.permission.PermissionNameConvert;
import com.sjkj.base_lib.utils.ToastUtils;

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

public class SplashActivity extends AppCompatActivity {
    private List<String> permissions = new ArrayList<>();
    private Handler handler = new Handler();
    private boolean isFirst = true;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            XXPermissions.with(SplashActivity.this)
                    .permission(permissions)
                    .interceptor(new PermissionInterceptor())
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(List<String> permissions, boolean allGranted) {
                            if (allGranted) {
                                ToastUtils.show(String.format(getString(R.string.demo_obtain_permission_success_hint), PermissionNameConvert.getPermissionString(SplashActivity.this, permissions)));
                                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                jumpPermissionPage();
                            }


                        }

                        @Override
                        public void onDenied(List<String> permissions, boolean never) {
                            if (never) {
                                ToastUtils.show("被永久拒绝授权，请手动授予权限");
                                jumpPermissionPage();
                            } else {
                                ToastUtils.show("获取权限失败");
                                jumpPermissionPage();
                            }
                        }
                    });

        }
    };

    private void jumpPermissionPage() {
        if (isFirst) {
            Intent intent = new Intent(SplashActivity.this, PermissionListActivity.class);
            startActivity(intent);
            isFirst = false;
            finish();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        permissions.add(Permission.CAMERA);
        permissions.add(Permission.CALL_PHONE);
        permissions.add(Permission.ACCESS_COARSE_LOCATION);
        permissions.add(Permission.ACCESS_FINE_LOCATION);
        permissions.add(Permission.RECORD_AUDIO);
        permissions.add(Permission.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            permissions.add(Permission.MANAGE_EXTERNAL_STORAGE);
        } else {
            permissions.add(Permission.WRITE_EXTERNAL_STORAGE);
            permissions.add(Permission.READ_EXTERNAL_STORAGE);
        }
        handler.postDelayed(runnable, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
        handler = null;
    }

}
