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.DrawcashRecordBean
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.mints.goodmoney.ui.adapter.DrawcashRecordAdapter
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 DrawcashRecordActivity : BaseActivity(), View.OnClickListener, OnRefreshListener {

    private var drawcashRecordAdapter: DrawcashRecordAdapter? = null

    private var mDrawcashData: MutableList<DrawcashRecordBean.RecordsBean> = 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.cashOutRecords, object : BaseSubscriber<BaseResponse<DrawcashRecordBean>>() {
                    override fun onError(e: Throwable) {
                        srl_drawcash.finishRefresh(false)
                    }

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

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

                            mDrawcashData.clear()
                            mDrawcashData.addAll(t.data.records)
                            drawcashRecordAdapter?.notifyDataSetChanged()
                        } else {
                            srl_drawcash.finishRefresh(false)
                        }
                    }

                })
    }

    private fun initRecy() {
        recy_drawcash.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL))
        drawcashRecordAdapter = DrawcashRecordAdapter(this, mDrawcashData)
        recy_drawcash.adapter = drawcashRecordAdapter
    }

    override fun isApplyKitKatTranslucency() = false

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

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