package com.yidian.shenghuoquan.buserorder.viewmodel

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.dto.OrderDetailDto
import com.yidian.shenghuoquan.buserorder.http.OrderRemoteDataSource
import com.yidian.shenghuoquan.buserorder.http.callback.IGetOrderDetailCallback
import org.json.JSONObject
import timber.log.Timber

/**
 * author: yinjiacheng
 * date: 2021/8/21 09:31
 * description: 订单详情页
 */
class OrderDetailViewModel : BaseViewModel() {

  private val _orderDetailData = MutableLiveData<OrderDetailDto?>()
  val orderDetailData: LiveData<OrderDetailDto?> = _orderDetailData

  /**
   * 请求订单详情
   */
  fun requestOrderDetail(orderId: String) {
    OrderRemoteDataSource.getOrderDetail(object : IGetOrderDetailCallback {
      override fun getOrderDetailSuccess(result: OrderDetailDto?) {
        if (_orderDetailData.value == null) {
          _orderDetailData.value = result
          // 订单详情页面曝光埋点只上报一次
          commitPageBuriedPoint()
        }
        _orderDetailData.value = result
      }

      override fun getOrderDetailFailure(message: String?) {
        _orderDetailData.value = null
      }
    }, hashMapOf("order_id" to orderId, "business_from" to "2"))
  }

  /**
   * 页面浏览的埋点
   * 页面可见且有业务数据时上传
   */
  private fun commitPageBuriedPoint() {
    val data = _orderDetailData.value
    // 遍历商品列表
    val skuIdStr = StringBuilder()
    val skuNameStr = StringBuilder()
    data?.mergeOrderItemList?.forEachIndexed { index, item ->
      skuIdStr.append(item.goodsSkuId)
      skuNameStr.append(item.goodsName)
      if (index < data.mergeOrderItemList.size - 1) {
        skuIdStr.append(",")
        skuNameStr.append(",")
      }
    }
    // 埋点数据包装
    val jsonObject = JSONObject()
    jsonObject.put("eventName", "EXPOSE_PAGE")
    val contextJsonObject = JSONObject()
    contextJsonObject.put("page", "ORDER_DETAIL_PAGE")
    val smallOrderInfoJsonObject = JSONObject()
    smallOrderInfoJsonObject.put("order_id", data?.orderId)
    smallOrderInfoJsonObject.put("marketing_id", data?.marketingId)
    smallOrderInfoJsonObject.put("marketing_name", data?.marketingName)
    smallOrderInfoJsonObject.put("marketing_type", "SPELL_MEAL")
    smallOrderInfoJsonObject.put("goods_sku_id", skuIdStr.toString())
    smallOrderInfoJsonObject.put("goods_sku_name", skuNameStr.toString())
    contextJsonObject.put("small_order_info", smallOrderInfoJsonObject)
    jsonObject.put("contextMap", contextJsonObject)
    val propertiesJsonObject = JSONObject()
    propertiesJsonObject.put("event_id", "SHQNG_R_1628750600250")
    jsonObject.put("propertiesMap", propertiesJsonObject)
    jsonObject.put("logType", 1)
    CPointUtils.performCommitLog(jsonObject)
    Timber.tag("BUserOrderBuriedPoint").d("commit order detail page log: $jsonObject")
  }

  /**
   * 点击返回按钮的埋点
   */
  fun commitWidgetBuriedPoint(widgetName: String) {
    val data = _orderDetailData.value
    // 遍历商品列表
    val skuIdStr = StringBuilder()
    val skuNameStr = StringBuilder()
    data?.mergeOrderItemList?.forEachIndexed { index, item ->
      skuIdStr.append(item.goodsSkuId)
      skuNameStr.append(item.goodsName)
      if (index < data.mergeOrderItemList.size - 1) {
        skuIdStr.append(",")
        skuNameStr.append(",")
      }
    }
    // 埋点数据包装
    val jsonObject = JSONObject()
    jsonObject.put("eventName", "CLICK_WIDGET")
    val contextJsonObject = JSONObject()
    contextJsonObject.put("page", "ORDER_DETAIL_PAGE")
    val smallOrderInfoJsonObject = JSONObject()
    smallOrderInfoJsonObject.put("order_id", data?.orderId)
    smallOrderInfoJsonObject.put("marketing_id", data?.marketingId)
    smallOrderInfoJsonObject.put("marketing_name", data?.marketingName)
    smallOrderInfoJsonObject.put("marketing_type", "SPELL_MEAL")
    smallOrderInfoJsonObject.put("goods_sku_id", skuIdStr.toString())
    smallOrderInfoJsonObject.put("goods_sku_name", skuNameStr.toString())
    contextJsonObject.put("small_order_info", smallOrderInfoJsonObject)
    jsonObject.put("contextMap", contextJsonObject)
    val entitiesJsonObject = JSONObject()
    val widgetInfoJsonObject = JSONObject()
    widgetInfoJsonObject.put("widget_type", "BUTTON")
    widgetInfoJsonObject.put("widget_name", widgetName)
    entitiesJsonObject.put("widget_info", widgetInfoJsonObject)
    jsonObject.put("entitiesMap", entitiesJsonObject)
    val propertiesJsonObject = JSONObject()
    propertiesJsonObject.put("event_id", "SHQNG_R_1628751452736")
    jsonObject.put("propertiesMap", propertiesJsonObject)
    jsonObject.put("logType", 1)
    CPointUtils.performCommitLog(jsonObject)
    Timber.tag("BUserOrderBuriedPoint").d("commit order detail widget log: $jsonObject")
  }
}
