package com.gh.gamecenter.home.test_v2

import android.content.Context
import android.view.ViewGroup
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.core.utils.TimeUtils
import com.lightgame.adapter.BaseRecyclerAdapter
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author : liujiarui
 * date : 2023/3/3
 * description :
 */
class HomeGameTestV2TimePointRvAdapter(
    context: Context,
    private val mViewModel: HomeGameTestV2ViewModel,
    var data: List<String>
) : BaseRecyclerAdapter<HomeGameTestV2TimePointViewHolder>(context) {

    private var mSelectedPosition: Int = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeGameTestV2TimePointViewHolder {
        return HomeGameTestV2TimePointViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: HomeGameTestV2TimePointViewHolder, position: Int) {
        val text = data.getOrElse(position) { "" }
        holder.binding.root.setOnClickListener {
            val bindingAdapterPosition = holder.bindingAdapterPosition
            if (bindingAdapterPosition == mSelectedPosition) return@setOnClickListener
            selectPoint(bindingAdapterPosition, true)
            mViewModel.onTimePointClick(text)
        }
        val isSelected = position == mSelectedPosition
        val todayAndWeekText = if (isSelected) {
            getShortDate(text)
        } else {
            getTodayAndWeekText(text)
        }
        holder.bind(
            todayAndWeekText,
            position == 0,
            position == itemCount - 1,
            isSelected
        )
    }

    /**
     * 把日期文本 yyyy-MM-dd 转换成 今天，周几，下周几格式
     */
    private fun getTodayAndWeekText(date: String): String {
        val today = TimeUtils.getToday()
        if (today == date) return "今天"
        val list = TimeUtils.getCurrentAndNextWeek(pattern = "yyyy-MM-dd")
        list.find { it.first == date }?.let {
            return it.second
        } ?: return getShortDate(date)
    }

    /**
     * 把日期文本 yyyy-MM-dd 转换成 MM.dd
     */
    private fun getShortDate(date: String): String {
        if (date == "recommend") return "推荐"
        val format: DateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        return try {
            val d = format.parse(date) ?: return date
            SimpleDateFormat("MM.dd", Locale.CHINA).format(d)
        } catch (e: Exception) {
            date
        }
    }

    /**
     * 时间轴日期转时间戳
     */
    private fun getDateTimeStamp(date: String): Long {
        if (date == "recommend") return 0
        val format: DateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        return try {
            val d = format.parse(date) ?: return 0
            d.time
        } catch (e: Exception) {
            0
        }
    }

    /**
     * 点击时间轴节点事件
     */
    fun selectPoint(position: Int, isClick: Boolean = false) {
        val lastSelectedPosition = mSelectedPosition
        mSelectedPosition = position
        notifyItemChanged(lastSelectedPosition)
        notifyItemChanged(position)
        //埋点上报
        val action = if (isClick) "手动点击" else "自动定位"
        NewFlatLogUtils.logGameTestV2TimeLineClick(
            action = action,
            type = getType(),
            time = getStartTime(),
            sequence = getSequence()
        )
    }

    fun getType(): String {
        val text = data.getOrElse(mSelectedPosition) { "" }
        return if (text == "recommend") "推荐" else "全部"
    }

    fun getStartTime(): Long {
        val text = data.getOrElse(mSelectedPosition) { "" }
        return getDateTimeStamp(text)
    }

    fun getSequence(): Int {
        return mSelectedPosition
    }
}