package com.casic.br.view

import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.provider.Settings
import android.view.View
import com.casic.br.R
import com.casic.br.extensions.initLayoutImmersionBar
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.extensions.show
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_select_wifi.*
import kotlinx.android.synthetic.main.include_base_title.*
import kotlinx.android.synthetic.main.include_base_title.leftBackView
import kotlinx.android.synthetic.main.include_base_title.titleView
import kotlinx.android.synthetic.main.include_left_back_title.*


class SelectWiFiActivity : KotlinBaseActivity() {

    private val context: Context = this@SelectWiFiActivity
    private val kTag = "SelectWifiActivity"
    private val wifi by lazy { applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager }

    override fun initLayoutView(): Int = R.layout.activity_select_wifi

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        initLayoutImmersionBar(rootView)
        leftBackView.setOnClickListener { finish() }
        titleView.visibility = View.GONE
    }

    override fun initData() {

    }

    override fun initEvent() {
        changeWifiView.setOnClickListener {
            startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
        }

        nextStepButton.setOnClickListener {
            val wifiName = wifiNameView.text.toString()
            if (wifiName.isBlank()) {
                "请确认WiFi已连接".show(this)
                return@setOnClickListener
            }
            val wifiKey = wifiKeyView.text.toString()
            if (wifiKey.isBlank()) {
                "请输入WiFi密码".show(this)
                return@setOnClickListener
            }
            navigatePageTo<ResetDeviceActivity>(arrayListOf(wifiName, wifiKey))
        }
    }

    override fun onResume() {
        super.onResume()
        //获取已连接WiFi配置
        wifiNameView.setText(wifi.connectionInfo.ssid.replace("\"", ""))
    }
}