package com.b2lmobitech.fieldcloudhrms

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.b2lmobitech.fieldcloudhrms.databinding.ActivityViewProfileBinding
import com.bumptech.glide.Glide
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.Locale

class ViewProfileActivity : AppCompatActivity() {

    private lateinit var binding: ActivityViewProfileBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityViewProfileBinding.inflate(layoutInflater)
        setContentView(binding.root)

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

        fetchProfile(peopleId, orgId)

        binding.btnBack.setOnClickListener { finish() }
    }

    private fun fetchProfile(peopleId: String, orgId: String) {

        RetrofitClient.instance.getProfile(peopleId, orgId)
            .enqueue(object : Callback<ProfileResponse> {
                override fun onResponse(
                    call: Call<ProfileResponse>,
                    response: Response<ProfileResponse>
                ) {
                    if (response.isSuccessful && response.body()?.success == true) {
                        bindData(response.body()!!.data)
                    }
                }

                override fun onFailure(call: Call<ProfileResponse>, t: Throwable) {
                    Toast.makeText(this@ViewProfileActivity, t.message, Toast.LENGTH_SHORT).show()
                }
            })
    }

    private fun bindData(d: ProfileData) {

        binding.tvName.text = d.name.orDash()
        binding.tvEmail.text = d.email_id.orDash()
        binding.tvEmail1.text = d.email_id.orDash()
        binding.tvEmpId.text = d.people_id.orDash()

        binding.tvDepartment.text = d.department_name.orDash()
        binding.tvDesignation.text = d.designation_name.orDash()
        binding.tvRole.text = d.role_name.orDash()
        binding.tvMobileNumber.text = d.mobile_number.orDash()

        binding.tvStatus.text = d.emp_status.orDash()
        binding.tvJoinDate.text = formatDate(d.join_date)

        binding.tvDob.text = d.dob.orDash()
        binding.tvMarital.text = d.marital_status.orDash()
        binding.tvBlood.text = d.blood_group.orDash()

        binding.tvBasicSalary.text = d.basic_salary.orDash()
        binding.tvpf.text = d.pf.orDash()
        binding.tvctc.text = d.ctc.orDash()
        binding.tvAadhar.text = d.aadhar_number.orDash()
        binding.tvPan.text = d.pan_number.orDash()

        binding.tvLocation.text = listOf(
            d.address,
            d.city_name,
            d.state_name,
            d.country_name
        ).filterNotNull().joinToString(", ").ifEmpty { "-" }

        Log.d("photp","${d.photo_image_path}")

        Glide.with(this)
            .load("https://processflow.in/HRMS/"+d.photo_image_path)
            .placeholder(R.drawable.placeholder)
            .into(binding.ivProfile)

        Glide.with(this)
            .load("https://processflow.in/HRMS/"+d.aadhar_photo_path)
            .placeholder(R.drawable.placeholder)
            .into(binding.etAadharImage)

        binding.ivEdit.setOnClickListener {
            startActivity(Intent(this, EditProfileActivity::class.java))
        }
    }

    private fun String?.orDash(): String = if (this.isNullOrBlank()) "-" else this

    private fun formatDate(date: String?): String {
        if (date.isNullOrBlank()) return "-"
        return try {
            val input = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
            val output = SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault())
            output.format(input.parse(date)!!)
        } catch (e: Exception) {
            "-"
        }
    }
}
