package com.renrenliao.im.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import com.gyf.immersionbar.ktx.statusBarHeight
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMFragment
import com.renrenliao.im.ext.click
import com.renrenliao.im.ext.gone
import com.renrenliao.im.ext.visible
import com.renrenliao.im.model.repository.LocalDataSource
import com.renrenliao.im.utils.GlideUtils
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.RouteUtils
import com.renrenliao.im.viewmodel.MineViewModel
import kotlinx.android.synthetic.main.fragment_mine.*
import kotlinx.coroutines.flow.collect
import org.koin.androidx.viewmodel.ext.android.getViewModel
import kotlin.random.Random


/**
 **@ClassName AlarmsFragment
 **@Description
 **@Author gan
 **@Date 2022/5/26 12:53
 **/
class MineFragment:BaseVMFragment<MineViewModel>() {

    private val TAG = MineFragment::class.java.simpleName
    private var avatarUrl = ""

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

    override fun initVM(): MineViewModel= getViewModel()

    override fun initView() {
        ll_parent.setPadding(0,statusBarHeight,0,0)
        onClick()
    }

    override fun initData() {

    }

    @SuppressLint("SetTextI18n")
    override fun startObserve() {

        lifecycleScope.launchWhenResumed {
            mViewModel.userInfo.collect {
                tv_name.text = it.nickname
                tv_chatId.text = "Chat ID: "+ it.chatid
                //性别 1 男 0 女
                if (it.userSex == 1){
                    mImgMale.setImageResource(R.drawable.sns_friend_list_form_item_male_img)
                }else{
                    mImgMale.setImageResource(R.drawable.sns_friend_list_form_item_female_img)
                }
                //管理账号 1
                if (it.userType == 1){
                    mImgCert.visible()
                }else{
                    mImgCert.gone()
                }
                //用户头像
                avatarUrl = it.userAvatarFileName!!
                GlideUtils.loadUserImage(iv_avatar, avatarUrl)
            }
        }
//        LocalDataSource.testLiveData.observe(viewLifecycleOwner,Observer<String>{
//            LogUtils.error("sss:$it")
//        })

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        LogUtils.error(TAG+"onViewCreated")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LogUtils.error(TAG+"onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtils.error(TAG+"onDestroy")
    }

    private fun onClick(){
        //个人信息点击
        cl_userinfo.click {
            RouteUtils.gotoUserInfo(cl_userinfo.context)
        }
        //钱包点击
        rl_wallet.click {

        }
        //相册点击
        rl_album.click {

        }
        //表情点击
        rl_emotion.click {

         //   LogUtils.error(Const.getTT())
         //   val random = (0..10).random()
         //   LocalDataSource.testLiveData.postValue(random.toString())
        }
        //设置点击
        rl_setting.click {
            RouteUtils.gotoSetting(rl_setting.context)

        }
        //点击头像
        iv_avatar.click {
            RouteUtils.gotoBigImage(iv_avatar.context,avatarUrl)
        }


    }
}