package com.example.apk_deteksi_kualitas_telur.Ui.Activity

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.text.style.UnderlineSpan
import androidx.core.content.ContextCompat
import com.example.apk_deteksi_kualitas_telur.R
import com.example.apk_deteksi_kualitas_telur.databinding.ActivityAyamRasBinding

class AyamRasActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAyamRasBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAyamRasBinding.inflate(layoutInflater)
        selectQualityAyamImg()
        expandedTextViewAyam()
        setContentView(binding.root)
    }

    //pilih gambar dan akan menampilkan pada CardView
    private fun selectQualityAyamImg() {
        binding.imgItem1.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.ayam_ras01))
        }
        binding.imgItem2.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.ayam_ras02))
        }
        binding.imgItem3.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.ayam_ras03))
        }
        binding.imgItem4.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.ayam_ras04))
        }
    }

    //tampilkan sedikit dan banyak pada text view ketika di klik
    private fun expandedTextViewAyam() {
        val penjelasanAyamRas = getString(R.string.penjelasan_ayam_ras)
        val tampilkanSemua = getString(R.string.tamplkan_semua)
        val fullText = textStyleOutputPenjelasan(penjelasanAyamRas, tampilkanSemua)
        binding.tvPenjelasanTelurBagus.text = fullText
        binding.tvPenjelasanTelurBagus.maxLines = 3

        var isExpanded = false
        binding.tvPenjelasanTelurBagus.setOnClickListener {
            isExpanded = !isExpanded
            if (isExpanded) {
                val penjelasanAyamRasFull = getString(R.string.penjelasan_ayam_ras_full)
                val tampilkanSedikit = getString(R.string.tampilkan_sedikit)
                val fullText = textStyleOutputPenjelasan(penjelasanAyamRasFull, tampilkanSedikit)

                binding.tvPenjelasanTelurBagus.text = fullText
                binding.tvPenjelasanTelurBagus.maxLines = 10000
            } else {
                val penjelasanAyamRas = getString(R.string.penjelasan_ayam_ras)
                val tampilkanSemua = getString(R.string.tamplkan_semua)
                val fullText = textStyleOutputPenjelasan(penjelasanAyamRas, tampilkanSemua)
                binding.tvPenjelasanTelurBagus.text = fullText
                binding.tvPenjelasanTelurBagus.maxLines = 3
            }
        }
    }

    private fun textStyleOutputPenjelasan(textAwal: String, textAkhir: String): SpannableString {
        val spannableString = SpannableString("$textAwal$textAkhir")
        //text underline
        val startIndex = spannableString.indexOf(textAkhir)
        val colorSpan = ContextCompat.getColor(this, R.color.coklatTelur)

        val boldSpan = StyleSpan(Typeface.BOLD)
        val underlineSpan = UnderlineSpan()

        if (startIndex != -1) {
            val endIndex = startIndex + textAkhir.length
            //memberi garis bawah
            spannableString.setSpan(
                underlineSpan, startIndex, endIndex, Spanned.SPAN_INCLUSIVE_INCLUSIVE
            )
            //memberi warna akhir color text
            spannableString.setSpan(
                ForegroundColorSpan(colorSpan),
                startIndex,
                endIndex,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE
            )

            //memberi warna akhir color text
            spannableString.setSpan(
                boldSpan, startIndex, endIndex, Spanned.SPAN_INCLUSIVE_INCLUSIVE
            )
        }
        return spannableString
    }

    private fun getSampleImage(drawable: Int): Bitmap {
        return BitmapFactory.decodeResource(resources, drawable, BitmapFactory.Options().apply {
            inMutable = true
        })
    }
}