package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.view.ViewGroup
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.home.custom.CustomNewGameTestUseCase
import com.gh.gamecenter.home.custom.viewholder.CustomHomeGameTestV2TimePointViewHolder
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author : liujiarui
 * date : 2023/3/3
 * description :
 */
class CustomHomeGameTestV2TimePointRvAdapter(
    context: Context,
    private val useCase: CustomNewGameTestUseCase,
    private val linkText: String,
    private val pageLocation: PageLocation

) : CustomBaseChildAdapter<String, CustomHomeGameTestV2TimePointViewHolder>(context) {

    private var selectedPosition: Int = 0

    override fun getKey(t: String): String {
        return t
    }

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

    override fun onBindViewHolder(holder: CustomHomeGameTestV2TimePointViewHolder, position: Int) {
        val text = dataList.getOrElse(position) { "" }
        holder.binding.root.setOnClickListener {
            val bindingAdapterPosition = holder.bindingAdapterPosition
            if (bindingAdapterPosition == selectedPosition) return@setOnClickListener
            selectPoint(bindingAdapterPosition, true)
            useCase.onTimePointClick(text)
        }
        val isSelected = position == selectedPosition
        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.getCurrentPreviousAndNextWeek(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 {
        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
        }
    }

    /**
     * 点击时间轴节点事件
     */
    fun selectPoint(position: Int, isClick: Boolean = false) {
        val lastSelectedPosition = selectedPosition
        selectedPosition = position
        notifyItemChanged(lastSelectedPosition)
        notifyItemChanged(position)
        //埋点上报
        val action = if (isClick) "手动点击" else "自动定位"
        NewFlatLogUtils.logGameTestV2TimeLineClick(
            action = action,
            type = getType(),
            time = getStartTime(),
            sequence = getSequence()
        )
        if (isClick) {
            SensorsBridge.trackColumnTestClick(
                location = "自定义页面",
                recommendType = linkText,
                text = "时间定位",
                bottomTab = pageLocation.bottomTab,
                multiTabName = pageLocation.severalTabPageName,
                multiTabId = pageLocation.severalTabPageId,
                position = pageLocation.tabPosition,
                tabContent = pageLocation.tabContent,
                customPageName = pageLocation.pageName,
                customPageId = pageLocation.pageId,
                buttonType = getShortDate(dataList.getOrElse(position) { "" })
            )
        }
    }

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

    fun getStartTime(): Long {
        val text = dataList.getOrElse(selectedPosition) { "" }
        return TimeUtils.getDateTimeStamp(text)
    }

    fun getSequence(): Int {
        return selectedPosition
    }
}