package com.bc_chat.mine

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.entity.User
import com.bc_chat.bc_base.entity.dao.GroupMemberDao
import com.bc_chat.bc_base.utils.AppConfig
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.mine.databinding.FragmentMineBinding
import com.zhaohaoting.framework.abs.AbsV4Fragment
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.utils.glid.GlideUtils
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager
import io.rong.imkit.RongIM

/**
 * author：Created by zhaohaoting on 2019/2/20
 * email：526309416@qq.com
 * desc：
 */
@Route(path = RouteConfig.MINE_FRAGMENT)
class MineFragment : AbsV4Fragment<BaseContract.BasePresenter>(), View.OnClickListener, Observer<String> {

    companion object {
        const val REQUESTCODE_USER_INFO = 31
        const val REQUESTCODE_WALLET = 32
        const val REQUESTCODE_SERVICE = 33
        const val REQUESTCODE_FEED_BACK = 34
        const val REQUESTCODE_SETTING = 35
    }

    private val binding by lazy {
        rootDataBinding as FragmentMineBinding
    }
    private var user: User? = null

    override fun initialize() {
        binding.onClickEvent = this
        ObserverManager.getInstance().registerObserver(Const.ObserverKey.UPDATE_MINE_USER_INFO, this)
        user = AppConfig.instance.getUser()
        initData()
    }

    private fun initData() {
        GlideUtils.loadCircleImage(activity, user?.photo, binding.ivAvatar)
        binding.tvNickName.text = if (!TextUtils.isEmpty(user?.remark)) user?.remark else user?.nickname
        binding.tvAccount.text = "账号：" + user?.phone
        if (Const.SERVICE_ID.contains(RongIM.getInstance().currentUserId)) {
            binding.tvService.gone()
            binding.tvServiceLine.gone()
        } else {
//            binding.tvService.visible()
//            binding.tvServiceLine.visible()
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_user_info -> {
                ARouter.getInstance().build(RouteConfig.USER_INFO_ACTIVITY).navigation(activity, REQUESTCODE_USER_INFO)
            }
            R.id.tv_wallet -> {
//                ARouter.getInstance().navigation(ImRongService::class.java)?.navigationWallet(activity)
                ARouter.getInstance().build(RouteConfig.MY_WALLET_ACTIVITY).navigation();
            }
            R.id.tv_customer_service -> {
                ARouter.getInstance().build(RouteConfig.APPLY_WITHDRAWAL_ACTIVITY).navigation(activity, REQUESTCODE_SETTING)
            }
            R.id.tv_mine_post -> {
                ARouter.getInstance().build(RouteConfig.MINE_TOPIC).navigation()
            }
            R.id.tv_mine_fans -> {
                ARouter.getInstance().build(RouteConfig.MINE_FOLLOW).withString("type", "2").navigation()
            }
            R.id.tv_mine_like -> {
                ARouter.getInstance().build(RouteConfig.MINE_FOLLOW).withString("type", "1").navigation()
            }
            R.id.tv_service -> {
                ARouter.getInstance().build(RouteConfig.SERVICE_CENTER_ACTIVITY).navigation(activity, REQUESTCODE_SERVICE)
            }
            R.id.tv_feed_back -> {
                ARouter.getInstance().build(RouteConfig.FEEDBACK_ACTIVITY).navigation(activity, REQUESTCODE_FEED_BACK)
            }
            R.id.tv_setting -> {
                ARouter.getInstance().build(RouteConfig.SETTING_ACTIVITY).navigation(activity, REQUESTCODE_SETTING)
            }

        }
    }

    override fun onEvent(key: String?, var1: String?) {
        GroupMemberDao.instance.saveUserInfo(user)
        initData()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun initPresenter(): BaseContract.BasePresenter? {
        return null
    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_mine
    }

    override fun onDestroy() {
        super.onDestroy()
        ObserverManager.getInstance().unregisterObserver(Const.ObserverKey.UPDATE_MINE_USER_INFO, this)
    }
}
