package com.yidian.shenghuoquan.buserorder.app.adapter

import android.graphics.Typeface
import android.net.Uri
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yidian.bcommon.constant.IntentConstants
import com.yidian.bcommon.constant.XBirdPageConstants
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.utils.CPointUtils
import com.yidian.shenghuoquan.buserorder.R
import com.yidian.shenghuoquan.buserorder.app.bean.OrderWebData
import com.yidian.shenghuoquan.buserorder.utils.Utils
import com.yidian.shenghuoquan.buserorder.view.RoundImageView
import com.yidian.xpage.XPageManager
import org.json.JSONObject
import timber.log.Timber

class OrderListAdapter : BaseQuickAdapter<OrderWebData, BaseViewHolder>(R.layout.border_recycle_order_item){
  companion object{
    const val roundRadus = 40
  }

  override fun convert(holder: BaseViewHolder, item: OrderWebData) {
    mapDataToHoder(item, holder)
  }

  private fun mapDataToHoder(data: OrderWebData, holder: BaseViewHolder) {
    resetImageStatu(holder)
    holder.itemView.findViewById<TextView>(R.id.tv_market_name).setOnClickListener {
      gotoMarketingDetail(data.marketing_id)
    }
    holder.itemView.findViewById<TextView>(R.id.tv_market_name).text = data.marketing_name
    holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = data.online_status
    when(data.order_item_list.size) {
      0 -> {
        holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image1).visibility = View.GONE
        holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image2).visibility = View.GONE
        holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image3).visibility = View.GONE
      }
      1 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_goog1_name).text = data.order_item_list[0].goods_name
        holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image2).visibility = View.GONE
        holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image3).visibility = View.GONE
        if (Utils.isWebUrl(data.order_item_list[0].desc_pic_url)) {
          Glide.with(context).load(Uri.parse(data.order_item_list[0].desc_pic_url)).apply(RequestOptions.bitmapTransform(RoundedCorners(roundRadus))).into(holder.itemView.findViewById(R.id.round_image1))
        }
      }
      2 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_goog1_name).text = data.order_item_list[0].goods_name
        holder.itemView.findViewById<TextView>(R.id.tv_goog2_name).text = data.order_item_list[1].goods_name
        holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image3).visibility = View.GONE
        if (Utils.isWebUrl(data.order_item_list[0].desc_pic_url) && Utils.isWebUrl(data.order_item_list[1].desc_pic_url)) {
          Glide.with(context).load(Uri.parse(data.order_item_list[0].desc_pic_url)).apply(RequestOptions.bitmapTransform(RoundedCorners(roundRadus))).into(holder.itemView.findViewById(R.id.round_image1))
          Glide.with(context).load(Uri.parse( data.order_item_list[1].desc_pic_url)).apply(RequestOptions.bitmapTransform(RoundedCorners(roundRadus))).into(holder.itemView.findViewById(R.id.round_image2))
        }
      }
      else -> {
        holder.itemView.findViewById<TextView>(R.id.tv_goog1_name).text = data.order_item_list[0].goods_name
        holder.itemView.findViewById<TextView>(R.id.tv_goog2_name).text = data.order_item_list[1].goods_name
        holder.itemView.findViewById<TextView>(R.id.tv_good3_name).text = data.order_item_list[2].goods_name
        if (Utils.isWebUrl(data.order_item_list[0].desc_pic_url) && Utils.isWebUrl(data.order_item_list[1].desc_pic_url) && Utils.isWebUrl(data.order_item_list[2].desc_pic_url)) {
          Glide.with(context).load(Uri.parse(data.order_item_list[0].desc_pic_url)).apply(RequestOptions.bitmapTransform(RoundedCorners(roundRadus))).into(holder.itemView.findViewById(R.id.round_image1))
          Glide.with(context).load(Uri.parse(data.order_item_list[1].desc_pic_url)).apply(RequestOptions.bitmapTransform(RoundedCorners(roundRadus))).into(holder.itemView.findViewById(R.id.round_image2))
          Glide.with(context).load(Uri.parse(data.order_item_list[2].desc_pic_url)).apply(RequestOptions.bitmapTransform(RoundedCorners(roundRadus))).into(holder.itemView.findViewById(R.id.round_image3))
        }
      }
    }
    holder.itemView.findViewById<TextView>(R.id.tv_order_goods_count).text = "共${data.goods_num}件"
    holder.itemView.findViewById<TextView>(R.id.tv_pay_time).text = data.pay_time

    holder.itemView.findViewById<RelativeLayout>(R.id.rl_show_order_detail).setOnClickListener {
      //跳转订单详情页面
      var map = HashMap<String, Any?>()
      map[IntentConstants.KEY_EXTRA_ORDER_ID] = data.order_id
      XPageManager.push(XRouterPathConstants.ORDER_DETAIL_ACTIVITY, map)
      detailBackPoint(it, data)
    }
    setOrderStatus(data, holder)
  }
  fun gotoMarketingDetail(marketingId: Int) {
    val pageUrl = XBirdPageConstants.FIGHT_ORDER_EVENT_DETAILS + "?marketing_id=${marketingId}"
    XPageManager.push(XRouterPathConstants.COMMON_WEB_VIEW_ACTIVITY, hashMapOf(Pair(XBirdPageConstants.PAGE_URL, pageUrl)))
  }

  private fun resetImageStatu(holder: BaseViewHolder) {
    holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image1).visibility = View.VISIBLE
    holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image2).visibility = View.VISIBLE
    holder.itemView.findViewById<LinearLayout>(R.id.ll_order_image3).visibility = View.VISIBLE
  }

  private fun setOrderStatus(data: OrderWebData, holder: BaseViewHolder) {
    when(data.order_status) {
      1 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_pay_money_status).text = "未支付:"
        holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = "未支付"
        holder.itemView.findViewById<TextView>(R.id.tv_order_list_yuan).visibility = View.INVISIBLE
        var money:TextView = holder.itemView.findViewById<TextView>(R.id.tv_pay_money_count)
        money.setTypeface(Typeface.DEFAULT_BOLD)
        money.text = ""
      }
      2 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_pay_money_status).text = "已支付:"
        holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = "已支付"
        var money:TextView = holder.itemView.findViewById<TextView>(R.id.tv_pay_money_count)
        money.setTypeface(Typeface.DEFAULT_BOLD)
        money.text = "${data.payment}"
      }
      3 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_pay_money_status).text = "待退款"
        holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = "待退款"
        holder.itemView.findViewById<TextView>(R.id.tv_order_list_yuan).visibility = View.INVISIBLE
        var money:TextView = holder.itemView.findViewById<TextView>(R.id.tv_pay_money_count)
        money.setTypeface(Typeface.DEFAULT_BOLD)
        money.text = ""
      }
      4 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_pay_money_status).text = "已退款"
        holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = "已退款"
        holder.itemView.findViewById<TextView>(R.id.tv_order_list_yuan).visibility = View.INVISIBLE
        var money:TextView = holder.itemView.findViewById<TextView>(R.id.tv_pay_money_count)
        money.setTypeface(Typeface.DEFAULT_BOLD)
        money.text = ""
      }
      5 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_pay_money_status).text = "退款中"
        holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = "退款中"
        holder.itemView.findViewById<TextView>(R.id.tv_order_list_yuan).visibility = View.INVISIBLE
        var money:TextView = holder.itemView.findViewById<TextView>(R.id.tv_pay_money_count)
        money.setTypeface(Typeface.DEFAULT_BOLD)
        money.text = ""
      }
      6 -> {
        holder.itemView.findViewById<TextView>(R.id.tv_pay_money_status).text = "已拒绝"
        holder.itemView.findViewById<TextView>(R.id.tv_pay_status).text = "已拒绝"
        holder.itemView.findViewById<TextView>(R.id.tv_order_list_yuan).visibility = View.INVISIBLE
        var money:TextView = holder.itemView.findViewById<TextView>(R.id.tv_pay_money_count)
        money.setTypeface(Typeface.DEFAULT_BOLD)
        money.text = ""
      }
    }
  }

  private fun detailBackPoint(view: View, data: OrderWebData) {
    Timber.tag("OrderBackPoint").i("detailBackPoint")
    val jsonObject = JSONObject()
    jsonObject.put("eventName", "CLICK_WIDGET")
    val propertiesJsonObject = JSONObject()
    propertiesJsonObject.put("event_id", "SHQNG_R_1628753463516")
    jsonObject.put("propertiesMap", propertiesJsonObject)
    val entitiesJsonObject = JSONObject()
    val widgetInfoJsonObject = JSONObject()
    widgetInfoJsonObject.put("widget_type", "BUTTON")
    widgetInfoJsonObject.put("widget_name", "check_order")
    entitiesJsonObject.put("widget_info", widgetInfoJsonObject)
    jsonObject.put("entitiesMap", entitiesJsonObject)
    val contextObj = JSONObject()
    val smallOrderInfo = JSONObject()
    smallOrderInfo.put("order_id", data.order_id)
    smallOrderInfo.put("marketing_id", data.marketing_id)
    smallOrderInfo.put("marketing_name", data.marketing_name)
    smallOrderInfo.put("market_type", "SPELL_MEAL")
    smallOrderInfo.put("goods_sku_id", getAllGoodsId(data))
    smallOrderInfo.put("goods_sku_name", getAllGoodsName(data))
    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
  }
}
