package com.adealink.weparty.module.order.data

import android.os.Parcelable
import com.adealink.frame.network.data.PageReq
import com.google.gson.annotations.GsonNullable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

/**
 *
 */
enum class OrderStatus(val status: Int) {
    CREATE_ORDER(0), //创建订单(订单初始状态)
    WAIT_FOR_ACCEPT(1), //等待接单

    PLAYMATE_ACCEPT(4), //接单
    PLAYMATE_IN_SERVICE(6), //服务中
    PLAYMATE_END_SERVICE(7), //服务完成(陪玩触发)

    PLAYMATE_REFUSE(5), //拒绝
    COMPLETE(2), //订单完成
    USER_REFUND(3), //退款
    USER_CANCEL(8); //用户取消订单

    companion object {
        @JvmStatic
        fun map(status: Int?): OrderStatus? {
            status ?: return null
            return entries.find { it.status == status }
        }

        //兜底方案,如果从服务端获取状态状态失败
        @JvmStatic
        fun getNextStatus(current: Int?, action: OrderAction): Int? {
            return getNextStatus(map(current), action)?.status
        }

        @JvmStatic
        fun getNextStatus(current: OrderStatus?, action: OrderAction): OrderStatus? {
            current ?: return null
            return when (current) {
                CREATE_ORDER,
                WAIT_FOR_ACCEPT -> {
                    when (action) {
                        OrderAction.ACCEPT -> {
                            PLAYMATE_ACCEPT
                        }

                        OrderAction.CANCEL -> {
                            USER_CANCEL
                        }

                        OrderAction.REFUSE -> {
                            PLAYMATE_REFUSE
                        }

                        else -> {
                            WAIT_FOR_ACCEPT
                        }
                    }
                }

                PLAYMATE_ACCEPT -> {
                    when (action) {
                        OrderAction.CANCEL -> {
                            USER_CANCEL
                        }

                        OrderAction.START_SERVICE -> {
                            PLAYMATE_IN_SERVICE
                        }

                        OrderAction.REFUND -> {
                            PLAYMATE_ACCEPT
                        }

                        else -> {
                            PLAYMATE_ACCEPT
                        }
                    }
                }


                PLAYMATE_IN_SERVICE -> {
                    when (action) {
                        OrderAction.REFUND -> {
                            PLAYMATE_IN_SERVICE
                        }

                        OrderAction.END_SERVICE -> {
                            PLAYMATE_END_SERVICE
                        }

                        else -> {
                            PLAYMATE_IN_SERVICE
                        }
                    }
                }

                PLAYMATE_END_SERVICE -> {
                    when (action) {
                        OrderAction.REFUND -> {
                            PLAYMATE_END_SERVICE
                        }

                        OrderAction.COMPLETE -> {
                            COMPLETE
                        }

                        else -> {
                            PLAYMATE_END_SERVICE
                        }
                    }
                }

                PLAYMATE_REFUSE -> {
                    PLAYMATE_REFUSE
                }

                USER_CANCEL -> {
                    USER_CANCEL
                }

                USER_REFUND -> {
                    USER_REFUND
                }

                COMPLETE -> {
                    COMPLETE
                }
            }
        }
    }
}

enum class OrderAction(val action: Int) {
    CREATE_ORDER(0),//下单
    ACCEPT(1), //接受订单
    REFUSE(2), //拒绝订单
    START_SERVICE(3), //开始服务
    END_SERVICE(4), //结束服务
    REFUND(5), //退款
    CANCEL(6), //取消
    COMPLETE(7), //完成

    STAR(7), //评价
}

@Parcelize
data class OrderDetailInfo(
    //订单字段
    @SerializedName("orderId") val orderId: String, //订单ID
    @SerializedName("bizCategoryName") val categoryName: String, //品类名称
    @SerializedName("categoryIcon") val categoryIcon: String, //品类图标
    @SerializedName("price") val price: Float, //单价
    @SerializedName("unit") val unit: String, //价格单位
    @SerializedName("purchaseQty") val purchaseQty: Int, //购买数量
    @SerializedName("status") var status: Int, //订单状态
    @SerializedName("createTime") val createTime: Long, //下单时间(时间戳, 毫秒)
    @GsonNullable
    @SerializedName("totalAmount") val totalAmount: Float?, //订单价格
    @GsonNullable
    @SerializedName("customerRemark") val customerRemark: String?, //客户备注
    @GsonNullable
    @SerializedName("star") var star: Float?, //评分

    //订单头像
    @SerializedName("sellerUserNo") val sellerUserNo: String, //卖家用户uid=陪玩师编号
    @SerializedName("buyerUserNo") val buyerUserNo: String, //客户uid
    @SerializedName("avatar") val avatar: String, //头像
    @SerializedName("nickname") val nickname: String, //昵称
    @SerializedName("gender") val gender: Int, //性别

    //退单字段
    @GsonNullable
    @SerializedName("refundApply") val refundApply: Boolean?, //是否退款申请中
) : Parcelable {
    fun isPendingAccept(): Boolean {
        return status == OrderStatus.CREATE_ORDER.status || status == OrderStatus.WAIT_FOR_ACCEPT.status
    }

    fun isComplete(): Boolean {
        return status == OrderStatus.PLAYMATE_REFUSE.status
                || status == OrderStatus.COMPLETE.status
                || status == OrderStatus.USER_REFUND.status
                || status == OrderStatus.USER_CANCEL.status
    }

    fun isInService(): Boolean {
        return status == OrderStatus.PLAYMATE_IN_SERVICE.status
    }

    fun hasEvaluate(): Boolean {
        return star != 0f
    }
}


@Parcelize
data class OrderAttachment(
    @GsonNullable
    @SerializedName("attachmentUrl") val attachmentUrl: String?,
    @GsonNullable
    @SerializedName("type") val type: Int?, //0:图片, 1:视频
    @GsonNullable
    @SerializedName("fileName") val fileName: String?,
) : Parcelable


data class QueryUnCompleteOrderReq(
    @SerializedName("userNo") val uid: String,
    @SerializedName("page") val page: PageReq,
)

data class QueryUnCompleteOrderRes(
    @SerializedName("list") val list: List<OrderDetailInfo>,
    @GsonNullable
    @SerializedName("next") val next: String?,
)

data class CreateOrderRes(
    @SerializedName("orderNo") val orderId: String, //订单编号
    @SerializedName("goldCoinBalance") val balance: Double, //金币余额
)

data class OrderPlaymateOpReq(
    @SerializedName("orderNo") val orderNo: String,
    @SerializedName("type") val type: Int, //1:接单，2:拒绝，3:服务，4:服务完成
) {
    companion object {
        const val OP_ACCEPT = 1
        const val OP_REFUSE = 2
        const val OP_START_SERVICE = 3
        const val OP_END_SERVICE = 4
    }
}

data class OrderDetailReq(
    @SerializedName("id") val orderId: String
)

data class OrderDetailData(
    @SerializedName("orderInfo") val order: OrderDetailInfo,
    @GsonNullable
    @SerializedName("reason") val refundReason: String?, //退款原因
    @GsonNullable
    @SerializedName("attachments") val refundAttachments: List<OrderAttachment>?, //退款附件
    @GsonNullable
    @SerializedName("playmateReason") val playmateReason: String?, //陪玩师凭证理由
    @GsonNullable
    @SerializedName("playmateAttachments") val playmateAttachments: List<OrderAttachment>?, //陪玩师凭证理由附件
)

data class RefundApplyReq(
    @SerializedName("orderId") val orderId: String, //订单ID
    @SerializedName("reason") val reason: String?,
    @SerializedName("attachments") val attachments: List<OrderAttachment>?,
)

data class PlaymateSubmitEvidenceReq(
    @SerializedName("orderId") val orderId: String,
    @GsonNullable
    @SerializedName("reason") val reason: String?,
    @GsonNullable
    @SerializedName("attachments") val attachments: List<OrderAttachment>?
)

enum class QRCodeOrder(val type: Int) {
    COMMON_QRCODE(1),
    SPECIAL_QRCODE(2);

    companion object {
        @JvmStatic
        fun map(type: Int?): QRCodeOrder? {
            return entries.find { it.type == type }
        }
    }
}

data class QRCodeOrderDetailReq(
    @SerializedName("id") val qrCode: String,
)

data class QRCodeOrderDetail(
    @SerializedName("qrCode") val qrCode: String,
    @SerializedName("sellerUserNo") val uid: String,
    @SerializedName("avatar") val avatar: String,
    @SerializedName("nickname") val nickname: String,
    @SerializedName("categoryIcon") val categoryIcon: String,
    @SerializedName("bizCategoryName") val categoryName: String,
    @SerializedName("price") val price: Float,
    @SerializedName("unit") val unit: String,
    @SerializedName("purchaseQty") val purchaseQty: Int,
    @SerializedName("type") val type: Int,
    @SerializedName("star") val star: Float,
)

data class QRCodeOrderPayReq(
    @SerializedName("qrCode") val qrCode: String,
    @SerializedName("remark") val remark: String?,
    @SerializedName("purchaseQty") val purchaseQty: Int?, //通用码订单里传
)