package com.cr.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.CompoundButton.OnCheckedChangeListener
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import com.cr.cruav.R
import com.cr.data.FontManager
import com.cr.models.SetItemModel
import com.cr.models.SettingAction

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/11 10:01
 * 描述：设置项适配器
 */
class SettingAdapter @JvmOverloads constructor(
    context: Context? = null,
    dataList: MutableList<SetItemModel>
) : CrAdapter<SetItemModel>(context, dataList), View.OnClickListener, OnCheckedChangeListener {
    /**
     * 对外接口
     */
    interface SettingListener {
        // todo: 2023/4/11 动作监听
        fun onAction(action: SettingAction)

        // todo: 2023/4/11 开关监听
        fun onSwitch(action: SettingAction, isChecked: Boolean)
    }

    // define: 2023/4/11 定义监听
    var listener: SettingListener? = null

    /**
     * 重写获取视图方法
     * @param position Int
     * @param convertView View?
     * @param viewGroup ViewGroup?
     * @return View?
     */
    override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View? {
        var holder: ViewHolder
        var view = convertView
        if (convertView == null) {
            holder = ViewHolder()
            view = inflater!!.inflate(R.layout.item_set, null)
            holder.imageView = view.findViewById(R.id.item_set_img)
            holder.lblTitle = view.findViewById(R.id.item_set_title)
            holder.icoArrow = view.findViewById(R.id.item_set_arrow)
            FontManager.setTextView(context!!, holder.icoArrow!!, R.string.ico_arrow)
            holder.aSwitch = view.findViewById(R.id.item_set_switch)
            view.tag = holder
        } else {
            holder = view!!.tag as ViewHolder
        }
        var model: SetItemModel = dataList!![position]
        holder.lblTitle?.text = model.title
        holder.imageView?.setImageDrawable(context!!.resources.getDrawable(model.imageId))
        holder.aSwitch?.visibility = if (model.isSwitch) View.VISIBLE else View.GONE
        // todo: 2023/4/11 设置开关状态
        holder.aSwitch?.isChecked = model.switchIsOn
        // todo: 2023/4/11 设置箭头
        holder.icoArrow?.visibility = if (model.isArrow) View.VISIBLE else View.GONE
        // todo: 2023/4/11 特殊处理
        if (model.isSwitch) {
            holder.lblTitle?.isClickable = false
            holder.aSwitch?.tag = model
            holder.aSwitch?.setOnCheckedChangeListener(this)
        } else {
            holder.lblTitle?.isClickable = true
            holder.lblTitle?.tag = model
            holder.lblTitle?.setOnClickListener(this)
        }
        return view
    }

    companion object {
        class ViewHolder {
            var imageView: ImageView? = null
            var lblTitle: TextView? = null
            var icoArrow: TextView? = null
            var aSwitch: Switch? = null
        }
    }

    /**
     * 视图点击事件
     * @param p0 View
     */
    override fun onClick(p0: View?) {
        var model:SetItemModel = p0?.tag as SetItemModel
        if(listener != null) model.action?.let { listener?.onAction(it) }
    }

    /**
     * 开关事件
     * @param p0 CompoundButton
     * @param p1 Boolean
     */
    override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
        var model:SetItemModel = p0?.tag as SetItemModel
        if(listener != null) model.action?.let { listener?.onSwitch(it,p1) }
    }
}