package com.example.criminalintent

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import androidx.core.os.BundleCompat
import java.util.UUID


class CrimeFragment : Fragment() {

    var mCrime:Crime? = null
    var mTitleField: EditText? = null
    var mSolvedCheckBox: CheckBox? = null
    var mDateButton: Button? = null

    companion object {
        val ARG_CRIME_ID = "crime_id"
//We would call newInstance OUTSIDE of this class.
        fun newInstance (crimeID:UUID?):CrimeFragment {
            val args = Bundle ()  //we pass a bundle to a fragment...much like we pass an intent to an activity
            args.putSerializable(ARG_CRIME_ID,crimeID) //put a crimeID (UUID) into the bundle
            val fragment = CrimeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("CRIMEFRAGMENT", "onCreate")
        super.onCreate(savedInstanceState)
        val crimeID: UUID? = BundleCompat.getSerializable (requireArguments(),ARG_CRIME_ID,UUID::class.java)
            //requireArguments().getSerializable(ARG_CRIME_ID) as UUID?
        //get the crimeID out of the bundle and get the crime from the crimelab.
        mCrime = CrimeLab.getCrime(crimeID)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("CRIMEFRAGMENT", "onCreateView")
        // Inflate the layout for this fragment
        val v:View? =  inflater.inflate(R.layout.fragment_crime, container, false)
        mTitleField = v!!.findViewById(R.id.crime_title)
        mTitleField!!.setText (mCrime!!.mTitle)
        //I don't have a button this time to register that there's new text
        //Every time text is changed, it calls a textchangedlistener
        //There are three methods that must be written in this listener, whether
        //you use them or not
        mTitleField!!.addTextChangedListener (object: TextWatcher {
            override fun beforeTextChanged(p0:CharSequence?,p1:Int, p2:Int, p3:Int) {}
            override fun onTextChanged (s:CharSequence?,start:Int,before:Int,count:Int) {
                mCrime!!.mTitle = s!!.toString()
            }
            override fun afterTextChanged (s: Editable) {}
        })
        mSolvedCheckBox = v.findViewById(R.id.crime_solved)
        mSolvedCheckBox!!.isChecked = mCrime!!.mSolved
        mSolvedCheckBox!!.setOnCheckedChangeListener { _, isChecked ->
            mCrime!!.mSolved = isChecked
        }
        mDateButton = v.findViewById (R.id.crime_date)
        mDateButton!!.text = mCrime!!.mDate!!.toString()
        mDateButton!!.isEnabled = false
        return v
    }

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


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

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

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

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

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

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

}