package com.develop.common.widget

import android.content.Context
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.RelativeLayout
import androidx.cardview.widget.CardView
import com.develop.base.ext.updateText
import com.develop.base.manager.WiFiPwdType
import com.develop.base.manager.WifiData
import com.develop.base.util.GlobalToast
import com.develop.common.R
import com.develop.common.databinding.WifiContentViewBinding

class WifiContentView : RelativeLayout {
    private var binding: WifiContentViewBinding
    var onOkClickListener: OnOkClickListener? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
    }

    private var wifiData: WifiData? = null

    private  var show = false;

    init {
        val root = View.inflate(context, R.layout.wifi_content_view, this)
        binding = WifiContentViewBinding.bind(root)
        binding.contentLayout.setOnClickListener{
            if(!show){
                wifiData?.let { it1 -> onOkClickListener?.onContentClick(it1) };
            }
        }
        binding.ckbEye.setOnCheckedChangeListener { compoundButton, b ->
            if (b) {
                binding.etPwd.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding.etPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
        }

        binding.tvOk.setOnClickListener {
            val pwd = binding.etPwd.text.toString()
            if (pwd.isEmpty()) {
                GlobalToast.showToast(context.getString(R.string.pwd_can_not_be_empty))
                return@setOnClickListener
            }
            wifiData?.apply {
                wifiData?.password = pwd
                showInputView(false, this)
                onOkClickListener?.onOkClick(this)
            }
        }

    }


    fun setWifiModel(model: WifiData) {
        wifiData = model
        if (model.capabilities == WiFiPwdType.ESS) {
            binding.ivLock.visibility = View.GONE
        } else {
            binding.ivLock.visibility = View.VISIBLE
        }
        binding.tvWifiState.text = if (model.isSavePwd && model.ipAddress.isNullOrEmpty()) {
            context.getString(R.string.network_wifi_status_saved)
        } else if (model.isConnected) {
            context.getString(R.string.network_wifi_status_connected)
        } else {
            context.getString(R.string.off_line)
        }
        binding.tvWifiName.updateText(model.ssid)
        binding.ivSignal.apply {
            setImageResource(R.drawable.icon_wifi_signal_level)
            setImageLevel(model.rssi)
        }


    }

    fun showInputView(isShow: Boolean, model: WifiData) {
        if (isShow) {
            if (model.isSelected) {
                model.isSelected = false
                binding.contentLayout.setBackgroundResource(0)
                binding.inputLayout.visibility = View.GONE
            } else {
                model.isSelected = true
                binding.contentLayout.setBackgroundResource(R.drawable.shape_10pxf4a12e_stoke)
                binding.inputLayout.visibility = View.VISIBLE
            }
        } else {
            model.isSelected = false
            binding.contentLayout.setBackgroundResource(0)
            binding.inputLayout.visibility = View.GONE
        }
    }

    interface OnOkClickListener {
        fun onOkClick(wifiData: WifiData)
        fun onContentClick(wifiData: WifiData)
    }

}