package com.abbidot.tracker.adapter

import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.MenuTxtBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.MultipleEntity
import com.abbidot.tracker.ui.activity.device.set.LedLightActivity
import com.abbidot.tracker.ui.activity.device.set.LiveDurationSetActivity
import com.abbidot.tracker.ui.activity.device.set.PowerOffActivity
import com.abbidot.tracker.ui.activity.device.set.TrackingDurationSetActivity
import com.abbidot.tracker.ui.fragment.device.HomeTrackFragment
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

/**
 *
 */
class HomePetTrackMenuAdapter(
    fragment: HomeTrackFragment, list: MutableList<MenuTxtBean>?
) : BaseMultiItemQuickAdapter<MenuTxtBean, BaseViewHolder>(list) {

    private var mTypeface: Typeface
    private var mHomeTrackFragment: HomeTrackFragment = fragment

    init {
        addItemType(MultipleEntity.TEXT, R.layout.item_qmui_common_item_view)
        addItemType(MultipleEntity.IMG_TEXT, R.layout.item_qmui_common_item_view)
        addItemType(MultipleEntity.TEXT_NAME, R.layout.item_qmui_common_item_view)
        addItemType(MultipleEntity.IMG_TEXT_IMG, R.layout.item_qmui_common_item_view)
        mTypeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
    }

    override fun convert(holder: BaseViewHolder, item: MenuTxtBean) {
//        val position = getItemPosition(item)
        val qMUICommonItemView =
            holder.getView<QMUICommonListItemView>(R.id.tracker_manage_setting_item).apply {
                ViewUtil.instance.showQMUICommonListItemView(
                    context, this, null, item.menuName
                )
                background =
                    ContextCompat.getDrawable(context, R.drawable.selector_transparent_pressed)
            }
        qMUICommonItemView.textView.apply {
            typeface = mTypeface
            val tColor = if (item.type == ConstantInt.SpecialType) R.color.grey_color
            else R.color.qmui_config_color_gray_1
            setTextColor(ContextCompat.getColor(context, tColor))
        }
        qMUICommonItemView.setOnClickListener {
            when (item.menuName) {
                context.getString(R.string.tracker_manage_led) -> mHomeTrackFragment.goPetDataActivity(
                    LedLightActivity::class.java
                )

                context.getString(R.string.tracker_manage_set_duration) -> mHomeTrackFragment.goMapDeviceDataActivity(
                    TrackingDurationSetActivity::class.java
                )

                context.getString(R.string.txt_live_duration) -> mHomeTrackFragment.goMapDeviceDataActivity(
                    LiveDurationSetActivity::class.java
                )

                context.getString(R.string.txt_firmware_version) -> mHomeTrackFragment.firmwareCheckUpdate()
                context.getString(R.string.txt_power_off) -> {
                    if (item.type == ConstantInt.SpecialType) return@setOnClickListener
                    mHomeTrackFragment.goPetDataActivity(PowerOffActivity::class.java)
                }
            }
        }

        when (item.itemType) {
            MultipleEntity.TEXT -> qMUICommonItemView.detailText = item.menuValue

            MultipleEntity.IMG_TEXT -> {
            }

            MultipleEntity.TEXT_NAME -> {
                qMUICommonItemView.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_SWITCH
                qMUICommonItemView.detailText = ""
                qMUICommonItemView.switch.apply {
                    isChecked = item.menuValue == ConstantInt.Open.toString()
                    isEnabled = isChecked
                    setOnClickListener {
                        if (!isChecked) {
                            //turnOff
                            mHomeTrackFragment.operateDeviceTips(R.string.txt_turnoff_tips, 0)
                        }
                        //屏蔽点击就变状态
                        isChecked = !isChecked
                    }
                }
            }

            MultipleEntity.IMG_TEXT_IMG -> setQMUICommonItemViewHead(qMUICommonItemView, item)
        }
    }

    /**
     * 设置QMUICommonItemView 自定义布局
     */
    private fun setQMUICommonItemViewHead(
        qMUICommonItemView: QMUICommonListItemView, item: MenuTxtBean
    ) {
        qMUICommonItemView.detailText = ""
        qMUICommonItemView.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CUSTOM
        val view =
            LayoutInflater.from(context).inflate(R.layout.layout_dfu_custom_qmui_group_right, null)
        qMUICommonItemView.addAccessoryCustomView(view)

        view.findViewById<AppCompatImageView>(R.id.iv_dfu_red_dot).visibility =
            if (item.isSwitch) View.VISIBLE else View.GONE
        val firmwareName = view.findViewById<TypefaceTextView>(R.id.tv_custom_group_dfu_detail_text)
        firmwareName.text = item.menuValue
    }
}