package com.gamedog.vididin.features.zero


import android.os.CountDownTimer
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.SpUtil
import com.ama.core.architecture.util.setOnClickBatch
import com.viddin.videos.free.R
import com.gamedog.vididin.beans.ZeroBuyItem
import com.viddin.videos.free.databinding.LayoutItemZerobuyBinding as ViewBinding

class ZeroItemAdapter(private val joinCallback: (itemId: Int, diamondCost: Int)->Unit) : ListAdapter<ZeroBuyItem, ZeroItemAdapter.ViewHolder>(DiffCallback()) {

    private val mBgResList = listOf<Int>(R.mipmap.zero_bg_item_sub1, R.mipmap.zero_bg_item_sub2, R.mipmap.zero_bg_item_sub3, R.mipmap.zero_bg_item_sub4)

    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))
    }

    private fun getJoinedIdList(): List<Int> {
        return SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
    }

    private fun startCountDownTimer(targetTimeMs: Long) {
        val countDownDuration = targetTimeMs - System.currentTimeMillis()

        val mCountDownTimer = object : CountDownTimer(countDownDuration, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val joinedIds = getJoinedIdList()

                currentList.forEach { item->
                    if (joinedIds.contains(item.id) && !item.completed) {
                        var remainSec = (item.end_time - System.currentTimeMillis()) / 1000
                        //item.mCountDownTimeStr =
                    }
                }

                /*long totalSeconds = millisUntilFinished / 1000;
                long minutes = totalSeconds / 60;
                long seconds = totalSeconds % 60;
                String timeText = String . format ("%02d:%02d", minutes, seconds);
                */
            }

            override fun onFinish() {
            }
        }
        mCountDownTimer.start()
    }

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

    inner class ViewHolder(private val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: ZeroBuyItem) {
            binding.tvTitle.text = item.title
            binding.tvJoinedNum.text = item.current_users?.size.toString()
            binding.tvPeopleTotal.text = "/${item.target_num}"
            binding.tvJoinGoldNum.text = item.cost.toString()
            binding.ivBgType.setImageResource(mBgResList[item.image])
            binding.tvRewardCashNum.text = item.price

            with(binding) {
                setOnClickBatch(flBottomBut) {
                    when (this) {
                        flBottomBut-> {
                             joinCallback(item.id, item.cost)
                        }
                    }
                }



                // judge state
                val joinedIds = getJoinedIdList()
                val hasJoined = joinedIds.contains(item.id)
                val hasCompleted = item.completed
                flBottomBut.isVisible = !hasJoined
                tvParticipateAlready.isVisible = hasJoined
                //tvRemainTime.isVisible = hasJoined && !hasCompleted
            }
        }
    }

    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
        }
    }
}