package com.example.apk_deteksi_kualitas_telur.Ui.Activity

import android.content.Intent
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.MainActivity
import com.example.apk_deteksi_kualitas_telur.R
import com.example.apk_deteksi_kualitas_telur.databinding.ActivityKualitasTelurBagusBinding

class KualitasTelurBagusActivity : AppCompatActivity() {
    private lateinit var binding: ActivityKualitasTelurBagusBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityKualitasTelurBagusBinding.inflate(layoutInflater)
        selectQualityTelurImg()
        expandedTextViewTelur()
        binding.imgBack.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        setContentView(binding.root)
    }

    //pilih gambar dan akan menampilkan pada CardView
    private fun selectQualityTelurImg() {
        binding.imgItem1.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.bagus01))
        }
        binding.imgItem2.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.bagus02))
        }
        binding.imgItem3.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.bagus03))
        }
        binding.imgItem4.setOnClickListener {
            binding.imgViewShow.setImageBitmap(getSampleImage(R.drawable.bagus04))
        }
    }

    //tampilkan sedikit dan banyak pada text view ketika di klik
    private fun expandedTextViewTelur() {
        var isExpanded = false
        val penjelasanTelurBagus = getString(R.string.penjelasan_telur_bagus)
        val tampilkanSemua = getString(R.string.tamplkan_semua)
        val textTampilanSedikit = textStyleOutputPenjelasan(penjelasanTelurBagus, tampilkanSemua)
        binding.tvPenjelasanTelurBagus.text = textTampilanSedikit

        binding.tvPenjelasanTelurBagus.setOnClickListener {
            isExpanded = !isExpanded
            if (isExpanded) {

                val penjelasanTelurBagusFull = getString(R.string.penjelasan_telur_bagus_full)
                val tampilkanSedikit = getString(R.string.tampilkan_sedikit)

                val fullText = textStyleOutputPenjelasan(penjelasanTelurBagusFull, tampilkanSedikit)
                binding.tvPenjelasanTelurBagus.text = fullText

                binding.tvPenjelasanTelurBagus.maxLines = 10000
            } else {
                val penjelasanTelurBagus = getString(R.string.penjelasan_telur_bagus)
                val tampilkanSemua = getString(R.string.tamplkan_semua)

                val fullText = textStyleOutputPenjelasan(penjelasanTelurBagus, 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
        })
    }
}