package com.example.criminalintent

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class CrimeListFragment : Fragment() { //A fragment is a piece of an activity.  It controls part of the screen
    private var mCrimeRecyclerView: RecyclerView? = null
    //RecyclerView:  What we're going to do here is build a scrollable list.  We may have many more
    //items in the list than can be viewed at once.  What a recycler view is deallocate items from
    //the list when they no longer visible (scrolled too high or low) and then remade when they
    //scroll to the view.  To the user, this happens invisibly.  But it save memory, since you're
    //not storing list items that no one can look at or select.

    private var mAdapter:CrimeAdapter? = null
    //Recycler Views require "adapters" as part of their object structure to work

//onCreateView is like onCreate, but it's called when a fragment is created.
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view =  inflater.inflate(R.layout.fragment_crime_list, container, false)
        mCrimeRecyclerView = view.findViewById (R.id.crime_recycler_view)
        mCrimeRecyclerView!!.layoutManager = LinearLayoutManager(activity)
        updateUI () //We'll write this method, this is going to update what's in the list
        return view
    }

    override fun onResume () {
        Log.d("CRIMELISTFRAGMENT", "onResume")
        super.onResume()
        updateUI()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("CRIMELISTFRAGMENT", "onCreate")
        super.onCreate(savedInstanceState)
    }

    override fun onStart() {
        Log.d("CRIMELISTFRAGMENT", "onStart")
        super.onStart()
    }

    override fun onStop() {
        Log.d("CRIMELISTFRAGMENT", "onStop")
        super.onStop()
    }

    override fun onDetach() {
        Log.d("CRIMELISTFRAGMENT", "onDetach")
        super.onDetach()
    }

    override fun onDestroyView() {
        Log.d("CRIMELISTFRAGMENT", "onDestroyView")
        super.onDestroyView()
    }

    override fun onDestroy() {
        Log.d("CRIMELISTFRAGMENT", "onDestroy")
        super.onDestroy()
    }

    override fun onPause() {
        Log.d("CRIMELISTFRAGMENT", "onPause")
        super.onPause()
    }

    private fun updateUI () {

        val crimeLab = CrimeLab
        val crimes = crimeLab.crimes
        if (mAdapter==null) {
            mAdapter = CrimeAdapter (crimes)
            mCrimeRecyclerView!!.adapter = mAdapter

        } else {
            mAdapter!!.notifyDataSetChanged()
        }

    }

    //An inner class can only be used by the class that containers
    //This saying that CrimeHolder inherits from RecyclerView.ViewHolder  AND from View.OnClickListenerClass
    private inner class CrimeHolder (itemView:View) : RecyclerView.ViewHolder (itemView),View.OnClickListener {

        private val mTitleTextView: TextView?
        private val mDateTextView: TextView?
        private val mSolvedCheckBox: CheckBox?
        private var mCrime:Crime? = null

//init in Kotlin is a form of initializer, this is the code that will run when the class is
        //instantiated
        init {
            itemView.setOnClickListener (this)
    mTitleTextView = itemView.findViewById (R.id.list_item_crime_title_text_view) as TextView
    mDateTextView = itemView.findViewById (R.id.list_item_crime_date_text_view) as TextView
    mSolvedCheckBox = itemView.findViewById (R.id.list_item_crime_solved_check_box) as CheckBox
    mSolvedCheckBox.isClickable = false
        }

        fun bindCrime (crime:Crime?) {
            mCrime = crime
            mTitleTextView!!.text = mCrime!!.mTitle
            mDateTextView!!.text = mCrime!!.mDate!!.toString()
            mSolvedCheckBox!!.isChecked = mCrime!!.mSolved

        }

        override fun onClick (v:View) {

            Toast.makeText (activity,mCrime!!.mTitle!!+" Clicked!",Toast.LENGTH_SHORT).show()
            val intent = CrimePagerActivity.newIntent (activity as Context,mCrime!!.mId)
            startActivity (intent)
        }
    }

    //These adapters are pretty much boilerplate.  When you have to write an adapter for PG4, you'll pretty much
    //just use this code

    private inner class CrimeAdapter (private val mCrimes:List<Crime>?):RecyclerView.Adapter<CrimeHolder>() {

        override fun onCreateViewHolder (parent:ViewGroup, viewType:Int): CrimeHolder {
            val layoutInflater = LayoutInflater.from(activity)
            val view = layoutInflater.inflate (R.layout.list_item_crime,parent,false)
            return CrimeHolder (view)
        }

        override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
            super.onDetachedFromRecyclerView(recyclerView)
        }

        override fun onBindViewHolder (holder:CrimeHolder,position:Int) {
            val crime = mCrimes!![position]
            holder.bindCrime (crime)
        }

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