package fr.plnech.dunbar.ui

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import androidx.recyclerview.widget.LinearLayoutManager
import fr.plnech.dunbar.FriendListActivity
import fr.plnech.dunbar.R
import fr.plnech.dunbar.data.Messages
import fr.plnech.dunbar.fetchFriends
import fr.plnech.dunbar.model.Friend
import fr.plnech.dunbar.notif.FriendReminder
import fr.plnech.dunbar.plural
import kotlinx.android.synthetic.main.activity_contacts.*
import kotlinx.android.synthetic.main.content_friends.*
import java.util.*


class FriendsActivity : AppCompatActivity() {

    private lateinit var messages: Messages
    private var friends = listOf<Friend>()

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

//        messages = Messages(applicationContext)

        fab.setOnClickListener { view ->
            reloadFriends()
            notifyFriend()
        }

        reloadFriends()
        notifyFriend()
    }

    private fun notifyFriend() {
        val idNotification = 0
        val notRecentlyTalked = friends.filter {
            it.lastDate != null && (Date().time - it.lastDate!!.time) > 1 * 60 * 60 * 1000
        }
        println("${notRecentlyTalked.size} good old friends I could talk to.")
        val friend = notRecentlyTalked.minBy { it.timesContacted }


        friend?.let {
            with(NotificationManagerCompat.from(this)) {
                val notification =
                    FriendReminder(applicationContext).createNotification(
                        friend,
                        this@FriendsActivity
                    )
                notification?.let {
                    notify(idNotification, notification)
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            R.id.action_friends -> {
                startActivity(Intent(this, FriendListActivity::class.java))
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    private fun reloadFriends() {
        friends = fetchFriends()
        displayFriends()
//        fetchMessages()
    }

    private fun fetchMessages() {
        messages.fetchAll()
        Toast.makeText(
            this, "${messages.sent.size} messages sent, " +
                    "${messages.inbox.size} messages received.", Toast.LENGTH_SHORT
        ).show()
    }


    private fun displayFriends() {
        val adapter = FriendsAdapter(friends)
        val nbFriends = adapter.itemCount

        friendsList.layoutManager = LinearLayoutManager(this)
        friendsList.setHasFixedSize(true)
        friendsList.adapter = adapter
        welcomeTitle.text = getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends))
    }

}
