package com.wdkl.ncs.android.component.hello.activity

import android.util.Log
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.enation.javashop.android.jrouter.external.annotation.Autowired
import com.enation.javashop.android.jrouter.external.annotation.Router
import com.enation.javashop.net.engine.model.NetState
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.wdkl.ncs.android.component.hello.R
import com.wdkl.ncs.android.component.hello.adapter.HelloItemAdapter
import com.wdkl.ncs.android.component.hello.databinding.HelloListActLayBinding
import com.wdkl.ncs.android.component.hello.launch.HelloLaunch
import com.wdkl.ncs.android.lib.base.BaseActivity
import com.wdkl.ncs.android.lib.utils.pop
import com.wdkl.ncs.android.lib.utils.push
import com.wdkl.ncs.android.lib.utils.showMessage
import com.wdkl.ncs.android.middleware.logic.contract.hello.HelloListActivityContract
import com.wdkl.ncs.android.middleware.logic.presenter.hello.HelloListActivityPresenter
import com.wdkl.ncs.android.middleware.model.JavaShopItem
import com.wdkl.ncs.android.middleware.model.ShopItem
import kotlinx.android.synthetic.main.hello_list_act_lay.*

@Router(path = "/hello/list")
class HelloListActivity : BaseActivity<HelloListActivityPresenter, HelloListActLayBinding>(), HelloListActivityContract.View {
    override fun bindDagger() {
        HelloLaunch.component.inject(this)
    }

    override fun onError(message: String, type: Int) {
        dismissDialog()
        showMessage(message)
    }

    override fun complete(message: String, type: Int) {
        dismissDialog()
        showMessage(message)
    }

    override fun start() {
        showDialog()
    }

    override fun networkMonitor(state: NetState) {

    }

    override fun destory() {

    }

    private var page :Int = 1
    private val adapter = HelloItemAdapter(ArrayList())

    //@Autowired(name= "keyword",required = true)
    @JvmField var keyword: String = "8"

    private lateinit var virtualLayoutManager: VirtualLayoutManager

    private lateinit var delegateAdapter: DelegateAdapter

    override fun getLayId(): Int {
        return R.layout.hello_list_act_lay
    }

    override fun init() {
        /**初始化LayoutMannager*/
        virtualLayoutManager = VirtualLayoutManager(this.activity)

        /**初始化适配器*/
        delegateAdapter = DelegateAdapter(virtualLayoutManager)
        delegateAdapter.addAdapter(adapter)
        mViewBinding.refresh.setRefreshFooter(ClassicsFooter(activity))
        /**配置到RecycleView*/
        listView.layoutManager = virtualLayoutManager
        listView.adapter = delegateAdapter
        presenter.loadData(keyword,page)
    }

    override fun bindEvent() {
        refresh.setOnLoadMoreListener {
            page += 1
            presenter.loadData(keyword,page)
        }
//        adapter.setOnItemClickListener { data, _ ->
//            push("/shop/detail",{postcard ->
//                postcard.withInt("shopId", data.shopId)
//            })
//        }
    }

    override fun render(data: ArrayList<JavaShopItem>) {
        if (page == 1){
            refresh.resetNoMoreData()
            if (data.size >0){
                adapter.data.clear()
                adapter.data.addAll(data)
                Log.i("abc1"," " + data.size);
                adapter.notifyDataSetChanged()
            }
            refresh.finishLoadMore()
        }else {
            if (data.size > 0) {
                adapter.data.addAll(data)
                adapter.notifyDataSetChanged()
                refresh.finishLoadMore()
            }else{
                refresh.finishLoadMoreWithNoMoreData()
            }
        }
    }
}