package fr.plnech.dunbar.ui

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import fr.plnech.dunbar.R
import kotlinx.android.synthetic.main.activity_friend_detail.*

/**
 * An activity representing a single Friend detail screen. This
 * activity is only used on narrow width devices. On tablet-size devices,
 * item details are presented side-by-side with a list of items
 * in a [FriendListActivity].
 */
class FriendDetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_friend_detail)
        setSupportActionBar(detail_toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.friend_detail_container, FriendDetailFragment().apply {
                    arguments = Bundle().apply {
                        putInt(
                            FriendDetailFragment.ARG_FRIEND_ID,
                            intent.getIntExtra(FriendDetailFragment.ARG_FRIEND_ID, -1)
                        )
                    }
                })
                .commit()
        }
    }

    override fun onOptionsItemSelected(item: MenuItem) =
        when (item.itemId) {
            android.R.id.home -> {
                navigateUpTo(Intent(this, FriendListActivity::class.java))
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
}
