package com.gh.gamecenter.personal

import android.content.Context
import android.os.Build
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.CheckLoginUtils
import com.gh.common.util.DataCollectionUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.*
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.databinding.GameCollectionBannerItemBinding
import com.gh.gamecenter.entity.AddonLinkEntity
import com.gh.gamecenter.game.upload.GameSubmissionActivity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.setting.SettingBridge
import com.gh.gamecenter.simulatorgame.SimulatorGameActivity
import com.gh.gamecenter.teenagermode.TeenagerModeActivity
import com.gh.gamecenter.toolbox.ToolBoxActivity
import com.gh.gamecenter.video.videomanager.VideoManagerActivity
import com.lightgame.adapter.BaseRecyclerAdapter

class HaloPersonalBannerAdapter(context: Context) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private val mBannerList = ArrayList<AddonLinkEntity>()

    fun submitList(updateList: ArrayList<AddonLinkEntity>) {
        mBannerList.clear()
        mBannerList.addAll(updateList)
        AppExecutor.uiExecutor.execute {
            notifyDataSetChanged()
        }
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is PersonalBannerViewHolder && mBannerList.isNotEmpty()) {
            val realPosition = getActualPosition(position)
            val entity = mBannerList.safelyGetInRelease(realPosition)
            if (entity != null) {
                holder.bindBanner(entity, realPosition)
            }
        }
    }

    override fun getItemCount() = if (getActualSize() <= 1) getActualSize() else Int.MAX_VALUE

    fun getActualPosition(position: Int): Int {
        var actualPosition = position
        val actualSize = getActualSize()
        if (actualSize in 1..position) {
            actualPosition = position % actualSize
        }
        return actualPosition
    }

    fun getActualSize() = mBannerList.size

    fun getActualFirstPositionInCenter(): Int {
        var index = itemCount / 2
        while (getActualPosition(index) != 0) {
            index++
        }
        return index
    }

    inner class PersonalBannerViewHolder(val binding: GameCollectionBannerItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root) {
        fun bindBanner(entity: AddonLinkEntity, position: Int) {
            ImageUtils.display(binding.bannerIv, entity.icon)
            binding.root.setOnClickListener {
                NewFlatLogUtils.logHaloSelfClick("活动位", entity.link?.text ?: "")
                SensorsBridge.trackHaloSelfClick(
                    profile = "活动位",
                    text = entity.link?.text ?: "",
                    linkType = entity.link?.type ?: "",
                    linkId = entity.link?.link ?: "",
                    linkText = entity.link?.text ?: ""
                )
                NewFlatLogUtils.logHaloPersonalActivityBannerClick(
                    entity.link?.link ?: "",
                    entity.link?.type ?: "",
                    entity.link?.text ?: "",
                )
                SensorsBridge.trackMyPageBannerClick(position)
                entity.link?.let {
                    when (it.type) {
                        "视频投稿" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH || BuildConfig.DEBUG) {
                                    mContext.startActivity(
                                        VideoManagerActivity.getIntent(
                                            mContext,
                                            "",
                                            "我的光环-视频投稿"
                                        )
                                    )
                                } else {
                                    DialogHelper.showDialog(
                                        mContext, "提示",
                                        "抱歉，您当前系统版本过低，暂不支持视频功能", "我知道了", ""
                                    )
                                }
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-视频投稿") {}
                            }
                        }

                        "账号安全" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                mContext.startActivity(SettingBridge.getSecurityIntent(mContext, "我的光环-账号安全"))
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-账号安全") {}
                            }
                        }

                        "模拟器游戏" -> {
                            mContext.startActivity(SimulatorGameActivity.getIntent(mContext))
                        }

                        "收货信息" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                mContext.startActivity(DeliveryInfoActivity.getIntent(mContext))
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-收货信息") {}
                            }
                        }

                        "实名认证" -> {
                            mContext.startActivity(
                                ShellActivity.getIntent(
                                    mContext,
                                    ShellActivity.Type.REAL_NAME_INFO,
                                    null
                                )
                            )
                        }

                        "微信提醒" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                mContext.startActivity(WebActivity.getBindWechatIntent(mContext))
                                SensorsBridge.trackEvent(
                                    "AppointmenWechatRemindConfigPageShow",
                                    "source_entrance",
                                    "我的光环-微信提醒"
                                )
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-微信提醒") { }
                            }
                        }

                        "游戏动态" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                DataCollectionUtils.uploadClick(mContext, "游戏动态", "发现")
                                DirectUtils.directToConcernInfo(mContext, "我的光环-banner")
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-游戏动态") { }
                            }
                        }

                        "资讯中心" -> {
                            DataCollectionUtils.uploadClick(mContext, "资讯中心", "发现")
                            mContext.startActivity(InfoActivity.getIntent(mContext))
                        }

                        "礼包中心" -> {
                            DataCollectionUtils.uploadClick(mContext, "礼包中心", "发现")
                            DirectUtils.directToGift(mContext, "(发现:礼包)")
                        }

                        "工具箱" -> {
                            DataCollectionUtils.uploadClick(mContext, "工具箱", "发现")
                            mContext.startActivity(ToolBoxActivity.getIntent(mContext, "(发现:工具箱)"))
                        }

                        "安装包清理" -> {
                            DataCollectionUtils.uploadClick(mContext, "安装包清理", "发现")
                            PermissionHelper.checkManageAllFilesOrStoragePermissionBeforeAction(mContext) {
                                mContext.startActivity(CleanApkActivity.getIntent(mContext))
                            }
                        }

                        "个人中心" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                mContext.startActivity(UserInfoActivity.getIntent(mContext))
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-个人中心") {}
                            }
                        }

                        "游戏投稿" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                PermissionHelper.checkStoragePermissionBeforeAction(mContext) {
                                    mContext.startActivity(
                                        GameSubmissionActivity.getIntent(mContext, "(我的光环)", "")
                                    )
                                }
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-游戏投稿") { }
                            }
                        }

                        "视频数据" -> {
                            if (UserManager.getInstance().isLoggedIn) {
                                DirectUtils.directVideoData(mContext, "我的光环-视频数据")
                            } else {
                                CheckLoginUtils.checkLogin(mContext, "我的光环-视频数据") {}
                            }
                        }

                        "青少年模式" -> {
                            mContext.startActivity(TeenagerModeActivity.getIntent(mContext))
                        }

                        "开服管理" -> {// 开服管理
                            DirectUtils.directToServersCalendarManagement("我的光环-活动位")
                        }

                        else -> {
                            DirectUtils.directToLinkPage(mContext, it, "我的光环banner", "", "我的光环-活动banner")
                        }
                    }
                }
            }
        }
    }
}