package com.yidian.shenghuoquan.buserorder.ui

import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.view.View
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.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.bcommon_icon_back)
    initRecycleView()
    initClickEvent()
  }

  private fun initRecycleView() {
    listAdapter = OrderListAdapter()
    binding.rcvOrderList.adapter = listAdapter
    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.setList(it)
      if (listAdapter.itemCount == 0) {
        binding.orderlistEmptyView.visibility = View.VISIBLE
      } else {
        binding.orderlistEmptyView.visibility = View.GONE
      }
    })
  }

  private fun initClickEvent() {
      binding.smartRefresh.setOnRefreshListener(this)
      binding.smartRefresh.setOnLoadMoreListener(this)
      binding.tvRefunded.setOnClickListener {
        if (binding.smartRefresh.isLoading || binding.smartRefresh.isRefreshing || vm._state == OrderListViewModel.REQUESTING) {
          return@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)
        binding.smartRefresh.autoRefresh()
      }
      binding.tvOrderlistAll.setOnClickListener {
        if (binding.smartRefresh.isLoading || binding.smartRefresh.isRefreshing || vm._state == OrderListViewModel.REQUESTING) {
          return@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)
        binding.smartRefresh.autoRefresh()
      }
  }

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

      }
  }

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

  override fun onRefresh(refreshLayout: RefreshLayout) {
    if (vm._state == OrderListViewModel.REQUEST_COMPLETE) {
      vm.requestFreshUserOrder(HashMap<String, String?>())
    }
  }

  override fun onLoadMore(refreshLayout: RefreshLayout) {
    if (vm._state == OrderListViewModel.REQUEST_COMPLETE) {
      vm.requestLoadMoreUserOrder(HashMap<String, String?>())
    }
  }

  override fun onBackPressed() {
    super.onBackPressed()
    vm.goBackPointReport()
  }

  override fun onResume() {
    super.onResume()
    binding.smartRefresh.autoRefresh()
  }

  override fun onPause() {
    super.onPause()
    //重置状态不影响下次请求
    vm._state = OrderListViewModel.REQUEST_COMPLETE
    if (binding.smartRefresh.isRefreshing) {
      binding.smartRefresh.finishRefresh()
    }
    if (binding.smartRefresh.isLoading) {
      binding.smartRefresh.finishLoadMore()
    }
  }
}
