package com.lukouguoji.hangban.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.hangban.R
import com.lukouguoji.hangban.model.HangBanListModel
import com.lukouguoji.hangban.viewModel.HangBanListViewModel
import com.lukouguoji.module_base.router.ARouterConstants

class HangBanListAdapter(
    val activity: Activity,
    private val viewModel: HangBanListViewModel,
    private val collectList: MutableList<HangBanListModel>
) :
    RecyclerView.Adapter<HangBanListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val fdate: TextView = view.findViewById(R.id.fdate)
        val fno: TextView = view.findViewById(R.id.fno)
        val range: TextView = view.findViewById(R.id.range)
        val countryType: TextView = view.findViewById(R.id.countryType)
        val jtz: TextView = view.findViewById(R.id.jtz)

        val inLayout: LinearLayout = view.findViewById(R.id.inLayout)
        val scheduledArrival: TextView = view.findViewById(R.id.scheduledArrival)
        val estimatedArrival: TextView = view.findViewById(R.id.estimatedArrival)
        val actualArrival: TextView = view.findViewById(R.id.actualArrival)

        val outLayout: LinearLayout = view.findViewById(R.id.outLayout)
        val scheduledTackOff: TextView = view.findViewById(R.id.scheduledTackOff)
        val estimatedTakeOff: TextView = view.findViewById(R.id.estimatedTakeOff)
        val actualTakeOff: TextView = view.findViewById(R.id.actualTakeOff)

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

        /* val serviceType: TextView = view.findViewById(R.id.serviceType)
         val flightStatus: TextView = view.findViewById(R.id.flightStatus)
         val aircraftCode: TextView = view.findViewById(R.id.aircraftCode)
         val standId: TextView = view.findViewById(R.id.standId)*/
    }

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

        viewHolder.itemView.setOnClickListener {

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

            ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_HANG_BAN_QUERY_INFO).withInt("id", queryList.id)
                .navigation()

        }

        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val collect = collectList[position]

        //0出港 1进港
        if (collect.status == "0") {
            holder.outLayout.visibility = View.VISIBLE
            holder.inLayout.visibility = View.GONE
        } else {
            holder.inLayout.visibility = View.VISIBLE
            holder.outLayout.visibility = View.GONE
        }

        holder.fdate.text = collect.fdate
        holder.fno.text = collect.fno
        holder.range.text = collect.range
        holder.countryType.text = collect.countryType
        holder.jtz.text = collect.jtz

        holder.scheduledArrival.text = collect.scheduledArrival
        holder.estimatedArrival.text = collect.estimatedArrival

        holder.actualArrival.text = collect.actualArrival
        holder.scheduledTackOff.text = collect.scheduledTackOff
        holder.estimatedTakeOff.text = collect.estimatedTakeOff
        holder.actualTakeOff.text = collect.actualTakeOff

        holder.delayFreeText.text = collect.delayFreeText

        /*holder.serviceType.text = collect.serviceType
        holder.flightStatus.text = collect.flightStatus
        if (collect.flightStatus.contains("延误")) {
            holder.flightStatus.setTextColor(activity.resources.getColor(R.color.red, activity.theme))
        }
        holder.aircraftCode.text = collect.aircraftCode
        holder.standId.text = collect.standId*/
    }

    override fun getItemCount() = collectList.size

    fun append(hangBanListModel: HangBanListModel) {
        collectList.add(hangBanListModel)
        notifyDataSetChanged()
    }

}