package com.zxbw.modulemain.fragment

import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxbw.modulemain.R
import com.zxhl.cms.common.NetConfig
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.utils.AdCallback
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.UserDataUtils
import kotlinx.android.synthetic.main.fragment_layout_user_center.*

/**
 * Created by gaoleichao on 2019/3/12
 */
class UserCenterFragment : BaseFragment(), AdCallback<String> {

    override fun layoutID(): Int = R.layout.fragment_layout_user_center

    override fun init(view: View?) {
        id_ll_member?.setOnClickListener {
            JumpUtils.CardVoucherJump("猫眼")
        }
        id_img_setting?.setOnClickListener {

        }
        id_ll_aboutus?.setOnClickListener {
            JumpUtils.webJump("关于我们", NetConfig.H5.WEB_URL_ABOUT_US)
        }
        id_ll_login_out?.setOnClickListener {
            showToast("1")
        }
        id_ll_xieyi_zhengze?.setOnClickListener {
            JumpUtils.webJump("隐私政策", NetConfig.H5.WEB_URL_PRIVACY)
        }
        id_ll_yonghu_xieyi?.setOnClickListener {
            JumpUtils.webJump("用户协议", NetConfig.H5.WEB_URL_USER)
        }
        id_ll_my_warehouse?.setOnClickListener {
            JumpUtils.FlashSaleJump()
        }
        id_ll_shangwu_hezuo?.setOnClickListener {
            JumpUtils.WithdrawalJump()
        }

    }

    override fun data() {
        super.data()
        userVisibleHint = true
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible) {
            UserDataUtils.updateUserInfo(this)
        }
    }

    override fun onResult(code: Int, result: String?) {
        updateView()
    }
    fun updateView(){
        val user = SettingPreference.getUserInfoData()
        if (user == null) {
            return
        }
        id_tv_user_number?.text = user.nickname
        id_tv_user_type?.text = "普通用户"
        if (!TextUtils.isEmpty(user.headImage)) {
            Glide.with(this).load(user.headImage)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .error(R.drawable.icon_default_user_img)
                .into(id_img_user_head)
        } else {
            id_img_user_head.setImageResource(R.drawable.icon_head_defalt)
        }
    }
}