package com.yidian.shenghuoquan.broot.ui

import android.view.View
import android.widget.RadioButton
import androidx.activity.viewModels
import com.yidian.bcommon.constant.RunConfig
import com.yidian.bcommon.mvvm.BaseMvvmActivity
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.shenghuoquan.broot.R
import com.yidian.shenghuoquan.broot.databinding.RootActivityTestBinding
import com.yidian.shenghuoquan.broot.ui.test.TestViewModel

/**
 * 环境切换
 */
class TestActivity : BaseMvvmActivity<RootActivityTestBinding, TestViewModel>() {

  override val layoutId: Int = R.layout.root_activity_test

  override val vm: TestViewModel by viewModels()

  override fun initView() {
    initEnvironment()
    subscribeUiEvent()
  }

  private fun initEnvironment() {
    when (RunConfig.currentEnvironment()) {
      RunConfig.RunEnvironment.DEV -> binding.radioDev.isChecked = true
      RunConfig.RunEnvironment.TEST -> binding.radioTest.isChecked = true
      RunConfig.RunEnvironment.PROD -> binding.radioProd.isChecked = true
    }
  }

  fun onRadioButtonClicked(view: View) {
    if (view is RadioButton) {
      val checked = view.isChecked
      when (view.getId()) {
        R.id.radio_dev ->
          if (checked) {
            RunConfig.switchEnvironment(RunConfig.RunEnvironment.DEV)
            ToastUtils.showShortSafe("切换为开发环境")
          }
        R.id.radio_test ->
          if (checked) {
            RunConfig.switchEnvironment(RunConfig.RunEnvironment.TEST)
            ToastUtils.showShortSafe("切换为测试环境")
          }
        R.id.radio_prod ->
          if (checked) {
            RunConfig.switchEnvironment(RunConfig.RunEnvironment.PROD)
            ToastUtils.showShortSafe("切换为生产环境")
          }
      }
      finish()
    }
  }

  private fun subscribeUiEvent() {

  }

}
