package com.example.chemical.ui


import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import com.example.chemical.comm.Constants
import com.example.chemical.databinding.ActivitySettingBinding
import com.example.chemical.utils.SharedPreferencesHelper
import com.example.chemical.utils.UiManager
import com.lztek.toolkit.Lztek
import com.rc.core.log.RcLog
import com.rc.core.ui.activity.RcBaseActivity
import com.rc.httpcore.HttpConfig

class SettingActivity : RcBaseActivity<ActivitySettingBinding>() {

    private val REQUEST_ENABLE_BT = 1
    private var mLztek: Lztek? = null
    private var mDeviceNum: String? = null  //设备唯一标识
    override fun createViewBinding() = ActivitySettingBinding.inflate(LayoutInflater.from(this))

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        try {
            mLztek = Lztek.create(this)
            val toUpperCase = mLztek!!.ethMac.toUpperCase()
            mDeviceNum = toUpperCase.replace(":", "")
            viewBinding.baNo.text = "$mDeviceNum"
        } catch (e: Exception) {
            showToast("设备编码异常")
        }
        viewBinding.apiBaseUrl.setText("${HttpConfig.API_BASE_URL}")
        viewBinding.apiBaseImgUrl.setText("${HttpConfig.API_BASE_IMG_URL}")
        viewBinding.apiBaseQcUrl.setText("${HttpConfig.API_BASE_QC_URL}")
        viewBinding.cameraType.setText("${Constants.FACE_TAG}")
        try {
            val urlBase = SharedPreferencesHelper.getUrlBase(this)
            if (urlBase != null) {
                viewBinding.apiBaseUrl.setText("${urlBase.a}")
                viewBinding.apiBaseImgUrl.setText("${urlBase.b}")
                viewBinding.apiBaseQcUrl.setText("${urlBase.c}")
                viewBinding.cameraType.setText("${urlBase.d}")
            }
        } catch (e: Exception) {
        }
        viewBinding.determine.setOnClickListener {
            val a = viewBinding.apiBaseUrl.text.toString()    //api
            val b = viewBinding.apiBaseImgUrl.text.toString() //文件图片地址
            val c = viewBinding.apiBaseQcUrl.text.toString()  //二维码打印地址
            val d = viewBinding.cameraType.text.toString()  //摄像头 前后置
            val e = viewBinding.pwd.text.toString()// 工程密码
            if (a == null || b == null || c == null || d == null || e == null) {
                showToast("配置信息不能为空")
                return@setOnClickListener
            }
            val settingsBean = SettingsBean(a, b, c, "$d", e)
            RcLog.info("======本地数据 $d")
            SharedPreferencesHelper.saveUrlBase(this, settingsBean)
            UiManager.switcherCashier(this, SplashActivity::class.java)
            finish()
        }

        viewBinding.blu.setOnClickListener {

            val intent = Intent(Settings.ACTION_SETTINGS)
            startActivity(intent)
        }
    }

}