package com.zxbw.modulemain.fragment

import android.text.TextUtils
import android.util.Log
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.zxbw.modulemain.view.DialogUtils
import com.zxhl.cms.common.NetConfig
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.utils.AdCallback
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.UserDataUtils
import com.zxhl.cms.utils.Utils
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_kefu?.setOnClickListener {
            JumpUtils.webJump("客服", NetConfig.H5.WEB_URL_CUSTOMER_SERVICE)
        }
        id_tv_kaitong?.setOnClickListener {
            JumpUtils.MemberOrderJump()
        }
        id_img_setting?.setOnClickListener {
            JumpUtils.SettingJump()
        }
        id_ll_aboutus?.setOnClickListener {
            JumpUtils.AbuoutUsJump()
            // JumpUtils.webJump("关于我们", NetConfig.H5.WEB_URL_ABOUT_US)
        }
        id_ll_login_out?.setOnClickListener {
            showToast("1")
            DialogUtils.showDeleteUserIdTips(mActivity) {
                deleteUser()
            }
        }
        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.WareHouseJump()
        }
        id_ll_shangwu_hezuo?.setOnClickListener {
            //JumpUtils.WithdrawalJump()
            JumpUtils.MyOrderJump("")
        }

    }

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

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible) {
            UserDataUtils.updateUserInfo(this)
        }
    }
    private var isPause = false
    override fun onPause() {
        super.onPause()
        isPause = true

    }
    override fun onResume() {
        super.onResume()
        if (isPause) {
            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.phone
        if (user.member) {
            id_tv_user_type?.visibility = View.GONE
            id_img_vip?.visibility = View.VISIBLE
            id_ll_trip?.visibility = View.GONE
            id_tv_kaitong?.visibility = View.GONE
        } else {
            id_tv_user_type?.text = "普通用户"
            id_img_vip?.visibility = View.GONE
            id_ll_trip?.visibility = View.VISIBLE
            id_tv_kaitong?.visibility = View.VISIBLE
        }
        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)
        }
    }

    fun deleteUser() {
        ApiClient.homeApi.getLoginOut()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    if (result != null) {
                        SettingPreference.saveToken("")
                        SettingPreference.saveUserInfo(null)
                        //JumpUtils.loginJump()
                        mActivity.finish()
                        Utils.onExit(mActivity)

                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {

                }
            })
    }
}