import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ym.game.view.MainDialog
import com.ym.library.module.ExchangeEntity
import com.ym.library.module.GameDuiHuanShopEntity
import com.ym.library.net.BaseObserver
import com.ym.library.net.GameApiClient
import com.ym.library.net.RxSchedulers
import com.ym.library.rxbus.RxBusConstant
import com.ym.library.rxbus.RxBusUtil
import com.ym.library.utils.Utils
import com.ym.modulecommon.base.BaseRecyclerAdapter
import com.ym.modulecommon.base.NoDoubleClickListener
import com.ym.xynt.R
import kotlinx.android.synthetic.main.item_with_draw_list.view.*

class AllOrderAdapter : BaseRecyclerAdapter<GameDuiHuanShopEntity, RecyclerView.ViewHolder> {

    constructor(activity: Activity, mDialog: Dialog) : super() {
        this.activity = activity
        this.dialog = mDialog
    }

    private var dialog: Dialog? = null
    private var activity: Activity? = null

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val order: GameDuiHuanShopEntity = mList[position]
        holder.itemView.cash_item_money_tv.text = "${order.cashStr}${order.unit}";

        val mAdapter: OrderAdapter? = OrderAdapter()
        mAdapter?.appendToList(order?.needPlantList)
        holder?.itemView?.item_recycler_fruit_list?.adapter = mAdapter


//        holder.itemView.item_click_duihuan?.addClickScale()
        holder.itemView.item_click_duihuan?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(view: View?) {
                duihuan(order.id, order.type!!, order.cashStr)
            }
        })


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChangeViewHolder {
        return ChangeViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.item_with_draw_list,
                parent,
                false
            )
        )
    }

    inner class ChangeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var item_list: RecyclerView?
        var item_duihuan: LinearLayout?
        var item_money_tv: TextView?

        init {
            item_list = itemView.findViewById<RecyclerView>(R.id.item_recycler_fruit_list)
            item_duihuan = itemView.findViewById<LinearLayout>(R.id.item_click_duihuan)
            item_money_tv = itemView.findViewById<TextView>(R.id.cash_item_money_tv)

//            val linearLayoutManager: LinearLayoutManager = object : LinearLayoutManager(activity) {
//                override fun canScrollVertically(): Boolean {
//                    return false
//                }
//            }
//            linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
//            item_list?.layoutManager = linearLayoutManager

            val gridLayoutManager: GridLayoutManager = object : GridLayoutManager(activity, 3) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            item_list?.layoutManager = gridLayoutManager
        }
    }


    fun duihuan(id: Int, type: Int, jine: String) {
        GameApiClient.gameApi.dowd(id).compose(
            RxSchedulers.observableIO2Main()
        ).subscribe(object : BaseObserver<ExchangeEntity>() {
            override fun onSuccess(result: ExchangeEntity?) {
                RxBusUtil.getDefault()
                    .send(RxBusConstant.RX_WITH_DRAW_LIST)
                if (result != null) {
                    MainDialog?.showWithDraw(
                        activity,
                        result?.status ?: 0,
                        result?.title ?: "",
                        result?.reason ?: "",
//                        result?.buttonText ?: "",
                        result?.CashText ?: "",
                        result?.buttonStatus ?: 0,
                        id,
                        type,
                        jine,
                        false,
                        dialog!!
                    )
                }
            }

            override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                Utils.showToast(activity, "" + errorMsg)
            }
        })
    }

}
