package com.zxhl.main.page.activity

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.box.MagicCoinLogEntity
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.adapter.MagicCoinAdapter
import com.zxhl.main.page.contract.MagicCoinContract
import com.zxhl.main.page.presenter.MagicCoinPresenter
import kotlinx.android.synthetic.main.activity_layout_my_magic_coin.*


class MyMagicCoinActivity:BaseActivity(),MagicCoinContract.View {
    private var mPresenter:MagicCoinPresenter?=null
    private var mAdapter:MagicCoinAdapter?=null
    override fun onClick(v: View?) {

    }
    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }
    override fun layoutID(): Int {
        return R.layout.activity_layout_my_magic_coin
    }

    override fun init() {
        id_img_back?.setOnClickListener { finish() }
        setUserInfo()
        mPresenter= MagicCoinPresenter(this)
        mAdapter= MagicCoinAdapter(this)
        id_rl_magic_coin_detail?.layoutManager=LinearLayoutManager(this)
        id_rl_magic_coin_detail.adapter=mAdapter
        mPresenter?.getMagicCoinLog(true)
        id_refresh_layout.setColorSchemeColors(resources.getColor(R.color.color_3A95FD))
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getMagicCoinLog(true)
        }
        id_rl_magic_coin_detail.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (Utils.isSlideToBottom(recyclerView)) {
                    if ((mAdapter?.list?.size ?: 0) > 0) {
                        mPresenter?.getMagicCoinLog(false)
                    }
                }
            }
        })

    }


    fun setUserInfo(){
        val user = SettingPreference.getUserInfoData()
        if (user == null) {
            return
        }
        id_tv_nick_name?.text = user.nickname
        id_tv_my_magic_coin_num?.text = "我的魔币：${user.avaMagic ?: "0"}"
        if (!TextUtils.isEmpty(user.headImage)) {
            Glide.with(this).load(user.headImage)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(id_img_head)
        } else {
            id_img_head.setImageResource(R.drawable.icon_user_photo_default)
        }
    }

    override fun setResult(isRefresh: Boolean, result: List<MagicCoinLogEntity>?) {
        if (isRefresh) {
            mAdapter?.clear()
        }
        id_refresh_layout?.isRefreshing = false
        val start = mAdapter?.getItemCount() ?: 0
        mAdapter?.appendToList(result)
        if (isRefresh) {
            mAdapter?.notifyDataSetChanged()
        } else {
            val end = mAdapter?.getItemCount() ?: 0
            mAdapter?.notifyItemRangeChanged(start, end)
        }
    }
}