package com.yidian.shenghuoquan.buserorder.viewmodel

import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.yidian.bcommon.mvvm.BaseViewModel
import com.yidian.bcommon.utils.CPointUtils
import com.yidian.shenghuoquan.buserorder.app.bean.OrderWebData
import com.yidian.shenghuoquan.buserorder.http.UserOrderListOperation
import com.yidian.shenghuoquan.buserorder.http.callback.OrderListCallBack
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber

class OrderListViewModel : BaseViewModel(){
    companion object {
      const val NORMAL = "NORMAL"
      const val REFUND = "REFUND"
      const val PULL = "PULL"
      const val LOAD = "LOAD"
    }

    private var orderList:MutableLiveData<List<OrderWebData>> = MutableLiveData<List<OrderWebData>>()
    var _orderList:LiveData<List<OrderWebData>> = orderList
    var _listType:String = NORMAL
    var _operationType = PULL
    fun requestFreshUserOrder(param:HashMap<String, String?>) {
      _operationType = PULL
      UserOrderListOperation.userOrderList(generateRereshParma(param), orderListCallBack)
    }
  fun requestLoadMoreUserOrder(param:HashMap<String, String?>) {
    _operationType = LOAD
    UserOrderListOperation.userOrderList(generateLoadmoreParma(param), orderListCallBack)
  }
    private var orderListCallBack = object : OrderListCallBack {
      override fun onOrderListCallback(orders: List<OrderWebData>?) {
        if (orders != null) {
          orderList.value = orders!!
          scanBackPoitReport()
        }
      }
    }

  private fun generateLoadmoreParma(param:HashMap<String, String?>):HashMap<String, String?> {
    param["business_from"] = "2"
    if (_listType == REFUND) {
      param["order_status"] = "5"
    }
    param["num"] = "${_orderList.value!!.size + 10}"
    return param
  }
  private fun generateRereshParma(param:HashMap<String, String?>):HashMap<String, String?> {
    param["business_from"] = "2"
    if (_listType == REFUND) {
      param["order_status"] = "5"
    }
    param["num"] = "10"
    return param
  }

  fun scanBackPoitReport() {
    Timber.tag("OrderBackPoint").i("scanBackPoitReport")
    orderList.value!!.forEach {
      val jsonObject = JSONObject()
      jsonObject.put("eventName", "EXPOSE_PAGE")
      val propertiesJsonObject = JSONObject()
      propertiesJsonObject.put("event_id", "SHQNG_R_1628753368926")
      jsonObject.put("propertiesMap", propertiesJsonObject)
      val contextObj = JSONObject()
      contextObj.put("page","PARTICIPATE_ORDER_PAGE")
      val smallOrderInfo = JSONObject()
      smallOrderInfo.put("order_id", it.order_id)
      smallOrderInfo.put("goods_sku_id", getAllGoodsId(it))
      smallOrderInfo.put("goods_sku_name",getAllGoodsName(it))
      smallOrderInfo.put("marketing_id", it.marketing_id)
      smallOrderInfo.put("marketing_name", it.marketing_name)
      smallOrderInfo.put("marketing_type", "SPELL_MEAL")
      contextObj.put("small_order_info", smallOrderInfo)
      jsonObject.put("contextMap", contextObj)
      jsonObject.put("logType", 1)
      CPointUtils.performCommitLog(jsonObject)
    }
  }

  private fun getAllGoodsId(data: OrderWebData) : String {
    var ids = ""
    data.order_item_list!!.forEach {
      ids = "$ids,${it.goods_sku_id}"
    }
    return ids
  }
  private fun getAllGoodsName(data: OrderWebData) : String {
    var name = ""
    data.order_item_list!!.forEach {
      name = "$name,${it.goods_name}"
    }
    return name
  }

  fun goBackPointReport() {
    Timber.tag("OrderBackPoint").i("goBackPointReport")
    var jsonObject = JSONObject()
    jsonObject.put("eventName", "CLICK_WIDGET")
    var propertiesJsonObject = JSONObject()
    propertiesJsonObject.put("event_id", "SHQNG_R_1628753463516")
    jsonObject.put("propertiesMap", propertiesJsonObject)
    var entitiesJsonObject = JSONObject()
    val widgetInfoJsonObject = JSONObject()
    widgetInfoJsonObject.put("widget_type", "BUTTON")
    widgetInfoJsonObject.put("widget_name", "return_key")
    entitiesJsonObject.put("widget_info", widgetInfoJsonObject)
    jsonObject.put("entitiesMap", entitiesJsonObject)
    jsonObject.put("logType", 1)
    CPointUtils.performCommitLog(jsonObject)
  }

}
