package com.yidian.shenghuoquan.buserorder.ui

import android.graphics.Typeface
import android.graphics.drawable.Drawable
import androidx.activity.viewModels
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.mvvm.BaseMvvmActivity
import com.yidian.news.ui.SystemBarUtils
import com.yidian.shenghuoquan.buserorder.R
import com.yidian.shenghuoquan.buserorder.app.adapter.OrderListAdapter
import com.yidian.shenghuoquan.buserorder.utils.SpaceItemDecoration
import com.yidian.shenghuoquan.buserorder.databinding.BorderActivityOrderListBinding
import com.yidian.shenghuoquan.buserorder.viewmodel.OrderListViewModel
import com.yidian.xpage.XPageViewProtocol
import timber.log.Timber


class OrderListActivity : BaseMvvmActivity<BorderActivityOrderListBinding, OrderListViewModel>() ,XPageViewProtocol, OnRefreshListener{

  override val layoutId: Int = R.layout.border_activity_order_list

  override val vm: OrderListViewModel by viewModels()

  private lateinit var listAdapter: OrderListAdapter

  override fun initView() {
    initTitleBar(binding.orderListToolbar, binding.tvTitle, "参与的")
    SystemBarUtils.setStatusBarTextColorWhite(this)
    binding.orderListToolbar.setNavigationIcon(R.drawable.icon_back)
    initRecycleView()
    initClickEvent()
  }

  private fun initRecycleView() {
    val spacingInPixels = resources.getDimensionPixelSize(R.dimen.dp10)
    binding.rcvOrderList.addItemDecoration(SpaceItemDecoration(spacingInPixels))
    vm._orderList.observe(this, {
      Timber.tag("OrderListObsver").i(">>>>>>>>>>>>>>>>>>>>>>>>")
      listAdapter = OrderListAdapter()
      binding.rcvOrderList.adapter = listAdapter
      listAdapter.setList(it)
    })
  }

  private fun initClickEvent() {
      binding.smartRefresh.setEnableLoadMore(false)
      binding.smartRefresh.setOnRefreshListener(this)
      binding.smartRefresh.autoRefresh()
      binding.tvRefunded.setOnClickListener {
        binding.tvRefunded.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
        val dra: Drawable? = getDrawable(R.drawable.icon_react_under_text)
        dra!!.setBounds(0, 0, dra!!.intrinsicWidth, dra!!.intrinsicHeight)
        binding.tvRefunded.setCompoundDrawables(null, null, null, dra)
        binding.tvOrderlistAll.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL))
        binding.tvOrderlistAll.setCompoundDrawables(null, null, null, null)
        var param = HashMap<String, String?>()
        param["order_status"] = "4"
        vm.requestUserOrder(param)
      }
      binding.tvOrderlistAll.setOnClickListener {
        binding.tvRefunded.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL))
        binding.tvRefunded.setCompoundDrawables(null, null, null, null)
        binding.tvOrderlistAll.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
        val dra: Drawable? = getDrawable(R.drawable.icon_react_under_text)
        dra!!.setBounds(0, 0, dra!!.intrinsicWidth, dra!!.intrinsicHeight)
        binding.tvOrderlistAll.setCompoundDrawables(null, null, null, dra)
        vm.requestUserOrder(HashMap<String, String?>())
      }
  }

  private fun requestUserOrder() {

  }

  override fun getXPageName(): String {
    return XRouterPathConstants.ORDER_LIST_ACTIVITY
  }

  override fun onRefresh(refreshLayout: RefreshLayout) {
    var param = HashMap<String, String?>()
    param["business_from"] = "2"
    vm.requestUserOrder(param)
    binding.smartRefresh.finishRefresh()
  }

}
