package com.fyp.evaluation.Student
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.*
import androidx.cardview.widget.CardView
import com.fyp.evaluation.R
import com.fyp.evaluation.SharedPreference
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.*
import com.makeramen.roundedimageview.RoundedImageView
import com.squareup.picasso.Picasso
class SupervisorPreview : AppCompatActivity() {
private var backBtn:RelativeLayout? = null
private var profileImage:RoundedImageView? = null
private var btnRequest:CardView? = null
private var nameTxtView:TextView? = null
private var requestTxtView:TextView? = null
private var statusActiveTxtView:TextView? = null
private var statusNotActiveTxtView:TextView? = null
private var spinner:Spinner? = null
private var arrayList = mutableListOf<String>()
private var firebaseUser:FirebaseUser? = null
private var uidSupervisor:String = ""
private lateinit var sharedPreference:SharedPreference
private var nameCurrentUser:String? = null
private var profileImageCurrentUser:String? = null
private var semesterCurrentUser:String? = null
private var pnameCurrentUser:String? = null
private var gnameCurrentUser:String = ""
private lateinit var refRequestCancelTemp:ValueEventListener
private lateinit var refRequestCancel:DatabaseReference
private lateinit var refIsAllowedTemp:ValueEventListener
private lateinit var refIsAllowed:DatabaseReference
private lateinit var refRetrieveTemp:ValueEventListener
private lateinit var refRetrieve:DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_supervisor_preview)
backBtn = findViewById(R.id.btnBack_supervisor_preview)
profileImage = findViewById(R.id.profileImage_supervisor_preview)
btnRequest = findViewById(R.id.Btnrequest_supervisor_preview)
nameTxtView = findViewById(R.id.txtViewname_supervisor_preview)
requestTxtView = findViewById(R.id.txtView_request)
statusActiveTxtView = findViewById(R.id.txtView_statusActive_supervisor_preview)
statusNotActiveTxtView = findViewById(R.id.txtView_statusNotActive_supervisor_preview)
spinner = findViewById(R.id.spinner_supervisor_preview)
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
if (supportActionBar!=null)
{
supportActionBar!!.hide()
}
spinner?.setPopupBackgroundResource(R.color.card_color1)
sharedPreference = SharedPreference(this@SupervisorPreview)
nameCurrentUser = sharedPreference.getName()
profileImageCurrentUser = sharedPreference.getProfileImage()
pnameCurrentUser = sharedPreference.getProjectName()
gnameCurrentUser = sharedPreference.getGroupName().toString()
semesterCurrentUser = sharedPreference.getSemester()
firebaseUser = FirebaseAuth.getInstance().currentUser
var intent = intent
uidSupervisor = intent.getStringExtra("uid").toString()
var profile_image = intent.getStringExtra("profileImage")
var name = intent.getStringExtra("name")
Picasso.get().load(profile_image).into(profileImage)
nameTxtView?.setText(name.toString())
retrieveSlots(uidSupervisor)
retrieveProjects(uidSupervisor)
isAlloted()
backBtn?.setOnClickListener(object: View.OnClickListener
{
override fun onClick(v: View?) {
finish()
}
})
btnRequest?.setOnClickListener(object: View.OnClickListener
{
override fun onClick(v: View?) {
var status = requestTxtView?.getText().toString()
if (status.equals("Cancel"))
{
cancelRequest()
}
else
{
sendRequest()
}
}
})
}
private fun sendRequest()
{
requestTxtView?.setText("Sending")
var ref = FirebaseDatabase.getInstance().reference.child("Requests")
var key = ref.push().key
var hashMap = HashMap<String,String>()
hashMap.put("id_supervisor",uidSupervisor)
hashMap.put("id",firebaseUser!!.uid)
hashMap.put("name",nameCurrentUser!!)
hashMap.put("profile_image",profileImageCurrentUser!!)
hashMap.put("project_name",pnameCurrentUser!!)
hashMap.put("group_name",gnameCurrentUser!!)
hashMap.put("semester",semesterCurrentUser!!)
hashMap.put("status","pending")
hashMap.put("key",key.toString())
ref.child(key!!).setValue(hashMap).addOnCompleteListener { task->
if(task.isSuccessful)
{
requestTxtView?.setText("Cancel")
}
}
}
private fun cancelRequest()
{
var ref = FirebaseDatabase.getInstance().reference.child("Requests")
refRequestCancelTemp = ref.addValueEventListener(object: ValueEventListener
{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists())
{
for (dataSnapShot in snapshot.children)
{
var id = dataSnapShot.child("id").getValue(String::class.java)
var id_supervisor = dataSnapShot.child("id_supervisor").getValue(String::class.java)
var key = dataSnapShot.child("key").getValue(String::class.java)
if (id.equals(firebaseUser!!.uid) && id_supervisor.equals(uidSupervisor))
{
FirebaseDatabase.getInstance().reference.child("Requests").child(key!!).removeValue()
ref.removeEventListener(refRequestCancelTemp)
}
}
requestTxtView?.setText("Request")
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
private fun retrieveRequestStatus()
{
var ref = FirebaseDatabase.getInstance().reference.child("Requests")
refRetrieveTemp = ref.addValueEventListener(object: ValueEventListener
{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists())
{
for (dataSnapShot in snapshot.children)
{
var id = dataSnapShot.child("id").getValue(String::class.java)
var id_supervisor = dataSnapShot.child("id_supervisor").getValue(String::class.java)
var status = dataSnapShot.child("status").getValue(String::class.java)
if (id.equals(firebaseUser!!.uid) && id_supervisor.equals(uidSupervisor))
{
if (status.equals("pending"))
{
requestTxtView?.setText("Cancel")
requestTxtView?.isEnabled = true
btnRequest?.isEnabled = true
}
if (status.equals("rejected"))
{
requestTxtView?.setText("Request")
requestTxtView?.isEnabled = true
btnRequest?.isEnabled = true
}
}
}
ref.removeEventListener(refRetrieveTemp)
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
private fun isAlloted()
{
var flag = false
var ref = FirebaseDatabase.getInstance().reference.child("Requests")
refIsAllowedTemp = ref.addValueEventListener(object: ValueEventListener
{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists())
{
for (dataSnapShot in snapshot.children)
{
var id = dataSnapShot.child("id").getValue(String::class.java)
var id_supervisor = dataSnapShot.child("id_supervisor").getValue(String::class.java)
var status = dataSnapShot.child("status").getValue(String::class.java)
if (id.equals(firebaseUser!!.uid))
{
if (status.equals("accepted"))
{
if (id_supervisor.equals(uidSupervisor))
{
flag = true
requestTxtView?.setText("Alloted")
requestTxtView?.isEnabled = false
btnRequest?.isEnabled = false
}
else
{
btnRequest?.visibility = View.INVISIBLE
}
}
}
}
ref.removeEventListener(refIsAllowedTemp)
if (!flag)
{
retrieveRequestStatus()
}
}
else
{
requestTxtView?.setText("Request")
requestTxtView?.isEnabled = true
btnRequest?.isEnabled = true
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
private fun retrieveSlots(uid:String?)
{
var slots = "20"
var flag = false
var ref = FirebaseDatabase.getInstance().reference.child("Slots").child(uid!!)
ref.addValueEventListener(object: ValueEventListener
{
override fun onDataChange(snapshot: DataSnapshot)
{
if (snapshot.exists())
{
slots = snapshot.child("id").getValue(String::class.java)!!
flag = true
}
if (flag)
{
var temp = slots.toInt()
temp -=20
if (temp>0)
{
statusActiveTxtView?.visibility = View.VISIBLE
statusNotActiveTxtView?.visibility = View.INVISIBLE
}
else
{
statusActiveTxtView?.visibility = View.INVISIBLE
statusNotActiveTxtView?.visibility = View.VISIBLE
}
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
private fun retrieveProjects(uid:String?)
{
var ref = FirebaseDatabase.getInstance().reference.child("Projects").child(uid!!)
ref.addValueEventListener(object: ValueEventListener
{
override fun onDataChange(snapshot: DataSnapshot)
{
if (snapshot.exists())
{
(arrayList as ArrayList<String>).clear()
for (dataSnapShot in snapshot.children)
{
var project = dataSnapShot.child("project").getValue(String::class.java)
arrayList.add(project.toString())
}
var adapterProjects = ArrayAdapter<String>(this@SupervisorPreview,R.layout.support_simple_spinner_dropdown_item,arrayList)
spinner?.adapter = adapterProjects
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
override fun onPause() {
// refRequestCancel.removeEventListener(refRequestCancelTemp)
// refIsAllowed.removeEventListener(refIsAllowedTemp)
// refRetrieve.removeEventListener(refRetrieveTemp)
super.onPause()
}
}