package com.base.locationsharewhite.ui.set

import android.graphics.Color
import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityRenameBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.location.LocationNameUtils
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.ToastUtils.toast

class RenameActivity : BaseActivity<ActivityRenameBinding>() {

    private val TAG = "RenameActivity"

    override val binding: ActivityRenameBinding by lazy {
        ActivityRenameBinding.inflate(layoutInflater)
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())


        binding.editName.setText(LocationLoginUtils.nickNameSp)
    }

    override fun initListener() {
        super.initListener()
        binding.tvRename.setOnClickListener {
            val newName = binding.editName.text.toString()
            if (newName.isEmpty()) {
                toast(getString(R.string.name_is_empty))
                return@setOnClickListener
            }
            LocationNameUtils.renameNickName(newName) { flag ->
                runCatching {
                    if (flag) {
                        toast("Rename success")
                    } else {
                        toast("Rename failed")
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()

        val isRecreate = updateAppLanguage(MyApplication.renameLanguage, TAG) {
            MyApplication.renameLanguage = it
        }
        if (isRecreate) {
            return
        }
    }

}