package com.casic.common.detector.gd.view

import android.content.Intent
import android.os.Bundle
import com.casic.common.detector.gd.databinding.ActivityRtkConfigBinding
import com.casic.common.detector.gd.utils.LocaleConstant
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.pengxh.kt.lite.widget.TitleBarView

class RtkConfigActivity : KotlinBaseActivity<ActivityRtkConfigBinding>() {

    override fun initEvent() {
        binding.saveButton.setOnClickListener {
            val account = binding.accountView.text.toString().trim()
            if (account.isBlank()) {
                "请输入差分定位服务账号！".show(this)
                return@setOnClickListener
            }

            val password = binding.passwordView.text.toString().trim()
            if (password.isBlank()) {
                "请输入密码！".show(this)
                return@setOnClickListener
            }

            val ip = binding.serverIpView.text.toString().trim()
            if (ip.isBlank()) {
                "请输入差分定位服务器IP！".show(this)
                return@setOnClickListener
            }

            val port = binding.serverPortView.text.toString().trim()
            if (port.isBlank()) {
                "请输入差分定位服务器端口号！".show(this)
                return@setOnClickListener
            }

            SaveKeyValues.putValue(LocaleConstant.ACK, account)
            SaveKeyValues.putValue(LocaleConstant.ACS, password)
            SaveKeyValues.putValue(LocaleConstant.RTK_SERVER, ip)
            SaveKeyValues.putValue(LocaleConstant.RTK_PORT, port)

            Intent().also {
                setResult(RESULT_OK, it)
            }
            finish()
        }
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        binding.accountView.setText(
            SaveKeyValues.getValue(LocaleConstant.ACK, "qxtmcr0087663") as String
        )

        binding.passwordView.setText(
            SaveKeyValues.getValue(LocaleConstant.ACS, "1f1f9b9") as String
        )

        binding.serverIpView.setText(
            SaveKeyValues.getValue(LocaleConstant.RTK_SERVER, "203.107.45.154") as String
        )

        binding.serverPortView.setText(
            SaveKeyValues.getValue(LocaleConstant.RTK_PORT, "8003") as String
        )
    }

    override fun initViewBinding(): ActivityRtkConfigBinding {
        return ActivityRtkConfigBinding.inflate(layoutInflater)
    }

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        binding.titleView.setOnClickListener(object : TitleBarView.OnClickListener {
            override fun onLeftClick() {

            }

            override fun onRightClick() {
                finish()
            }
        })
    }
}