package com.yidian.yac.newscontent.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.common.base.BaseFragment
import com.yidian.common.chameleon.ChameleonEvent
import com.yidian.common.chameleon.ChameleonEventService
import com.yidian.common.chameleon.ChameleonServiceManager
import com.yidian.yac.newscontent.R
import com.yidian.yac.newscontent.adapter.SimpleHomeAdapter
import com.yidian.yac.newscontent.bean.SimpleItemBean
import kotlinx.android.synthetic.main.fragment_subscription.*


/**
 * 首页订阅Fragment
 */
class SubscriptionFragment:BaseFragment(),ChameleonEventService {
    companion object{
        fun newInstance():SubscriptionFragment{
            val fragment = SubscriptionFragment()
            val args = bundleOf()
            fragment.arguments = args
            return fragment
        }
    }
    private val adapter:SimpleHomeAdapter by lazy { SimpleHomeAdapter() }
    override val contentViewId: Int
        get() = R.layout.fragment_subscription

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        initView()
        initData()
    }

    private fun initView() {
//        refreshLayout.setRefreshHeader(ClassicsHeader(context))
//        refreshLayout.setRefreshFooter(ClassicsFooter(context))
        refreshLayout.setOnRefreshListener { refreshHeader ->
            refreshHeader.finishRefresh(2000)
        }
        refreshLayout.setOnLoadMoreListener { refreshFooter ->
            refreshFooter.finishLoadMore(2000)
        }
        rv_subscription.layoutManager = LinearLayoutManager(activity)
        rv_subscription.adapter = adapter
    }


    private fun initData() {
        val list = ArrayList<SimpleItemBean>()
        repeat(15){
            when(it){
                3 -> list.add(SimpleItemBean(380, "name $it", "content $it"))
                6 -> list.add(SimpleItemBean(375, "name $it", "content $it"))
                9 -> list.add(SimpleItemBean(377, "name $it", "content $it"))
                else->list.add(SimpleItemBean(0, "name $it", "content $it"))
            }
        }

        adapter.setData(list)

    }

    override fun onResume() {
        super.onResume()
        ChameleonServiceManager.registerChameleonService(this)
    }

    override fun onPause() {
        super.onPause()
        ChameleonServiceManager.unregisterChameleonService(this)
    }
    //卡梅隆与native的交互事件分发，需要在此过滤需要的事件类型,当前的事件在子线程中
    override fun dispatchEvent(event: ChameleonEvent) {
//        activity?.runOnUiThread{
//          startActivity(Intent(context, NewsContentActivity::class.java))
//        }
    }
}