package fr.plnech.dunbar

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import fr.plnech.dunbar.dummy.DummyContent
import fr.plnech.dunbar.model.Friend
import kotlinx.android.synthetic.main.activity_friend_detail.*
import kotlinx.android.synthetic.main.friend_detail.view.*

/**
 * A fragment representing a single Friend detail screen.
 * This fragment is either contained in a [FriendListActivity]
 * in two-pane mode (on tablets) or a [FriendDetailActivity]
 * on handsets.
 */
class FriendDetailFragment : Fragment() {

    /**
     * The dummy content this fragment is presenting.
     */
    private var item: Friend? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        arguments?.let {
            if (it.containsKey(ARG_ITEM_ID)) {
                // Load the dummy content specified by the fragment
                // arguments. In a real-world scenario, use a Loader
                // to load content from a content provider.
                item = DummyContent.ITEM_MAP[it.getInt(ARG_ITEM_ID)]
                activity?.toolbar_layout?.title = item?.name
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView = inflater.inflate(R.layout.friend_detail, container, false)

        // Show the dummy content as text in a TextView.
        item?.let {
            rootView.friend_detail.text = it.mapString()
            activity?.title = it.name
        }

        return rootView
    }

    companion object {
        /**
         * The fragment argument representing the item ID that this fragment
         * represents.
         */
        const val ARG_ITEM_ID = "item_id"
    }
}
