package com.xty.message.adapter

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.common.weight.InputView
import com.zj365.mime.R
import com.zj365.mime.adapter.DetailChildAdapter
import com.xty.network.model.SportDetail

class SportDetailAdapter: BaseMultiItemQuickAdapter<SportDetail.Action, BaseViewHolder>() {
    var mapView= LinkedHashMap<Int,List<TextView>>()
    var map2Adapter=LinkedHashMap<Int,DetailChildAdapter>()

    init {
        addItemType(1, R.layout.item_detail_1)
        addItemType(2,R.layout.item_detail_2)
        addItemType(3,R.layout.item_detail_3)
        addItemType(4,R.layout.item_detail_3)
    }
    override fun convert(holder: BaseViewHolder, item: SportDetail.Action) {

        holder.setText(R.id.mTitle,"训练动作${holder.layoutPosition+1}")
        when(holder.itemViewType){
            1->{

                holder.getView<InputView>(R.id.mV1).setValues(item.name)

                var newTime=item.time.substring(5,item.time.length)
                var newTime2=item.endTime.substring(5,item.endTime.length)
                holder.setText(R.id.mTime1,newTime)
                holder.setText(R.id.mTime2,newTime2)

            }
            2->{
                holder.getView<InputView>(R.id.mV1).setValues(item.name)
                var newTime=item.time.substring(5,item.time.length)
                var newTime2=item.endTime.substring(5,item.endTime.length)
                holder.setText(R.id.mTime1,newTime)
                holder.setText(R.id.mTime11,newTime2)
                var time2=holder.getView<TextView>(R.id.mTime2)
                var time3=holder.getView<TextView>(R.id.mTime3)
                var list= listOf(time2,time3)
                mapView[holder.layoutPosition]=list
            }
            3,4->{
                var newTime=item.time.substring(5,item.time.length)
                var newTime2=item.endTime.substring(5,item.endTime.length)

                var realTime1= if(item.realTime.isNullOrEmpty()){
                    ""
                }else{
                    item.realTime.substring(5,item.realTime.length)
                }

                var realEndTime2= if(item.realEndTime.isNullOrEmpty()){
                    ""
                }else{
                    item.realEndTime.substring(5,item.realEndTime.length)
                }

                holder.getView<InputView>(R.id.mV1).setValues(item.name)
                holder.setText(R.id.mTime1,"$newTime\n$newTime2")
                holder.setText(R.id.mTime2,"$realTime1\n$realEndTime2")
            }
        }

        var rv=holder.getView<RecyclerView>(R.id.mRecycle)
        var adapter=DetailChildAdapter()
        map2Adapter[holder.layoutPosition]=adapter
        initChildAdapter(rv,adapter,item.groupList)
    }
    private fun initChildAdapter(rv:RecyclerView,adapter: DetailChildAdapter,list:MutableList<SportDetail.Action.Group>) {
        rv.adapter=adapter
        rv.layoutManager=LinearLayoutManager(context)
        adapter.setNewInstance(list)

    }

    /**
     * 子项
     */
    fun getChildInfo(position:Int):MutableList<String>{
        var list= mutableListOf<String>()
        var iterator=  map2Adapter[position]!!.mapView.iterator()
        while (iterator.hasNext()){
            var map=iterator.next()
            list.add(map.value[0].text.trim().toString())
            list.add( map.value[1].text.trim().toString())
        }
        return list
    }

}