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.OnLoadMoreListener
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.shenghuoquan.buserorder.viewmodel.OrderListViewModel.Companion.LOAD
import com.yidian.shenghuoquan.buserorder.viewmodel.OrderListViewModel.Companion.PULL
import com.yidian.xpage.XPageViewProtocol
import timber.log.Timber


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

  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.setStatusBarTextColorBlack(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(">>>>>>>>>>>>>>>>>>>>>>>>")
      if (binding.smartRefresh.isRefreshing) {
        binding.smartRefresh.finishRefresh()
      }
      if (binding.smartRefresh.isLoading) {
        binding.smartRefresh.finishLoadMore()
      }
      listAdapter = OrderListAdapter()
      binding.rcvOrderList.adapter = listAdapter
      listAdapter.setList(it)
      var oldPos = listAdapter.itemCount
      setRecyclePos(oldPos)
    })
  }

  private fun initClickEvent() {
      binding.smartRefresh.setOnRefreshListener(this)
      binding.smartRefresh.setOnLoadMoreListener(this)
      binding.smartRefresh.autoRefresh()
      binding.tvRefunded.setOnClickListener {
        vm._listType = OrderListViewModel.REFUND
        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)
        vm.requestFreshUserOrder(HashMap<String, String?>())
      }
      binding.tvOrderlistAll.setOnClickListener {
        vm._listType = OrderListViewModel.NORMAL
        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.requestFreshUserOrder(HashMap<String, String?>())
      }
  }

  private fun setRecyclePos(pos:Int) {
      when(vm._operationType) {
        PULL -> {
          binding.rcvOrderList.smoothScrollToPosition(1)
        }
        LOAD -> {
          binding.rcvOrderList.smoothScrollToPosition(pos)
        }

      }
  }

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

  override fun onRefresh(refreshLayout: RefreshLayout) {
    vm.requestFreshUserOrder(HashMap<String, String?>())
  }

  override fun onLoadMore(refreshLayout: RefreshLayout) {
    vm.requestLoadMoreUserOrder(HashMap<String, String?>())
  }

}
