package com.abbidot.tracker.ui.common

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.RecyclerView
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.PetLabelsAdapter
import com.abbidot.tracker.adapter.TextViewAdapter
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.bean.LabelBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.dialog.CommonListDialog
import com.abbidot.tracker.dialog.SelectPetLabelDialog
import com.abbidot.tracker.dialog.ShowCalenderDialog
import com.abbidot.tracker.util.ViewUtil
import com.haibin.calendarview.Calendar

/**
 *Created  by .yzq on 2024/6/16/016.
 * @link
 * @description:
 */
class PetProfileCommon(
    context: Context, showSelectLabelsRecyclerView: RecyclerView, petBean: PetBean?
) {

    private val mContext = context
    private val mShowSelectLabelsRecyclerView = showSelectLabelsRecyclerView

    private var mSelectLabelList: MutableList<LabelBean> = mutableListOf()
    private val mPetBean = petBean

    private var mSelectLabelDialog: SelectPetLabelDialog? = null
    private var mBirthdayDialog: ShowCalenderDialog? = null
    private var mProfileSexDialog: CommonListDialog? = null

    private lateinit var mLabelsAdapter: PetLabelsAdapter
    private lateinit var mSexAdapter: TextViewAdapter
    private val mSelectLabelsAdapter: PetLabelsAdapter =
        PetLabelsAdapter(mContext, mSelectLabelList)

    init {
        ViewUtil.instance.setRecyclerViewFlexboxLayout(
            mContext,
            mShowSelectLabelsRecyclerView,
            mSelectLabelsAdapter,
            right = AppUtils.dpToPx(6),
            bottom = AppUtils.dpToPx(8)
        )

        mLabelsAdapter = PetLabelsAdapter(mContext, null).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    if (mLabelsAdapter.getData().isNotEmpty()){
                        val label = mLabelsAdapter.getData()[pos]
                        label.check = !label.check
                        notifyItemChanged(pos)
                    }
                }
            })
        }
    }

    fun getLabelData(labels: MutableList<String>) {
        val labelList = mutableListOf<LabelBean>()
        for (i in 0 until labels.size) {
            LabelBean().run {
                name = labels[i]
                if (mSelectLabelList.size > 0) {
                    for (label in mSelectLabelList) {
                        if (name == label.name) check = true
                    }
                }
                labelList.add(this)
            }
        }
        mLabelsAdapter.setData(labelList, true)
    }

    /**
     * 显示标签弹窗
     */
    fun showLabelDialog(okListener: BaseDialog.OnDialogOkListener) {
        if (null == mSelectLabelDialog) {
            mSelectLabelDialog = SelectPetLabelDialog(mContext, mLabelsAdapter).apply {
                setOkListener(okListener)
            }
        }
        mSelectLabelDialog?.show()
    }

    /**
     * 显示已选的标签
     */
    fun showSelectPetLabels(
        labelTitle: TextView, scrollView: NestedScrollView
    ) {
        val labelList = mLabelsAdapter.getData()
        mSelectLabelList.clear()
        mSelectLabelsAdapter.notifyItemRangeChanged(0, mSelectLabelList.size)
        for (lab in labelList) {
            if (lab.check) {
                LabelBean().apply {
                    name = lab.name
                    mSelectLabelList.add(this)
                }
            }
        }
        mSelectLabelsAdapter.setData(mSelectLabelList, true)

        val textColor = if (mSelectLabelList.size > 0) ContextCompat.getColor(
            mContext, R.color.data_black_color
        )
        else ContextCompat.getColor(mContext, R.color.block_color2)
        labelTitle.setTextColor(textColor)

        scrollView.fullScroll(View.FOCUS_DOWN)

        mPetBean?.apply {
            labels = if (mSelectLabelList.size > 0) mSelectLabelList.joinToString(",") { it.name }
            else ""
        }
    }

    /**
     * 显示生日弹窗
     */
    fun showBirthdayDialog(showSelectDateView: TextView) {
        if (null == mBirthdayDialog) {
            val calendar = java.util.Calendar.getInstance()
            val cYear = calendar.get(java.util.Calendar.YEAR)
            mBirthdayDialog =
                ShowCalenderDialog(mContext, object : BaseDialog.OnDialogSelectListener {
                    override fun onSelectClick(dialog: BaseDialog<*>, any: Any) {
                        if (any is Calendar) {
                            val selectBirthday =
                                Utils.formatTime(any.timeInMillis, Utils.DATE_FORMAT_PATTERN_EN3)
                            showSelectDateView.text = selectBirthday
                            mPetBean?.apply {
                                birthdayDate =
                                    Utils.formatTime(any.timeInMillis, Utils.DATE_FORMAT_PATTERN_CN)
                            }
                        }
                    }
                },minYear = cYear-20)
        }
        mBirthdayDialog?.show()
    }

    /**
     * 设置性别数据
     */
    fun setSexListData(profileSexTextView: TextView) {
        val sexList = mutableListOf<DataBean>()
        DataBean().run {
            name = mContext.getString(R.string.pet_manage_gender_male)
            menuType = ConstantInt.Man
            sexList.add(this)
        }
        DataBean().run {
            name = mContext.getString(R.string.pet_manage_gender_female)
            menuType = ConstantInt.WoMan
            sexList.add(this)
        }
        mSexAdapter = TextViewAdapter(mContext, sexList).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    val dataBean = sexList[pos]
                    mPetBean?.apply {
                        gender = dataBean.menuType
                    }
                    profileSexTextView.text = sexList[pos].name
                    mProfileSexDialog?.dismiss()
                }
            })
        }
    }

    /**
     * 显示性别弹窗
     */
    fun showSexDialog() {
        if (null == mProfileSexDialog) {
            mProfileSexDialog = CommonListDialog(mContext, mSexAdapter, 3, 3)
        }
        mProfileSexDialog?.show()
    }
}