package com.base.scanqrclear.ui.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.view.Gravity
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.OpenFoodBean
import com.base.scanqrclear.databinding.DialogOpenFoodBinding

object OpenFoodDialog {

    @SuppressLint("SetTextI18n")
    fun Activity.showOpenFoodDialog(openFoodBean: OpenFoodBean) {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogOpenFoodBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val params = dialog.window?.attributes
        params?.width = ConstraintLayout.LayoutParams.MATCH_PARENT
//        params?.height = resources.getDimensionPixelOffset(R.dimen.dp_400)
        params?.gravity = Gravity.BOTTOM
//        params?.y = 50

        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        binding.ivClose.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvName.text = openFoodBean.product_name
        var grade = "NA"
        var gradeDesc = "NA"
        var gradeImage = R.mipmap.grade_a
        if (
            openFoodBean.nutriscore_grade?.contains("A") == true ||
            openFoodBean.nutriscore_grade?.contains("a") == true
        ) {
            grade = "A"
            gradeDesc = "Very good nutritional quality"
            gradeImage = R.mipmap.grade_a
        }
        if (
            openFoodBean.nutriscore_grade?.contains("B") == true ||
            openFoodBean.nutriscore_grade?.contains("b") == true
        ) {
            grade = "B"
            gradeDesc = "Good nutritional quality"
            gradeImage = R.mipmap.grade_b
        }
        if (
            openFoodBean.nutriscore_grade?.contains("C") == true ||
            openFoodBean.nutriscore_grade?.contains("c") == true
        ) {
            grade = "C"
            gradeDesc = "Average nutritional quality"
            gradeImage = R.mipmap.grade_c
        }
        if (
            openFoodBean.nutriscore_grade?.contains("D") == true ||
            openFoodBean.nutriscore_grade?.contains("d") == true
        ) {
            grade = "D"
            gradeDesc = "Poor nutritional quality"
            gradeImage = R.mipmap.grade_d
        }
        if (
            openFoodBean.nutriscore_grade?.contains("E") == true ||
            openFoodBean.nutriscore_grade?.contains("e") == true
        ) {
            grade = "E"
            gradeDesc = "Bad nutritional quality"
            gradeImage = R.mipmap.grade_e
        }


        binding.tvGrade.text = "Nutr.comp:$grade"
        binding.tvGradeDesc.text = gradeDesc
        binding.ivGrade.setImageResource(gradeImage)

        var novaImage = R.mipmap.nova_na
        if (openFoodBean.nova_groups == "1") {
            novaImage = R.drawable.nova_group_1
        }
        if (openFoodBean.nova_groups == "2") {
            novaImage = R.drawable.nova_group_2
        }
        if (openFoodBean.nova_groups == "3") {
            novaImage = R.drawable.nova_group_3
        }
        if (openFoodBean.nova_groups == "3") {
            novaImage = R.drawable.nova_group_4
        }
        binding.ivNova.setImageResource(novaImage)
        binding.tvNova.text = openFoodBean.nova_groups_tags?.first() ?: "NA"

        binding.tvIngredients.text = openFoodBean.ingredients_text ?: "NA"
        if (openFoodBean.product_quantity == null || openFoodBean.product_quantity_unit == null) {
            binding.tvContent.text = "NA"
        } else {
            binding.tvContent.text = "${openFoodBean.product_quantity}${openFoodBean.product_quantity_unit}"
        }
        binding.tvDataSource.text = openFoodBean.dataSource
    }
}