package com.develop.setting.init.adapter

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.develop.food.base.manager.wifi.WiFiPwdType
import com.develop.food.base.manager.wifi.WifiData
import com.develop.food.base.utils.GlobalToast
import com.develop.setting.R

class WifiListAdapter : BaseQuickAdapter<WifiData, BaseViewHolder>
    (R.layout.item_wifi_content) {
    private var selectedPosition = -1
    var onOkClickListener: OnOkClickListener? = null
    override fun convert(holder: BaseViewHolder, item: WifiData) {
        holder.getView<AppCompatTextView>(R.id.tv_wifi_name).text = item.ssid
        val tvWifiState = holder.getView<AppCompatTextView>(R.id.tv_wifi_state)
        val wifiLevel = holder.getView<ImageView>(R.id.iv_signal)
        val lockView = holder.getView<ImageView>(R.id.iv_lock)
        val contentLayout = holder.getView<RelativeLayout>(R.id.content_layout)
        val inputLayout = holder.getView<RelativeLayout>(R.id.input_layout)
        val ckbEye = holder.getView<CheckBox>(R.id.ckb_eye)
        val etPwd = holder.getView<EditText>(R.id.et_pwd)
        val tvOk = holder.getView<AppCompatTextView>(R.id.tv_ok)
        if (item.capabilities == WiFiPwdType.ESS) {
            lockView.visibility = View.GONE
        } else {
            lockView.visibility = View.VISIBLE
        }
        tvWifiState.text = if (item.isSavePwd && item.ipAddress.isNullOrEmpty()) {
            context.getString(R.string.network_wifi_status_saved)
        } else if (item.isConnected) {
            context.getString(R.string.network_wifi_status_connected)
        } else {
            context.getString(R.string.off_line)
        }
        if (selectedPosition == holder.adapterPosition) {
            if (item.isSelected) {
                item.isSelected = false
                contentLayout.setBackgroundResource(0)
                inputLayout.visibility = View.GONE

            } else {
                item.isSelected = true
                contentLayout.setBackgroundResource(R.drawable.shape_30pxf4a12e_stoke)
                inputLayout.visibility = View.VISIBLE
            }
        } else {
            item.isSelected = false
            contentLayout.setBackgroundResource(0)
            inputLayout.visibility = View.GONE
        }
        wifiLevel.setImageResource(R.drawable.icon_wifi_signal_level)
        wifiLevel.setImageLevel(item.rssi)

        ckbEye.setOnCheckedChangeListener { compoundButton, b ->
            if (b) {
                etPwd.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                etPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
        }
        tvOk.setOnClickListener {
            val pwd = etPwd.text.toString()
            if (pwd.isEmpty()) {
                GlobalToast.showToast(context.getString(R.string.pwd_can_not_be_empty))
                return@setOnClickListener
            }
            item.password = pwd
            onOkClickListener?.onOkClick(item)
            contentLayout.setBackgroundResource(0)
            inputLayout.visibility = View.GONE
        }

    }

    fun updateSelectedPosition(adapterPosition: Int) {
        val tempPosition = selectedPosition
        selectedPosition = adapterPosition
        notifyItemChanged(selectedPosition)
        notifyItemChanged(tempPosition)
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    interface OnOkClickListener {
        fun onOkClick(accessPoint: WifiData)
    }

}