package com.gamedog.vididin.features.zero


import android.text.SpannableString
import android.text.Spanned
import android.text.style.UnderlineSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.ama.core.architecture.util.ResUtil
import com.gamedog.vididin.beans.ZeroBuyItem
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_FAIL
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_ONGOING
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_SUCCESS
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_UNSTART
import com.gamedog.vididin.manager.ZeroManager
import com.viddin.videos.free.R
import java.text.SimpleDateFormat
import com.viddin.videos.free.databinding.ZeroRecordWinBinding as ViewBinding

class ZeroRecordAdapter(private val onWithdrawCallback: (ZeroBuyItem)-> Unit) : ListAdapter<ZeroBuyItem, ZeroRecordAdapter.ViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ViewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    override fun submitList(list: List<ZeroBuyItem?>?) {
        val sortedList = list?.sortedByDescending { it?.start_time }
        super.submitList(sortedList)
    }

    private fun isWinItem(item: ZeroBuyItem): Boolean {
        val winerIds = item.winners
        return winerIds?.contains(AccountManager.getAccount().userId) ?: false
    }

    inner class ViewHolder(private val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: ZeroBuyItem) {
            val isWinItem = isWinItem(item)

            with(binding) {
                tvDateWin.text = SimpleDateFormat("yyyy/MM/dd").format(item.start_time * 1000)
                rlBg.setBackgroundResource(if (isWinItem) R.drawable.bg_records_win else R.drawable.bg_records_lost)
                ivLeftWin.setImageResource(if (isWinItem) R.mipmap.record_win else R.mipmap.record_lost)
                tvTitleWin.text = ResUtil.getString(if (isWinItem) R.string.record_win_item_title else R.string.record_lost_item_title)
                tvDescWin.text = item.title
                llRightWin.isVisible = isWinItem
                llRightLost.isVisible = !isWinItem

                if (isWinItem) {
                    val withdrawBean = ZeroManager.instance().getZeroWithdrawItem(item)
                    when (withdrawBean.withdrawState) {
                        TRANSACTION_STATE_UNSTART -> {
                            llRightWin.isClickable = true
                            tvWinRightBottom.text = SpannableString(ResUtil.getString(R.string.zero_win_state_withdrarw_unstart)).apply {
                                setSpan(
                                    UnderlineSpan(),
                                    0,
                                    this.length,
                                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                                )
                            }
                        }

                        TRANSACTION_STATE_FAIL -> {
                            llRightWin.isClickable = true
                            tvWinRightBottom.text = SpannableString(ResUtil.getString(R.string.zero_win_state_failed)).apply {
                                setSpan(
                                    UnderlineSpan(),
                                    0,
                                    this.length,
                                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                                )
                            }
                        }

                        TRANSACTION_STATE_ONGOING -> {
                            llRightWin.isClickable = false
                            tvWinRightBottom.text = ResUtil.getString(R.string.zero_win_state_withdrarw_ongoing)
                        }

                        TRANSACTION_STATE_SUCCESS -> {
                            llRightWin.isClickable = false
                            tvWinRightBottom.text = ResUtil.getString(R.string.zero_win_state_withdrarw_success)
                        }
                    }

                    llRightWin.setOnClickListener {
                        onWithdrawCallback(item)
                    }
                }
            }
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<ZeroBuyItem>() {
        override fun areItemsTheSame(oldItem: ZeroBuyItem, newItem: ZeroBuyItem): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: ZeroBuyItem, newItem: ZeroBuyItem): Boolean {
            return false
        }
    }
}