package com.cheng.blzb.ui.fragment.mine.order.list

import android.text.TextUtils
import androidx.core.graphics.toColorInt
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cheng.blzb.R
import com.cheng.blzb.bean.OrderEntity
import com.cheng.blzb.manager.UserConfigManager
import com.cheng.blzb.utils.DateUtils
import com.cheng.blzb.widget.CommonShapeView
import java.text.DecimalFormat

class OrderAdapter : BaseQuickAdapter<OrderEntity, BaseViewHolder>(R.layout.listitem_order) {

    init {
        addChildClickViewIds(R.id.btn_invoice, R.id.btn_certificate)
    }

    override fun convert(holder: BaseViewHolder, item: OrderEntity) {
        holder.setText(R.id.tv_name, item.goods_name)
        holder.setText(R.id.tv_order_no, item.out_trade_no)
        holder.setText(
            R.id.tv_pay_type,
            when (item.pay_type) {
                "weixin" -> "微信支付"
                "alipay" -> "支付宝支付"
                "apple" -> "苹果支付"
                "bank" -> "对公转账"
                else -> "未知"
            }
        )
        holder.setText(R.id.tv_amount, "￥${DecimalFormat("0.00").format(item.total_fee.toFloat())}")
        holder.setText(R.id.tv_create_time, item.create_time)
        holder.setText(R.id.tv_pay_time, item.pay_time)

        holder.setGone(R.id.tv_pay_time_title, TextUtils.isEmpty(item.pay_time))
        holder.setGone(R.id.tv_pay_time, TextUtils.isEmpty(item.pay_time))

        val status = holder.getView<CommonShapeView>(R.id.tv_status)
        holder.setGone(R.id.btn_invoice, true)
        holder.setGone(R.id.btn_certificate, true)
        when (item.status) {
            "1" -> {
                status.text = "待支付"
                status.setTextColor("#FF4C4C".toColorInt())
                status.setBgColor("#FFEDED".toColorInt())
                if (item.pay_type == "bank") {
                    holder.setVisible(R.id.btn_certificate, true)
                }
            }

            "2" -> {
                when (item.invoice_status) {
                    "1" -> { //1：未开票 2：开票中 3：催开票4：开票完成
                        status.text = "已支付"
                        status.setTextColor("#2090FE".toColorInt())
                        status.setBgColor("#E8F3FE".toColorInt())

                        holder.setText(R.id.btn_invoice, "开具发票")
                        holder.setVisible(R.id.btn_invoice, true)
                    }
                    "2" -> {
                        status.text = "开票中"
                        status.setTextColor("#FF9B2F".toColorInt())
                        status.setBgColor("#1AFF9B2F".toColorInt())

                        val intervalMinutes = UserConfigManager.getInvoiceIntervalTime()
                        val intervalTime = if (!TextUtils.isEmpty(intervalMinutes)) {
                            intervalMinutes!!.toInt() * 60 * 1000L
                        } else {
                            3 * 24 * 60 * 60 * 1000L
                        }
                        val payTime = DateUtils.strDateToLong(item.pay_time, "yyyy-MM-dd HH:mm:ss")
                        if (intervalTime + payTime <= System.currentTimeMillis()) {
                            holder.setText(R.id.btn_invoice, "加急开票")
                            holder.setVisible(R.id.btn_invoice, true)
                        }
                    }
                    "3" -> {
                        status.text = "开票中"
                        status.setTextColor("#FF9B2F".toColorInt())
                        status.setBgColor("#1AFF9B2F".toColorInt())
                    }
                    "4" -> {
                        status.text = "已开票"
                        status.setTextColor("#125FFE".toColorInt())
                        status.setBgColor("#E7EFFE".toColorInt())

                        holder.setText(R.id.btn_invoice, "查看发票")
                        holder.setVisible(R.id.btn_invoice, true)
                    }
                }
            }

            "3" -> {
                status.text = "已退款"
                status.setTextColor("#CCCCCC".toColorInt())
                status.setBgColor("#EEEEEE".toColorInt())
            }

            "4" -> {
                status.text = "已取消"
                status.setTextColor("#CCCCCC".toColorInt())
                status.setBgColor("#EEEEEE".toColorInt())
            }

            "5" -> {
                status.text = "待审核"
                status.setTextColor("#FF9B2F".toColorInt())
                status.setBgColor("#1AFF9B2F".toColorInt())
            }
        }
    }
}