package com.abbidot.tracker.adapter

import android.content.Intent
import android.text.TextUtils
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.MenuTxtBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.MultipleEntity
import com.abbidot.tracker.ui.activity.HomeV2Activity
import com.abbidot.tracker.ui.activity.help.FeedbackActivity
import com.abbidot.tracker.ui.activity.help.HowWorkActivity
import com.abbidot.tracker.ui.activity.set.AboutActivity
import com.abbidot.tracker.ui.activity.set.AdvancedSetActivity
import com.abbidot.tracker.ui.activity.set.NotificationsSetActivity
import com.abbidot.tracker.ui.activity.set.PermissionsSetActivity
import com.abbidot.tracker.ui.common.UnitSelectDialog
import com.abbidot.tracker.ui.fragment.account.AccountV2Fragment
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.widget.TypefaceTextView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView

/**
 *Created  by .yzq on 2023/12/27/027.
 * @link
 * @description:
 */
class HomeAccountMenuAdapter(
    accountV2Fragment: AccountV2Fragment, list: MutableList<MenuTxtBean>?
) : BaseMultiItemQuickAdapter<MenuTxtBean, BaseViewHolder>(list) {

    private val mLogOutTipsType = 0
    private val mClearCacheTipsType = 1
    private val mCloseAccountTipsType = 2
    private lateinit var mHomeV2Activity: HomeV2Activity
    private lateinit var mUnitSelectDialog: UnitSelectDialog
    private val mAccountV2Fragment = accountV2Fragment
    private var isShowDialog = false

    init {
        addItemType(MultipleEntity.TEXT, R.layout.item_title_text_type)
        addItemType(MultipleEntity.IMG_TEXT, R.layout.item_qmui_common_item_view)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mHomeV2Activity = context as HomeV2Activity
        mUnitSelectDialog = UnitSelectDialog(context)
    }

    override fun convert(holder: BaseViewHolder, item: MenuTxtBean) {
        val position = getItemPosition(item)
        when (item.itemType) {
            MultipleEntity.TEXT -> {
                val typefaceTextView = holder.getView<TypefaceTextView>(R.id.tv_title_name)
                typefaceTextView.text = item.menuName
            }

            MultipleEntity.IMG_TEXT -> {
                holder.getView<QMUICommonListItemView>(R.id.tracker_manage_setting_item).run {
                    ViewUtil.instance.showQMUICommonListItemView(
                        context, this, {
//                            Toaster.show("$position")
                            when (position) {
                                1 -> mHomeV2Activity.startActivity(
                                    Intent(
                                        context, HowWorkActivity::class.java
                                    )
                                )

                                2 -> mHomeV2Activity.startActivity(
                                    Intent(
                                        context, FeedbackActivity::class.java
                                    )
                                )

                                4 -> mHomeV2Activity.startActivity(
                                    Intent(
                                        context, NotificationsSetActivity::class.java
                                    )
                                )

                                5 -> mUnitSelectDialog.showUnitDialog(object :
                                    BaseDialog.OnDialogSelectListener {
                                    override fun onSelectClick(dialog: BaseDialog<*>, any: Any) {
                                        if (any is Int) {
                                            val unit = any
                                            val oldUnit = Util.getMeasureSpUnit()
                                            if (unit == oldUnit) {
                                                return
                                            }
                                            item.menuValue =
                                                if (unit == ConstantInt.Type1) context.getString(R.string.txt_imperial)
                                                else context.getString(R.string.txt_metric)
                                            mAccountV2Fragment.setMeasureUnit(unit)
                                        }
                                    }
                                })
//                                    mHomeV2Activity.startActivity(
//                                        Intent(
//                                            context, FirstMeasurementUnitsActivity::class.java
//                                        )
//                                    )


                                6 -> mHomeV2Activity.startActivity(
                                    Intent(
                                        context, PermissionsSetActivity::class.java
                                    )
                                )

                                7 -> mHomeV2Activity.startActivity(
                                    Intent(
                                        context, AdvancedSetActivity::class.java
                                    )
                                )

                                9 -> mHomeV2Activity.startActivity(
                                    Intent(
                                        context, AboutActivity::class.java
                                    )
                                )

                                10 -> dialogShowType(
                                    mLogOutTipsType,
                                    mHomeV2Activity.getString(R.string.txt_log_out_tips),
                                    position
                                )
                            }
                        }, item.menuName
                    )
                    if (!TextUtils.isEmpty(item.menuValue)) {
                        detailText = item.menuValue
                    }
                    setImageDrawable(ContextCompat.getDrawable(context, item.imageId))
                }
            }
        }
    }

    private fun dialogShowType(type: Int, string: String, pos: Int) {
        if (isShowDialog) return
        isShowDialog = true
        ViewUtil.instance.showDialog(
            context,
            string,
            object : BaseDialog.OnDialogOkListener {
                override fun onOkClick(dialog: BaseDialog<*>) {
                    isShowDialog = false
                    dialog.dismiss()
                    when {
                        mLogOutTipsType == type -> {
                            Util.logOut(mHomeV2Activity, mHomeV2Activity.lifecycleScope)
                        }

                        mCloseAccountTipsType == type -> {
//                        closeAccount()
                        }

                        else -> {
//                        mHomeV2Activity.clearCache(this@HomeAccountMenuAdapter, pos)
                        }
                    }
                }
            },
            okTextResId = R.string.txt_sure,
            cancelListener = object : BaseDialog.OnDialogCancelListener {
                override fun onCancelClick(dialog: BaseDialog<*>) {
                    isShowDialog = false
                    dialog.dismiss()
                }
            })
    }
}