package com.khmer9.kdmain.screen.main

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.google.android.material.card.MaterialCardView
import com.khmer9.kdmain.R
import com.khmer9.kdmain.config.Config
import com.khmer9.kdmain.extension.btnClick
import com.khmer9.kdmain.extension.setSafeOnClickListener
import com.khmer9.kdmain.remote.ModuleData
import com.khmer9.kdmain.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()

    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)
        val backgroundModule = itemView.findViewById<LinearLayoutCompat>(R.id.background_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) {
                    Config.LOTTO_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_80)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    Config.LOTTO_PLUS_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_plus)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)
                    }
                    Config.COCK_FIGHT_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_logo_cock)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)

                        backgroundModule.setBackgroundResource(R.drawable.chickenbg)
                    }
                    Config.YUKI_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_yuki)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)

                        backgroundModule.setBackgroundResource(R.drawable.yukibg)
                    }
                    Config.APONG_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_apong)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)

                        backgroundModule.setBackgroundResource(R.drawable.apongbg)
                    }
                    else -> {
                        picasso
                            .load(R.drawable.ic_lotto_main)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)

                        backgroundModule.setBackgroundResource(R.drawable.lottobg)
                    }
                }
            } 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) {
                    Config.LOTTO_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_80)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)

                        backgroundModule.setBackgroundResource(R.drawable.lottobg)
                    }
                    Config.LOTTO_PLUS_APP_ID -> {
                        picasso
                            .load(R.drawable.ic_lotto_plus)
                            .placeholder(R.drawable.animal_seven)
                            .into(imgIcon)

                        backgroundModule.setBackgroundResource(R.drawable.lottobg)
                    }
                    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)
                }
            }
        }
    }
}