package com.base.locationsharewhite.ui.set

import android.content.Intent
import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsShowCallBack
import com.base.locationsharewhite.databinding.ActivityRenameBinding
import com.base.locationsharewhite.helper.BaseActivity
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.VIEWING_NICKNAME_KEY
import com.base.locationsharewhite.utils.SpStringUtils._DEVICE_NICKNAME_
import com.base.locationsharewhite.utils.ToastUtils.toast

class RenameActivity : BaseActivity<ActivityRenameBinding>() {

    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_VIEWER_NICK_NAME = "rename_viewer_nick_name"
        const val RENAME_VIEWING_NICK_NAME = "rename_viewing_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())

        val recreated = changeLanguage(this)
        if (recreated) return

        if (AdsMgr.adsConfigBean.functionInShowAd) {
            AdsMgr.showInsert(this, showCallBack = object : AdsShowCallBack() {
                override fun close(where: Int) {
                }

                override fun failed(where: Int) {
                }

                override fun googleFailed(where: Int) {
                }
            })
        }

        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)
        }

        if (doWhat == RENAME_VIEWER_NICK_NAME) {
            binding.editName.setText(nickName)
            binding.tvEnterTip.text = getString(R.string.enter_friend_nickname)
        }
        if (doWhat == RENAME_VIEWING_NICK_NAME) {
            binding.editName.setText(nickName)
            binding.tvEnterTip.text = getString(R.string.enter_friend_nickname)
        }


        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit)
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        val recreated = changeLanguage(this)
        if (recreated) return
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (AdsMgr.adsConfigBean.functionBackShowAd) {
                AdsMgr.showInsert(this@RenameActivity,false,object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        finishToMain()
                    }

                    override fun failed(where: Int) {
                        finishToMain()
                    }

                    override fun googleFailed(where: Int) {
                        finishToMain()
                    }
                })
            }else{
                finishToMain()
            }
        }
        binding.tvRename.setOnClickListener {

            when (doWhat) {
                RENAME_USER_NAME -> {
                    renameUser {
                        AdsMgr.showInsert(this)
                    }
                }

                RENAME_VIEWER_NICK_NAME -> {
                    renameViewerNickName()
                    AdsMgr.showInsert(this)
                }

                RENAME_VIEWING_NICK_NAME -> {
                    renameViewingNickName()
                    AdsMgr.showInsert(this)
                }
            }

        }
    }

    private fun renameViewerNickName() {
        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", true)
        finish()
    }

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

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

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

        toast("Rename success", true)
        finish()
    }

    private fun renameUser(success: () -> Unit) {
        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", true)
                    success.invoke()
                } else {
                    toast("Rename failed", true)
                }
                finish()
            }
        }
    }

    override fun onResume() {
        super.onResume()

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

}