package com.yidian.shenghuoquan.bmanagercenter.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.http.httpbean.GetShopListBean
import com.yidian.shenghuoquan.bmanagercenter.ui.store.EditStoreActivity
import com.yidian.shenghuoquan.bmanagercenter.ui.store.viewholder.StoreViewHolder

/**
 * 门店管理列表的adapter
 */
class StoreAdapter(private val context: Context) : RecyclerView.Adapter<StoreViewHolder>() {

  private lateinit var data: ArrayList<GetShopListBean>

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StoreViewHolder {
    val view = LayoutInflater.from(context).inflate(R.layout.store_management_item, parent, false)
    return StoreViewHolder(view)
  }

  override fun onBindViewHolder(holder: StoreViewHolder, position: Int) {
    val store = data[position]
    holder.setData(store, position)
    holder.setOnListener(object : HolderListener {
      override fun jump() {
        val intent = Intent(context, EditStoreActivity::class.java)
        intent.putExtra("shop_name", store.shop_name)
        intent.putExtra("shop_id", store.shop_id)
        intent.putExtra("sub_shop_id", store.sub_shop_id)
        intent.putExtra("address", store.address)
        intent.putExtra("phone", store.phone)
        intent.putExtra("latitude", store.latitude)
        intent.putExtra("longitude", store.longitude)
        intent.putExtra("from", EditStoreActivity.fromEditStore)
        context.startActivity(intent)
      }
    })
  }

  override fun getItemCount(): Int {
    return if (::data.isInitialized) {
      data.size
    } else {
      0
    }
  }

  fun setData(it: ArrayList<GetShopListBean>) {
    this.data = it
    notifyDataSetChanged()
  }

  interface HolderListener {
    fun jump()
  }

}
