package com.gh.gamecenter.energy.view

import android.content.Context
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.provider.ILinkDirectUtilsProvider
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.provider.IFloatingBackViewManagerProvider
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.energy.R
import com.gh.gamecenter.energy.databinding.NoviceTaskItemBinding
import com.gh.gamecenter.energy.entity.TaskEntity
import com.gh.gamecenter.energy.utils.DialogUtils
import com.gh.gamecenter.energy.utils.IntegralLogHelper
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.adapter.BaseRecyclerAdapter

class HorizontalTaskAdapter(
    context: Context,
    private val entrance: String,
    var list: List<TaskEntity>
) : BaseRecyclerAdapter<HorizontalTaskAdapter.EnergyTaskViewHolder>(context) {


    override fun getItemCount() = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = EnergyTaskViewHolder(parent.toBinding())


    override fun onBindViewHolder(holder: EnergyTaskViewHolder, position: Int) {
        holder.binding.run {
            root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                leftMargin = if (position == 0) 16F.dip2px() else 0
            }

            val task = list[position]
            ImageUtils.display(taskIcon, task.icon)
            taskName.text = task.name

            if (task.status == "finished") {
                taskBtn.text = "已完成"
                taskBtn.setBackgroundResource(R.drawable.bg_energy_task_btn_finished)
            } else {
                taskBtn.text = "+${task.energy}光能"
                taskBtn.setBackgroundResource(R.drawable.bg_energy_task_btn_normal)
            }

            taskNotice.setOnClickListener {
                DialogUtils.showEnergyTaskNoticeDialog(mContext, task.name, task.descr)
            }

            root.setOnClickListener {
                task.run {
                    IntegralLogHelper.logTask(
                        "click_mission",
                        entrance,
                        taskId,
                        name,
                        "新手任务"
                    )
                }

                mContext.ifLogin(entrance) {
                    when (task.status) {
                        "normal" -> {
                            if (task.action == "enter_invite_code") {
                                InviteCodeDialog.showInviteCodeDialog(
                                    mContext as AppCompatActivity,
                                    UserManager.getInstance().userInfoEntity?.loginMobile?.isNotEmpty()
                                        ?: false,
                                    "task_list",
                                    true,
                                    position
                                )
                            } else {
                                val floatingBackViewManager = ARouter.getInstance()
                                    .build(RouteConsts.provider.floatingBackViewManager)
                                    .navigation() as? IFloatingBackViewManagerProvider
                                floatingBackViewManager?.enableBackViewForTaskType()
                                val directUtils = ARouter.getInstance().build(RouteConsts.provider.linkDirectUtils)
                                    .navigation() as? ILinkDirectUtilsProvider
                                directUtils?.directToLinkPage(mContext, task.link, entrance, "")
                            }
                        }

                        "limiting" -> ToastUtils.toast(R.string.limiting_tip.toResString())

//                        "finished" -> {}
                    }
                }
            }
        }
    }

    class EnergyTaskViewHolder(val binding: NoviceTaskItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}