package com.mints.goodmoney.ui.activitys

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import com.mints.goodmoney.R
import com.mints.goodmoney.manager.AppHttpManager
import com.mints.goodmoney.mvp.model.BaseResponse
import com.mints.goodmoney.mvp.model.GoldRecordBean
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.mints.goodmoney.ui.adapter.CoinRecordAdapter
import com.mints.library.net.neterror.BaseSubscriber
import com.mints.library.net.neterror.Throwable
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_drawcash_record.*
import kotlinx.android.synthetic.main.header_layout.*

class CoinRecordActivity : BaseActivity(), View.OnClickListener, OnRefreshListener {

    private var coinRecordAdapter: CoinRecordAdapter? = null

    private var mCoinData: MutableList<GoldRecordBean.ListBean> = mutableListOf()

    override fun getContentViewLayoutID() = R.layout.activity_drawcash_record

    override fun initViewsAndEvents() {
        tv_title.text = "金币明细"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
        iv_left_icon.setOnClickListener(this)
        srl_drawcash.setOnRefreshListener(this)

        initRecy()

        getData()
    }

    private fun getData() {
        AppHttpManager.getInstance(baseApplication)
                .call(baseApplication.loanService.coinRecords, object : BaseSubscriber<BaseResponse<GoldRecordBean>>() {
                    override fun onError(e: Throwable) {
                        srl_drawcash.finishRefresh(false)
                    }

                    override fun onNext(t: BaseResponse<GoldRecordBean>) {

                        if (t.status == 200) {
                            srl_drawcash.finishRefresh(true)

                            mCoinData.clear()
                            mCoinData.addAll(t.data.list)
                            coinRecordAdapter?.notifyDataSetChanged()
                        } else {
                            srl_drawcash.finishRefresh(false)
                        }
                    }

                })
    }

    private fun initRecy() {
//        recy_drawcash.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL))
        coinRecordAdapter = CoinRecordAdapter(this, mCoinData)
        recy_drawcash.adapter = coinRecordAdapter
    }

    override fun isApplyKitKatTranslucency() = false

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_left_icon -> finish()
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getData()
    }
}