package com.b2lmobitech.fieldcloudhrms

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.google.android.material.bottomnavigation.BottomNavigationView

class HomeActivity : AppCompatActivity() {

    private lateinit var imgProfile: ImageView
//    private lateinit var imgSearch: ImageView
    private lateinit var imgNotification: ImageView
    private lateinit var txtTitle: TextView
    private lateinit var bottomNav: BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        imgProfile = findViewById(R.id.imgProfile)
//        imgSearch = findViewById(R.id.imgSearch)
        imgNotification = findViewById(R.id.imgNotification)
        txtTitle = findViewById(R.id.txtTitle)
        bottomNav = findViewById(R.id.bottomNav)

        val sharedPref = getSharedPreferences("login_pref", Context.MODE_PRIVATE)
        val userId = sharedPref.getString("user_id", "")!!
        val orgId = sharedPref.getString("org_id", "")!!

        loadProfileImage(userId, orgId)


        // Default tab
        loadFragment(HomeFragment())

        bottomNav.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.menu_home -> {
                    txtTitle.text = "Home"
                    loadFragment(HomeFragment())
                    true
                }
                R.id.menu_onboard -> {
                    txtTitle.text = "Attendance"
                    loadFragment(AttendanceFragment())
                    true
                }
//                R.id.menu_add -> {
//                    txtTitle.text = "Add"
//                    loadFragment(AddFragment())
//                    true
//                }
                R.id.menu_leave -> {
                    txtTitle.text = "Leave"
                    loadFragment(LeaveTrackerFragment())
                    true
                }
                R.id.menu_more -> {
                    txtTitle.text = "More"
                    loadFragment(MoreFragment())
                    true
                }
                else -> false
            }
        }

//        imgSearch.setOnClickListener {
//            // TODO: Open search screen
//        }

        imgNotification.setOnClickListener {
            // TODO: Open notifications screen
        }

        imgProfile.setOnClickListener {
            startActivity(Intent(this, SettingsActivity::class.java))
        }
    }

    private fun loadFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragmentContainer, fragment)
            .commit()
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

    fun setTitleText(title: String) {
        txtTitle.text = title
    }

    private fun loadProfileImage(userId: String, orgId: String) {

        RetrofitClient.instance.getProfilePhoto(userId, orgId)
            .enqueue(object : retrofit2.Callback<ProfilePhotoResponse> {

                override fun onResponse(
                    call: retrofit2.Call<ProfilePhotoResponse>,
                    response: retrofit2.Response<ProfilePhotoResponse>
                ) {
                    val res = response.body()

                    if (res != null && res.success && !res.photo.isNullOrEmpty()) {

                        val imageUrl =
                            if (res.photo.startsWith("http"))
                                res.photo
                            else
                                "https://processflow.in/HRMS/" + res.photo

                        Glide.with(this@HomeActivity)
                            .load(imageUrl)
                            .placeholder(R.drawable.outline_person_2_24)
                            .error(R.drawable.outline_person_2_24)

                            .into(imgProfile)
                    }
                }

                override fun onFailure(call: retrofit2.Call<ProfilePhotoResponse>, t: Throwable) {
                    // silent fail
                }
            })
    }

}
