package com.example.imageactivity

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.imageactivity.ui.theme.ImageActivityTheme
import java.util.concurrent.Executors

class MainActivity : ComponentActivity() {

    var webthread = false
    var mIcon11: Bitmap? = null  //Bitmap stores an image in memory

    private var mImageView: ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mImageView = findViewById (R.id.picture)  //ImageView is a widget that stores an image
        //You can preload an image in the layout if you want to, but this code is designed
        //to find an image on the web and load it in

        val executor = Executors.newSingleThreadExecutor()//create a new thread

        executor.execute {  //This starts the thread.  It starts a thread running the code in the braces
            doInBackground("http://philos.nmu.edu/weirdal/12 Jackson Park Express.jpg") // start it running
        }
        Log.e ("IMAGE","Started")
        while (!webthread);
        Log.e ("IMAGE","Ended")

        onPostExecute (mIcon11!!,mImageView!!)
    }

    //The main thread can't access the Internet.  Other threads can.  So this doInBackground method
    //will run in another thread.
    fun doInBackground(url: String)  {

        try {
            val infile = java.net.URL(url).openStream()  //open up the web address
            mIcon11 = BitmapFactory.decodeStream(infile)  //Convert this to a Bitmap
        } catch (e:Exception) {
            Log.e("Error",e.message!!)
            e.printStackTrace()
        }
        webthread = true
    }

    fun onPostExecute (result:Bitmap,bmImage:ImageView)
    {
        /*
        //Android wants to fill the entire ImageView with the image
                //Stretching it out of proportion if it has to
                //This math stuff will enlarge or shrink the bitmap
                //so that it fits in the image view, but it won't
                //stretch it in any way.

                var width: Int = bmImage.context.resources.displayMetrics.widthPixels
                //   val height: Int = bmImage.context.resources.displayMetrics.heightPixels
                var height = width * result.height / result.width
                if (height > bmImage.context.resources.displayMetrics.heightPixels) {
                    height = bmImage.context.resources.displayMetrics.heightPixels
                    width = height*result.width / result.height
                }
                val bitmap = Bitmap.createScaledBitmap(result, width, height, true)
                bmImage.setImageBitmap(bitmap)
        */
        bmImage.setImageBitmap(result)
        Log.e ("IMAGE","Made It To The End")

    }
}

/*

A JPG uses lossy compression.
A GIF uses lossless compression.
A BMP is uncompressed.

 */