package dev.kaderli.cpu_stress

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.regex.Pattern

class MainActivity : AppCompatActivity() {
    @Volatile
    var runningThreads = true
    inner class RegexThread : Thread("Regex Thread") {
        init {
            // Create a new, second thread
            start() // Start the thread
        }

        // This is the entry point for the second thread.
        override fun run() {
            while (runningThreads) {
                val p = Pattern.compile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val stressButton = findViewById<Button>(R.id.stressButton)
        stressButton.setOnClickListener {
            stressCpu()
        }
    }

    private fun stressCpu() {
        var i = 0;
        var running: Boolean = true;
        while (running) {
            Log.i("CPU-Stress", "Run #$i")

            runningThreads = true
            for (i in 1..8 ) {
                RegexThread()
            }
            val start = System.currentTimeMillis()
            Thread.sleep(2000);
            runningThreads = false
            Thread.sleep(2000);

            i++
            if (i == 100) {
                running = false
            }
        }
    }
}