package com.swago.home

import android.view.View
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.constant.UrlConstant
import com.swago.baseswago.dialog.BenefitBagDialog
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.ConfigModel
import com.swago.baseswago.util.*
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() {
        if (SwagoInfo.isGooglePackage()){
            binding.tvXWithdrawal.visibility = View.GONE
        }else{
            binding.tvXWithdrawal.visibility = View.VISIBLE
        }

        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.tvEdit.setOnClickListener(object: NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.editInfo).navigation()
            }

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

        })

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

        binding.tvXTopUp.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (SwagoInfo.isGooglePackage()){
                    ARouter.getInstance().build(ARouteConstant.User.recharge).navigation()
                }else{
                    ARouter.getInstance().build(ARouteConstant.Base.webView)
                        .withString("url", UrlConstant.TOP_UP)
                        .navigation()
                }

            }
        })

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

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

        binding.tvXWithdrawal.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.Base.webView)
                    .withString("url", UrlConstant.WITHDRAW)
                    .navigation()
            }
        })

        binding.tvBlackList.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.User.blacklist).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).placeholder(R.mipmap.default_avatar).error(R.mipmap.default_avatar).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.tvCoin.text = it.user_coins
            binding.tvDiamonds.text = it.user_diamonds
            binding.tvLevelView.setUserLevel(it.user_wealth_level.toInt())

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

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