package com.base.locationsharewhite.ui.set

import android.content.Intent
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.SpStringUtils
import com.base.locationsharewhite.utils.SpStringUtils.VIEWER_NICKNAME_KEY
import com.base.locationsharewhite.utils.SpStringUtils._DEVICE_NICKNAME_
import com.base.locationsharewhite.utils.ToastUtils.toast

class RenameActivity : BaseActivity<ActivityRenameBinding>() {

    private val TAG = "RenameActivity"

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

    private var doWhat = RENAME_USER_NAME
    private var device: String = ""
    private var nickName: String = ""

    companion object {
        const val RENAME_USER_NAME = "rename_user_name"
        const val RENAME_FRIEND_NICK_NAME = "rename_friend_nick_name"
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        doWhat = intent?.extras?.getString("doWhat", RENAME_USER_NAME) ?: RENAME_USER_NAME
        device = intent?.extras?.getString("device", "") ?: ""
        nickName = intent?.extras?.getString("nickName", "") ?: ""
    }

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

        doWhat = intent.extras?.getString("doWhat", RENAME_USER_NAME) ?: RENAME_USER_NAME
        device = intent?.extras?.getString("device", "") ?: ""
        nickName = intent?.extras?.getString("nickName", "") ?: ""

        if (doWhat == RENAME_USER_NAME) {
            binding.editName.setText(LocationLoginUtils.myNickNameSp)
        } else {
            binding.editName.setText(nickName)
            binding.tvEnterTip.text = getString(R.string.enter_friend_nickname)
        }

//        if (BuildConfig.DEBUG) {
//            binding.tvDevice.visibility = View.VISIBLE
//            binding.tvDevice.text = AppPreferences.getInstance().getString("uuid", "")
//            binding.tvDevice.setOnClickListener {
//                copyText("device", binding.tvDevice.text.toString())
//            }
//        }
    }

    override fun initListener() {
        super.initListener()
        binding.tvRename.setOnClickListener {

            when (doWhat) {
                RENAME_USER_NAME -> {
                    renameUser()
                }

                RENAME_FRIEND_NICK_NAME -> {
                    renameFriendNickName()
                }
            }

        }
    }

    private fun renameFriendNickName() {
        val newName = binding.editName.text.toString()
        if (newName.isEmpty()) {
            toast(getString(R.string.name_is_empty))
            return
        }

        val nickList = SpStringUtils.getSpStringList(VIEWER_NICKNAME_KEY)
        val olderSp = nickList.find { it.contains(device) }
        olderSp?.let { SpStringUtils.deleteSpString(VIEWER_NICKNAME_KEY, it) }

        val sp = device + _DEVICE_NICKNAME_ + newName
        SpStringUtils.addSpString(VIEWER_NICKNAME_KEY, sp)

        toast("Rename success")
        finish()
    }

    private fun renameUser() {
        val newName = binding.editName.text.toString()
        if (newName.isEmpty()) {
            toast(getString(R.string.name_is_empty))
            return
        }
        LocationNameUtils.renameNickName(newName) { flag ->
            runOnUiThread {
                if (flag) {
                    toast("Rename success")
                } else {
                    toast("Rename failed")
                }
                finish()
            }
        }
    }

    override fun onResume() {
        super.onResume()

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

}