package com.app.simplitend.appblocking;

import static com.app.simplitend.appblocking.TopAppDetectionService.BLOCKED_APP_NAME;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

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

import com.app.simplitend.R;
import com.app.simplitend.welcome.activities.WelcomeActivity;

import java.util.List;

public class BlockApp extends AppCompatActivity {

    private TextView textView;

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

        String appName = getIntent().getStringExtra(BLOCKED_APP_NAME);

        textView = findViewById(R.id.block_app_text);

        if (appName != null){
            textView.setText(appName + " is blocked by SimpliTend");
        }

        findViewById(R.id.open_simplitent).setOnClickListener(view -> {

            ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1);

            try {
                if (!runningTasks.isEmpty()) {
                    // There is at least one task on the activity stack
                    ActivityManager.RunningTaskInfo taskInfo = runningTasks.get(0);
                    ComponentName topActivity = taskInfo.topActivity;

                    // You can also check the number of activities in the task
                    int numActivities = taskInfo.numActivities;

                    if (numActivities > 1){
                        // There are activities below this screen to be shown
                        finish();
                    }else{
                        // no activities beneath this screen
                        throw new Exception();
                    }
                } else {
                    // There are no tasks on the activity stack, meaning no activities are in the foreground.
                    // Thus, opening the app from start
                    Intent intent = new Intent(this, WelcomeActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                }
            } catch (Exception e) {
                Intent intent = new Intent(this, WelcomeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }
        });

    }

    @Override
    protected void attachBaseContext(Context newBase) {
        final Configuration configuration = new Configuration(newBase.getResources().getConfiguration());
        configuration.fontScale = Math.min(configuration.fontScale, 1.1f);
        applyOverrideConfiguration(configuration);
        super.attachBaseContext(newBase);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String appName = intent.getStringExtra(BLOCKED_APP_NAME);

        textView = findViewById(R.id.block_app_text);

        if (textView != null && appName != null){
            textView.setText(appName + " is blocked by SimpliTend");
        }
    }

    @Override
    public void onBackPressed() {

    }
}
