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.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

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)
    }
    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 = ""
      }

    }
  }
}
