package com.renrenliao.im.ui.activity

import androidx.lifecycle.lifecycleScope
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMActivity
import com.renrenliao.im.bean.UserInfo
import com.renrenliao.im.utils.GlideUtils
import com.renrenliao.im.viewmodel.UserInfoViewModel
import com.renrenliao.im.widget.dialog.NickNameDialog
import kotlinx.android.synthetic.main.activity_userinfo.*
import kotlinx.coroutines.flow.collect
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 **@ClassName UserInfoActivity
 **@Description 个人中心   //修改用户信息和登陆返回的不一样
 **@Author gan
 **@Date 2022/5/30 12:40
 **/
class UserInfoActivity : BaseVMActivity<UserInfoViewModel>() {

    var userInfo: UserInfo? = null
    //var nickNameDialog:NickNameDialog?= null

    override fun getLayoutResId(): Int = R.layout.activity_userinfo

    override fun initVM(): UserInfoViewModel = getViewModel()

    override fun initView() {
//        immersionBar {
//            statusBarColor(R.color.main_theme_color)
//        }
//        ll_parent.setPadding(0, statusBarHeight, 0, 0)

        onClick()
    }

    override fun initData() {

    }

    override fun startObserve() {
        lifecycleScope.launchWhenResumed {
            mViewModel.userInfo.collect {
                tv_uid.text = it.userUid.toString()
                tv_nickname.text = it.nickname
                if (it.userSex == 1) {
                    tv_sex.text = "男"
                } else {
                    tv_sex.text = "女"
                }
                tv_sign.text = it.whatsUp
                userInfo = it
                //用户头像
                GlideUtils.loadUserImage(iv_avatar, it.userAvatarFileName!!)
            }
        }

    }

    private fun onClick() {
        //返回
        tool_bar.setOnBackListener {
            onBackPressed()
        }
        //修改昵称
        rl_nickname.setOnClickListener {

            val nickNameDialog = NickNameDialog(this, userInfo?.nickname ?: "")
            nickNameDialog.setOnClickListener {
                val map = HashMap<String,String>()
                map["nickName"] = it
                map["sex"] = userInfo?.userSex.toString()
                map["whatsUp"] = userInfo?.whatsUp?:""
                map["userDesc"] = userInfo?.userDesc?:""

                mViewModel.updateUserInfo(map)
            }
            nickNameDialog.show()
        }

    }


}