package com.rdiot.yx485.ui.bind

import android.content.Context
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.king.zxing.util.LogUtils
import com.lindroy.iosdialog.IAlertDialog
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.base.BindModelFactory
import com.rdiot.yx485.databinding.FraEnterWifiInfoBinding
import com.rdiot.yx485.ui.bind.model.BindViewModel
import com.rdiot.yx485.util.goToSystemWifiSetting
import com.rdiot.yx485.util.setClickLimitListener


/**
 * 输入WiFi信息页
 * @author mR2hao
 * @date 2022/11/21
 */
class EnterWifiInfoFragment : BaseFragment<FraEnterWifiInfoBinding>() {
    override fun getLayoutId() = R.layout.fra_enter_wifi_info
    private val bindViewModel: BindViewModel by activityViewModels { BindModelFactory() }
    private lateinit var wifiInfo: WifiInfo


    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.vm = bindViewModel
        binding.tvChangeWifi.setClickLimitListener {
            act.goToSystemWifiSetting(true)
        }

        binding.btnNext.setClickLimitListener {
            findNavController().navigate(R.id.action_enterWifiInfoFragment_to_bindFragment)
        }

//        binding.etWifi.doAfterTextChanged { text ->
//            binding.etPwd.setText("")
//            text?.let {
//                binding.etWifi.clearFocus()
//            }
//        }

//        getWifiInfo()

    }

    override fun onResume() {
        super.onResume()
        (act as BindActivity).setNBTitle(getString(R.string.sel_wifi))

        wifiInfo =
            (act.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo
        getWifiInfo()
    }

    /** 检查Wi-Fi信息是否正确 */
    private fun checkWifiInfoIsOk(): Boolean {
        if (getSSID().contains("unknown ssid", true)) {
            // 没有连接到WI-FI
            LogUtils.e("没有连接到 Wifi")
            IAlertDialog.build(this)
                .setTitle(R.string.hint)
                .setMessage(R.string.not_connect_wifi)
                .setCancelOutside(false)
                .setNegButtonText(R.string.got_it)
                .setNegClickListener { dialogInterface ->
                    dialogInterface.dismiss()
                }
                .setDismissible(false)
                .setAlpha(1.0F)
                .setPosButtonText(R.string.go_setting)
                .setPosClickListener { dialogInterface ->
                    act.goToSystemWifiSetting(true)
                    dialogInterface.dismiss()
                }.show()
            return false
        }

        return true
    }


    /**
     * 检查当前连接的网络是否为5G WI-FI
     */
    private fun is5GWifiConnected() = wifiInfo.frequency in 4900..5900

    /** 获取WI-FI信息 */
    private fun getWifiInfo() {

        if (checkWifiInfoIsOk()) {
            val curSsid = getSSID()
            bindViewModel.wifiSsid.set(curSsid)

            if (is5GWifiConnected()) {
                binding.tv5gErr.visibility = View.VISIBLE
            } else {
                binding.tv5gErr.visibility = View.INVISIBLE
            }
        }
//        val curSsid = getSSID()
//        LogUtils.i("当前Wifi名：$curSsid")
    }


    /**
     * 获取当前连接WIFI的SSID
     */
    private fun getSSID(): String {
        val len: Int = wifiInfo.ssid.length
        return if (wifiInfo.ssid.startsWith("\"") && wifiInfo.ssid.endsWith("\"")) {
            wifiInfo.ssid.substring(1, len - 1)
        } else {
            wifiInfo.ssid
        }
    }


}
