package com.khmer9.sbcmain.screen.main

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.google.android.material.card.MaterialCardView
import com.khmer9.sbcmain.R
import com.khmer9.sbcmain.extension.btnClick
import com.khmer9.sbcmain.extension.setSafeOnClickListener
import com.khmer9.sbcmain.remote.ModuleData
import com.khmer9.sbcmain.remote.SubModuleData
import com.squareup.picasso.Picasso

class MainModuleViewHolder(itemView: View, private val detailButtonListener: ((String, String, Int) -> Unit)? = null) : BaseListViewHolder(itemView) {
    private val picasso = Picasso.get()
    private val LOTTO_APP_ID = "630780f0f635dd035329f5b7"
    private val LOTTO_PLUS_APP_ID = "62e397edae71cfa8782aa3a0"
    private val COCK_ID = "631c17831da02749e7e23f35"
    private val YUKI_ID = "630478c22983a591133f0492"
    private val APONG_ID = "631c1d1a0d213229fc5fd11a"

    override fun bindView(item: BaseListItem) {
        val imgIcon = itemView.findViewById<ImageView>(R.id.img_icon)
        val tvName = itemView.findViewById<TextView>(R.id.tv_name)
        val itemModule = itemView.findViewById<MaterialCardView>(R.id.item_module)

        if (item is ModuleData) {
            tvName.text = item.name

            try {
                val displayMetrics = itemView.context.resources.displayMetrics
                val pxWidth = displayMetrics.widthPixels
                val pxHeight = displayMetrics.heightPixels
                val dpWidth = pxWidth / displayMetrics.density
                val dpHeight = pxHeight / displayMetrics.density

                // high resolution pixel 14400
                var height = dpHeight * 0.5804f

                //low resolution
                if (pxWidth < 1000) {
                    height = dpHeight * 0.2804f
                }

                imgIcon.layoutParams.height = height.toInt()
                imgIcon.layoutParams.width = height.toInt()

                when (item.appId) {
                    LOTTO_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_80)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    LOTTO_PLUS_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_plus)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    COCK_ID -> {
                        picasso
                            .load(R.drawable.ic_logo_cock)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    YUKI_ID -> {
                        picasso
                            .load(R.drawable.ic_yuki)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    APONG_ID -> {
                        picasso
                            .load(R.drawable.ic_apong)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    else -> {
                        picasso
                            .load(R.drawable.ic_lotto_main)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                }
            } catch (e: Exception) {
            }

            itemModule.setSafeOnClickListener {
                it.btnClick().subscribe {
                    detailButtonListener?.invoke(item.id, item.appId, item.numberSub)
                }
            }
        } else if (item is SubModuleData) {
            tvName.text = item.name

            try {
                val displayMetrics = itemView.context.resources.displayMetrics
                val pxWidth = displayMetrics.widthPixels
                val pxHeight = displayMetrics.heightPixels
                val dpWidth = pxWidth / displayMetrics.density
                val dpHeight = pxHeight / displayMetrics.density

                // high resolution pixel 14400
                var height = dpHeight * 0.5804f

                //low resolution
                if (pxWidth < 1000) {
                    height = dpHeight * 0.2804f
                }

                imgIcon.layoutParams.height = height.toInt()
                imgIcon.layoutParams.width = height.toInt()

                when (item.appId) {
                    LOTTO_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_80)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    LOTTO_PLUS_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_plus)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    else -> {
                        picasso
                            .load(item.smallIcon)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                }
            } catch (e: Exception) {
            }

            itemModule.setSafeOnClickListener {
                it.btnClick().subscribe {
                    detailButtonListener?.invoke(item.id, item.appId, 0)
                }
            }
        }
    }
}