package com.develop.food.base.widgets

import android.content.Context
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.RelativeLayout
import androidx.cardview.widget.CardView
import com.develop.base.R
import com.develop.base.databinding.WifiContentViewBinding
import com.develop.food.base.data.model.WifiModel
import com.develop.food.base.manager.WifiLevel
import com.develop.food.base.manager.WifiManager
import com.develop.food.base.utils.GlobalToast

class WifiContentView : CardView {
    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 mIsSelected = false

    private var model: WifiModel? = null

    init {
        val root = View.inflate(context, R.layout.wifi_content_view, this)
        binding = WifiContentViewBinding.bind(root)
        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
            }
            model?.pwd = pwd
            onOkClickListener?.onOkClick(model)
        }

    }


    fun setWifiModel(model: WifiModel) {
        this.model = model
        if (model.isOnLine) {
            binding.ivLock.visibility = View.GONE
            binding.tvWifiState.text = context.getString(R.string.on_line)
        } else {
            binding.ivLock.visibility = View.VISIBLE
            binding.tvWifiState.text = context.getString(R.string.off_line)
        }

        binding.contentLayout.setBackgroundResource(0)
        binding.inputLayout.visibility = View.GONE
        when (WifiManager.getWifiLevel(model.rssi)) {
            WifiLevel.HEIGHT -> {

            }
            WifiLevel.GENERAL -> {

            }
            WifiLevel.LOW -> {

            }
            WifiLevel.EXTREMELY -> {

            }
        }
    }

    fun showInputView(isShow: Boolean) {
        if (isShow) {
            if (mIsSelected) {
                mIsSelected = false
                binding.contentLayout.setBackgroundResource(0)
                binding.inputLayout.visibility = View.GONE
            } else {
                mIsSelected = true
                binding.contentLayout.setBackgroundResource(R.drawable.shape_30pxf4a12e_stoke)
                binding.inputLayout.visibility = View.VISIBLE
            }
        } else {
            binding.contentLayout.setBackgroundResource(0)
            binding.inputLayout.visibility = View.GONE
        }
    }

    interface OnOkClickListener {
        fun onOkClick(wifiModel: WifiModel?)
    }

}