package com.swago.home

import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.swago.baseswago.R
import com.swago.baseswago.UserVm
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.dialog.BenefitBagDialog
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.ConfigModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.SpUtil
import com.swago.baseswago.util.UserInfo
import com.swago.home.databinding.FragmentMineBinding

/**
 *@date 2021/11/20 10:32
 *description:
 */
class MineFragment : BaseXFragment<FragmentMineBinding>() {

    private val userVm by viewModels<UserVm>()

    override fun loadData() {}

    override fun initOther() {
        binding.llFans.setOnClickListener(object: NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance()
                    .build(ARouteConstant.User.followFan)
                    .withInt("type", 1)
                    .navigation()
            }

        })
        binding.llFollow.setOnClickListener(object: NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance()
                    .build(ARouteConstant.User.followFan)
                    .withInt("type", 0)
                    .navigation()
            }

        })

        binding.llCoin.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.recharge).navigation()
            }
        })

        binding.ivEdit.setOnClickListener(object: NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.editInfo).navigation()
            }

        })
        binding.ivSetup.setOnClickListener(object: NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.setting).navigation()
            }

        })

        binding.tvItemLevel.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.level).navigation()
            }
        })

        binding.tvRecharge.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.recharge).navigation()
            }
        })

        binding.tvTask.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.taskCenter).navigation()
            }
        })

        binding.tvAnchorBlackList.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.roomBlacklist)
                    .withInt("blackType",1).navigation()
            }
        })

        binding.tvUserBlackList.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.roomBlacklist)
                    .withInt("blackType",2).navigation()
            }
        })
    }

    override fun initLiveData() {
        userVm.userInfoLiveData.observe(this) {
            UserInfo.setUserInfo(it)
            SpUtil.putString("user_info", Gson().toJson(it))
            Glide.with(this).load(it.user_head_img_url).into(binding.ivAvatar)
            binding.tvName.text = it.user_name
            binding.tvID.text =
                AppContext.getContext().resources.getString(R.string.id_x).format(it.user_account)
            binding.tvFansNum.text = it.user_fans_num
            binding.tvFollowNum.text = it.user_follow_num
            binding.tvSign.text = it.user_signature
            binding.tvCoin.text = it.user_coins
            binding.tvDiamonds.text = it.user_diamonds
            binding.tvLevelView.setUserLevel(it.user_wealth_level.toInt())
            if (it.user_sex == 1) {
                binding.ivSex.setImageResource(R.mipmap.info_female)
            } else if (it.user_sex == 0) {
                binding.ivSex.setImageResource(R.mipmap.info_male)
            }

            if (it.is_new == 1 && it.is_benefit == 0) {
                BenefitBagDialog.newInstance().show(childFragmentManager, "BenefitBagDialog")
            }
        }
    }

    fun setCustomServiceData(configModel:ConfigModel){
        binding.tvGmail.text = "gmail:${configModel.app_gmail}"
        binding.tvWhatsApp.text = configModel.app_phone
    }

    override fun onResume() {
        super.onResume()
        userVm.getUserInfo()
    }
}