package com.yidian.shenghuoquan.buserorder.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.yidian.bcommon.mvvm.BaseViewModel
import com.yidian.bcommon.mvvm.ErrorInfo
import com.yidian.bcommon.utils.ToastUtils
import com.yidian.shenghuoquan.buserorder.dto.*
import com.yidian.shenghuoquan.buserorder.http.OrderRemoteDataSource
import org.json.JSONArray
import org.json.JSONObject


class ConfirmOrderViewModel : BaseViewModel() {

  private var payOrderId: String? = null
  private lateinit var currentParamsJson: JSONObject// 当前订单的请求参数
  private var originalAmount: Double = 0.00// 原价总和
  var couponAmount: Double = 0.00// 优惠券金额
  var couponAmountValue = MutableLiveData(0.00)
  var couponNameValue = MutableLiveData("红包/优惠券")

  var selectedPickUpPoint = MutableLiveData<Shipping?>()// 自提点
  var receivingName = MutableLiveData<String>()// 收货人姓名
  var receivingPhone = MutableLiveData<String>()//收货人电话
  var marketingName = MutableLiveData<String>()//活动名称
  var selectedCouponList = MutableLiveData<MutableList<CouponDto>?>()// 选中的优惠券
  var couponsAvailableList = MutableLiveData<List<CouponDto>?>()// 可用优惠券列表
  var totalAmount = MutableLiveData(0.00)// 合计金额 = 原价 - 优惠
  var remark = MutableLiveData<String>()

  /**
   * 商品列表成功 Event
   */
  private val _goodsSuccessEvent = MutableLiveData<List<GoodsDto>?>()
  val goodsSuccessEvent: LiveData<List<GoodsDto>?> = _goodsSuccessEvent

  /**
   * 选择自提点 Event
   */
  private val _choosePointEvent = MutableLiveData<Long?>()
  val choosePointEvent: LiveData<Long?> = _choosePointEvent

  /**
   * 选择优惠券 Event
   */
  private val _chooseCouponEvent = MutableLiveData<List<CouponDto>?>()
  val chooseCouponEvent: LiveData<List<CouponDto>?> = _chooseCouponEvent

  /**
   * 选择自提点 Event
   */
  private val _toOrderDetailEvent = MutableLiveData<String?>()
  val toOrderDetailEvent: LiveData<String?> = _toOrderDetailEvent

  /**
   * 调起支付 Event
   */
  private val _toPaymentEvent = MutableLiveData<String>()
  val toPaymentEvent: LiveData<String> = _toPaymentEvent

  /**
   * 请求订单信息
   */
  fun requestConfirmOrderInfo(paramsJson: JSONObject) {
    OrderRemoteDataSource.requestOrderConfirm(paramsJson) {
      if (it == null) {
        return@requestOrderConfirm
      }
      currentParamsJson = paramsJson
      selectedPickUpPoint.value = it.shipping
      receivingName.value = it.shipping?.receiverName ?: ""
      receivingPhone.value = it.shipping?.receiverMobile ?: ""
      marketingName.value = it.marketingInfo?.marketingName ?: ""
      _goodsSuccessEvent.value = it.goodsList
      calculateOriginalAmount(it.goodsList)
      couponsAvailableList.value = it.couponList
    }
  }

  /**
   * 计算商品原价 = 数量 * 单价
   */
  private fun calculateOriginalAmount(goodsDto: List<GoodsDto>?) {
    if (goodsDto.isNullOrEmpty()) {
      return
    }
    goodsDto.forEach {
      originalAmount = originalAmount.plus(it.price * it.goodsNum)
    }
    // 计算出合计
    calculateTotalAmount()
  }

  /**
   * 计算商品合计 = 原价 - 优惠
   */
  private fun calculateTotalAmount() {
    var total = originalAmount - couponAmount
    if (total < 0) {
      total = 0.00
    }
    totalAmount.value = total
  }

  /**
   * 选择自提点，如果有上次选中的带过去自提点id
   */
  fun choosePickUpPoint() {
    // 带过去当前选中自提点id
    _choosePointEvent.value = selectedPickUpPoint.value?.takePlaceId
  }

  fun choosePickUpPointSuccess(point: PickupPoint) {
    selectedPickUpPoint.value = Shipping(
      takePlaceId = point.takePlaceId,
      takePlaceName = point.takePlaceName,
      contactName = point.contactName,
      phone = point.phone,
      address = point.address
    )
  }

  /**
   * 选择代金券, 如果有上次选中的带过去最新的列表
   */
  fun chooseVoucher() {
    //选择之前先将上次的金额清零
    couponAmount = 0.00
    _chooseCouponEvent.value = couponsAvailableList.value
  }

  /**
   * 选择的优惠券金额已支持多选，⚠️ ：名称和UI暂不支持
   */
  fun chooseVoucherSuccess(indexList: List<Int>) {
    // 获取选中的优惠券
    val list = mutableListOf<CouponDto>()
    indexList.forEach {
      val coupon = couponsAvailableList.value?.get(it)
      coupon?.let { couponDto ->
        // 累加优惠券金额
        couponAmount += couponDto.amount?.toDouble() ?: 0.00
        list.add(couponDto)
      }
    }
    selectedCouponList.value = list
    // 更新优惠券总金额
    couponAmountValue.value = couponAmount
    // 更新选择优惠券名称
    couponNameValue.value = couponsAvailableList.value?.get(0)?.name
    // 重新计算相关金额
    calculateTotalAmount()
  }

  /**
   * 创建订单
   */
  fun createOrder() {
    if (selectedPickUpPoint.value?.takePlaceId == null) {
      postErrorEvent(ErrorInfo(message = "请选择自提点"))
      return
    }
    if (receivingName.value.isNullOrBlank()) {
      postErrorEvent(ErrorInfo(message = "请输入收货姓名"))
      return
    }
    if (receivingPhone.value.isNullOrBlank()) {
      postErrorEvent(ErrorInfo(message = "请输入手机号码"))
      return
    }
    if (receivingPhone.value?.length != 11) {
      postErrorEvent(ErrorInfo(message = "请输入11位手机号码"))
      return
    }
    if (!selectedCouponList.value.isNullOrEmpty()) {// 添加上选中的优惠券
      currentParamsJson.put("coupon_list", JSONArray(selectedCouponList.value))
    }
    currentParamsJson.put("take_place_id", selectedPickUpPoint.value?.takePlaceId)
    currentParamsJson.put("receiver_name", receivingName.value)
    currentParamsJson.put("receiver_mobile", receivingPhone.value)
    currentParamsJson.put("comment", remark.value)
    OrderRemoteDataSource.requestCreateOrder(currentParamsJson) {
      it?.let {
        ToastUtils.showShort("创建订单成功${it.orderId}")
        if (it.payment == 0) {// 0元单不用支付直接进订单详情
          _toOrderDetailEvent.value = it.orderId
        } else {
          toGetCharge(it.orderId)
        }
      }
    }
  }

  /**
   * 获取charge并进行支付
   */
  private fun toGetCharge(orderId: String?) {
    payOrderId = orderId
    val requestParams = HashMap<String, String?>()
    requestParams["order_id"] = orderId
    requestParams["pay_method_id"] = "105"// 微信 App 支付
    OrderRemoteDataSource.requestPayOrder(requestParams) {
      it?.let {
        ToastUtils.showShortSafe("获取charge成功：${it.size}")
        // 调起支付
        _toPaymentEvent.value = Gson().toJson(it[0].v?.charges)
      }
    }
  }

  /**
   * 支付结果
   */
  fun paymentSuccess() {
    _toOrderDetailEvent.value = payOrderId
  }


}
