package com.duben.microtribe.ui.activitys

import android.os.Bundle
import android.view.View
import com.duben.microtribe.R
import com.duben.microtribe.manager.AppHttpManager
import com.duben.microtribe.mvp.model.BaseResponse
import com.duben.microtribe.mvp.model.OrderRecordBean
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.adapter.OrderRecordAdapter
import com.duben.microtribe.ui.adapter.base.OnItemClickListener
import com.duben.microtribe.utils.UcropUtils
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_order_record.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 * 描述：订单记录
 * 作者：孟崔广
 * 邮箱：mengcga@163.com
 */
class OrderRecordActivity : BaseActivity(), View.OnClickListener, OnRefreshListener {

    private var orderRecordAdapter: OrderRecordAdapter? = null

    private var mAgreementDTO: OrderRecordBean.AgreementDTO? = null

    private var mOrderData: MutableList<OrderRecordBean.RecordsBean> = mutableListOf()

    override fun getContentViewLayoutID() = R.layout.activity_order_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)
        tv_right.visibility = View.VISIBLE
        tv_right.setOnClickListener(this)
        tv_right.text = "续费管理"

        srl_drawcash.setOnRefreshListener(this)

        initRecy()

        getData()
    }

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

                    override fun onNext(t: BaseResponse<OrderRecordBean>) {
                        if (t.status == 200) {
                            srl_drawcash.finishRefresh(true)
                            mAgreementDTO = t.data.agreement
                            if (mAgreementDTO == null) {
                                tv_right.visibility = View.GONE
                            }

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

                })
    }

    private fun initRecy() {
        orderRecordAdapter = OrderRecordAdapter(this, mOrderData)
        recy_drawcash.adapter = orderRecordAdapter
        orderRecordAdapter!!.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(view: View?, position: Int) {
                if (mOrderData != null && mOrderData.size > 0) {
                    showToast("订单号已复制到剪切板!")

                    UcropUtils.clipData(mOrderData.get(position).orderid)
                }
            }
        })
    }

    override fun isApplyKitKatTranslucency() = false

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_left_icon -> finish()
            R.id.tv_right -> {
                if (mAgreementDTO == null) {
                    showToast("暂未开通自动续费")
                } else {
                    val bundle = Bundle()
                    bundle.putSerializable(ManageOrderActivity.AGREEMENT_DATA, mAgreementDTO)
                    readyGo(ManageOrderActivity::class.java, bundle)
                }
            }
        }
    }

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