package com.yzx.escreen.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yzx.escreen.R
import com.yzx.escreen.model.LeaveActivity
import com.yzx.escreen.utils.setSrc
import com.yzx.escreen.utils.toTime
import kotlinx.android.synthetic.main.layout_leave_step.view.*
import org.jetbrains.anko.dip
import org.jetbrains.anko.textColor

class HomeDialogStepAdapter(list: MutableList<LeaveActivity>) :
    BaseQuickAdapter<LeaveActivity, BaseViewHolder>(R.layout.layout_leave_step, list) {

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: LeaveActivity) {
        holder.itemView.inImg.visibility = View.GONE
        when (item.is_approve) {
            0 -> {
                holder.itemView.addUserName.visibility = View.VISIBLE
                holder.itemView.addUserName.text = item.add_user_name
                holder.itemView.approvalComments.visibility = View.GONE
            }
            2 -> {
                holder.itemView.addUserName.visibility = View.GONE
                holder.itemView.approvalComments.visibility = View.VISIBLE
                holder.itemView.approvalComments.text = item.approval_comments
                holder.itemView.approvalComments.textColor = if (item.color.isNotEmpty()) {
                    Color.parseColor(item.color)
                } else {
                    Color.parseColor("#333333")
                }

                val inItem = item.getInImg()
                if (inItem != null) {
                    holder.itemView.inImg.visibility = View.VISIBLE
                    holder.itemView.inImg.setSrc(inItem.identity, inItem.attachment_id)
                } else {
                    holder.itemView.inImg.visibility = View.GONE
                }
            }
            else -> {
                holder.itemView.addUserName.visibility = View.GONE
                holder.itemView.approvalComments.visibility = View.VISIBLE
                var str = "审批人：${item.add_user_name}"
                if (item.approval_comments != null && item.approval_comments.isNotEmpty()) {
                    str += "。${item.approval_comments}"
                }
                holder.itemView.approvalComments.text = str
            }
        }
        if (item.add_user_type_name.isNullOrBlank()) {
            holder.itemView.addUserTypeName.visibility = View.GONE
        } else {
            holder.itemView.addUserTypeName.visibility = View.VISIBLE
            holder.itemView.addUserTypeName.text = "（${item.add_user_type_name}）"
        }
        holder.itemView.activityName.text = item.activity_name

        if (item.is_approve == 1 && item.approval_status > 0) {
            holder.itemView.statusName.visibility = View.VISIBLE
            holder.itemView.statusName.text = when (item.approval_status) {
                1 -> "（待审批）"
                2 -> "（通过）"
                3 -> "（拒绝）"
                else -> ""
            }
        } else {
            holder.itemView.statusName.visibility = View.GONE
        }

        if (item?.sign_key != null && item?.sign_key?.isNotEmpty()) {
            holder.itemView.signImg.visibility = View.VISIBLE
            holder.itemView.signImg.setSrc(item.sign_identity, item.sign_key)
        } else {
            holder.itemView.signImg.visibility = View.GONE
        }

        if (item.is_approve == 0 || item.approval_status > 1) {
            holder.itemView.stepTime.visibility = View.VISIBLE
            holder.itemView.stepTime.text = item.add_time.toTime()
        } else {
            holder.itemView.stepTime.visibility = View.GONE
        }

        if (holder.adapterPosition == 0) {
            holder.itemView.line1.setBackgroundResource(R.color.transparent)
        } else {
            holder.itemView.line1.setBackgroundResource(R.color.gray_1)
        }

        if (holder.adapterPosition == data.size - 1) {
            holder.itemView.contentLayout.setPadding(0, 0, 0, 0)
        } else {
            holder.itemView.contentLayout.setPadding(0, 0, 0, holder.itemView.contentLayout.dip(20))
        }
    }
}