package com.lukouguoji.gnj.adapt

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.gnj.R
import com.lukouguoji.gnj.model.GnjWareHouseList
import com.lukouguoji.module_base.router.ARouterConstants

/**
 * 仓库管理列表
 */
class GnjWareHouseListAdapter(val activity: Activity, private val collectList: List<GnjWareHouseList>) :
    RecyclerView.Adapter<GnjWareHouseListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val groupNameLayout: LinearLayout = view.findViewById(R.id.groupNameLayout)
        val groupTitle: TextView = view.findViewById(R.id.groupTitle)
        val groupContent: TextView = view.findViewById(R.id.groupContent)
        val days: TextView = view.findViewById(R.id.days)

        val no: TextView = view.findViewById(R.id.no)
        val pc: TextView = view.findViewById(R.id.pc)
        val weight: TextView = view.findViewById(R.id.weight)
        /*val goods: TextView = view.findViewById(R.id.goods)
        val spCode: TextView = view.findViewById(R.id.spCode)
        val agentCode: TextView = view.findViewById(R.id.agentCode)
        val by1: TextView = view.findViewById(R.id.by1)
        val origin: TextView = view.findViewById(R.id.origin)*/

        val flight: TextView = view.findViewById(R.id.flight)

        /* val location: TextView = view.findViewById(R.id.location)
         val awbType: TextView = view.findViewById(R.id.awbType)
         val businessType: TextView = view.findViewById(R.id.businessType)
         val status: TextView = view.findViewById(R.id.status)*/
        val date: TextView = view.findViewById(R.id.date)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.gnj_ware_house_list_item, parent, false)
        val viewHolder = ViewHolder(view)

        viewHolder.itemView.setOnClickListener {

            var adapterPosition = viewHolder.adapterPosition
            if (adapterPosition < 0 || collectList.isEmpty()) {
                return@setOnClickListener
            }
            var gncFuBang = collectList[adapterPosition]

            ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_GNJ_WARE_HOUSE_INFO).withInt("id", gncFuBang.id)
                .navigation()

        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val collect = collectList[position]
        val groupTitle = collect.groupTitle
        val groupName = collect.groupName
        val days = collect.days
        //第一条
        if (position == 0 && groupName != "") {
            holder.groupNameLayout.visibility = View.VISIBLE
            holder.groupTitle.text = groupTitle
            holder.groupContent.text = groupName
            //出库时间分组时，days数据不为null，展示
            if (days != null) {
                holder.days.text = days
                holder.days.visibility = View.VISIBLE
            } else {
                holder.days.visibility = View.GONE
            }
        }
        //不是第一条
        else if (groupName != "") {
            //当前分组名和上一次分组名不一样，展示新的分组
            val preCollect = collectList[position - 1]
            val preGroupName = preCollect.groupName
            if (groupName != preGroupName) {
                holder.groupNameLayout.visibility = View.VISIBLE
                holder.groupTitle.text = groupTitle
                holder.groupContent.text = groupName
                //出库时间分组时，days数据不为null，展示
                if (days != null) {
                    holder.days.text = days
                    holder.days.visibility = View.VISIBLE
                } else {
                    holder.days.visibility = View.GONE
                }
            } else {
                holder.groupNameLayout.visibility = View.GONE
            }
        } else {
            holder.groupNameLayout.visibility = View.GONE
        }

        holder.no.text = collect.waybillNo
        holder.pc.text = collect.pc.toString()
        holder.weight.text = "${collect.weight} KG"
        /*holder.goods.text = collect.goods
        holder.spCode.text = collect.spCode
        holder.agentCode.text = collect.agentCode
        holder.by1.text = collect.by1
        holder.origin.text = collect.origin*/

        holder.flight.text = collect.flight
        /*holder.location.text = collect.location
        holder.awbType.text = collect.awbType
        holder.businessType.text = collect.businessType
        holder.status.text = collect.status*/
        holder.date.text = collect.date
    }

    override fun getItemCount() = collectList.size

}