package com.b2lmobitech.fieldcloudhrms

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.b2lmobitech.fieldcloudhrms.databinding.ActivityAttendanceDetailBinding
import com.bumptech.glide.Glide

class AttendanceDetailActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAttendanceDetailBinding

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

        val date = intent.getStringExtra("date") ?: ""
        val day = intent.getStringExtra("day") ?: ""
        val inTime = intent.getStringExtra("inTime") ?: "00:00"
        val outTime = intent.getStringExtra("outTime") ?: "00:00"
        val status = intent.getStringExtra("status") ?: ""
        val totalHours = if (status == "IN") {
            "00:00"
        } else {
            intent.getStringExtra("totalHours") ?: "00:00"
        }
        val checkInImage = intent.getStringExtra("checkInImage") ?: ""
        val checkInSignature = intent.getStringExtra("checkInSignature") ?: ""
        val checkInLocation = intent.getStringExtra("checkInLocation") ?: ""
        val checkOutLocation = intent.getStringExtra("checkOutLocation") ?: ""
        val checkimg = "https://processflow.in/HRMS/" + checkInImage
        val checksign = "https://processflow.in/HRMS/" + checkInSignature
        Log.d("Checkin","$checkInImage,$checkInSignature")

        binding.txtDate.text = date
        binding.txtDay.text = day

        if (status.equals("ABSENT", true) || status.equals("UPCOMING", true)) {
            binding.layoutDetails.visibility = View.GONE
            binding.txtNoDetails.visibility = View.VISIBLE
            binding.imgCheckIn.visibility = View.GONE
            binding.signCheckIn.visibility = View.GONE
            binding.txtStatus.text = status
            return
        }

        // If PRESENT or IN_PROGRESS → show details
        binding.txtStatus.text = status
        binding.txtInTime.text = inTime
        binding.txtOutTime.text = outTime
        binding.txtTotalHours.text = totalHours

        // Load Check-In Image
        if (checkInImage.isNotEmpty() && checkInImage != "no data available") {
            binding.imgCheckIn.visibility = View.VISIBLE
            Glide.with(this)
                .load(checkimg)
                .placeholder(R.drawable.placeholder) // optional
                .error(R.drawable.no_image) // optional
                .into(binding.imgCheckIn)
        } else {
            binding.imgCheckIn.visibility = View.GONE
        }
        // Load Check-In Signature
        if (checkInSignature.isNotEmpty() && checkInSignature != "no data available") {
            binding.signCheckIn.visibility = View.VISIBLE
            Glide.with(this)
                .load(checksign)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.no_image)
                .into(binding.signCheckIn)
        } else {
            binding.signCheckIn.visibility = View.GONE
        }

        binding.btnBack.setOnClickListener { finish() }


    }
}
