package com.example.criminalintent

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.IntentCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.criminalintent.CrimeActivity.Companion.EXTRA_CRIME_ID
import java.util.UUID

class CrimePagerActivity : AppCompatActivity() {

    private var mViewPager: ViewPager2? = null
    private var mCrimes:List<Crime>? = null

    companion object {
        fun newIntent (packageContext: Context, crimeId:UUID?): Intent {
            val intent = Intent (packageContext,CrimePagerActivity::class.java)
            intent.putExtra(EXTRA_CRIME_ID,crimeId)
            return intent

        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_crime_pager)
        val crimeId = IntentCompat.getSerializableExtra (intent,EXTRA_CRIME_ID,UUID::class.java)
           // intent.getSerializableExtra(EXTRA_CRIME_ID) as UUID
        mViewPager = findViewById (R.id.crime_view_pager)
        mCrimes = CrimeLab.crimes
        val fragmentManager = supportFragmentManager
        mViewPager!!.adapter = object : FragmentStateAdapter(fragmentManager,lifecycle) {

            override fun createFragment(position: Int): Fragment {
                val crime = mCrimes!![position]
                return CrimeFragment.newInstance(crime.mId)
            }

            override fun getItemCount(): Int {
                return mCrimes!!.size
            }
        }

        for (i in mCrimes!!.indices) {
            if (mCrimes!![i].mId==crimeId) {
                mViewPager!!.currentItem = i
                break
            }

        }
    }
}