package edu.byu.suite.features.classRolls.controller

import android.os.Bundle
import android.text.util.Linkify
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import edu.byu.suite.R
import edu.byu.suite.features.classRolls.model.Student
import edu.byu.support.ByuClickListener
import edu.byu.support.ByuViewHolder
import edu.byu.support.activity.ByuRecyclerViewActivity
import edu.byu.support.adapter.ByuRecyclerAdapter
import edu.byu.support.defaults.DefaultRow
import edu.byu.support.utils.inflate
import java.util.*

class StudentActivity: ByuRecyclerViewActivity() {

	companion object {
		const val STUDENT = "student"
	}

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

		val student = intent.extras?.getParcelable<Student>(STUDENT)
		supportActionBar.title = student?.sortName

		val rows = ArrayList<DefaultRow>()
		rows.add(DefaultRow("Name", student?.sortName))
		student?.classStanding?.let { rows.add(DefaultRow("Standing:", it)) }
		student?.emphasis?.let { rows.add(DefaultRow("Emphasis:", it)) }
		student?.email?.let { rows.add(DefaultRow("Email", it)) }
		student?.phone?.let { rows.add(DefaultRow("Phone", it)) }
		if (student?.addressLine1 != null && student.addressLine2 != null) {
			rows.add(DefaultRow("Address:", student.addressLine1 + " " + student.addressLine2))
		}
		setAdapter(StudentInfoLinkifyAdapter(rows))
	}

	private inner class StudentInfoLinkifyAdapter(rows: List<DefaultRow>): ByuRecyclerAdapter<DefaultRow>(rows) {

		override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ByuViewHolder<DefaultRow> {
			return StudentInfoLinkifyViewHolder(inflate(parent, R.layout.default_list_item_detail))
		}

		private inner class StudentInfoLinkifyViewHolder(itemView: View): ByuViewHolder<DefaultRow>(itemView) {

			private val textView: TextView = itemView.findViewById(R.id.textView)
			private val detailView: TextView = itemView.findViewById(R.id.detailText)

			override fun bind(data: DefaultRow, listener: ByuClickListener<DefaultRow>?) {
				super.bind(data, listener)
				textView.text = data.text
				detailView.text = data.detailText
				detailView.setTextIsSelectable(true)
				Linkify.addLinks(detailView, Linkify.ALL)
			}
		}
	}
}
