package com.duben.microtribe.ui.activitys

import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.duben.microtribe.R
import com.duben.microtribe.manager.LocalVedioManager
import com.duben.microtribe.mvp.model.BannerList
import com.duben.microtribe.mvp.model.VedioBean
import com.duben.microtribe.mvp.presenters.WatchRecordPresenter
import com.duben.microtribe.mvp.views.WatchRecordView
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.adapter.WatchRecordAdapter
import com.duben.microtribe.ui.widgets.StickHeaderDecoration
import com.duben.microtribe.utils.TimeHelp
import kotlinx.android.synthetic.main.activity_watch_record.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 *  @author Assen
 *  @date 2023/7/7
 *  @desc
 */
class WatchRecordActivity : BaseActivity(), WatchRecordView {

    private val watchRecordPresenter by lazy { WatchRecordPresenter() }

    private var mWatchRecordAdapter: WatchRecordAdapter? = null
    private var datas = mutableListOf<VedioBean>()

    override fun getContentViewLayoutID() = R.layout.activity_watch_record

    override fun initViewsAndEvents() {
        watchRecordPresenter.attachView(this)

        tv_title.text = "观看记录"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
        iv_left_icon.setOnClickListener { finish() }

        watchRecordPresenter.getHistory()

        mWatchRecordAdapter = WatchRecordAdapter()
        val emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_history, null)
        mWatchRecordAdapter?.setEmptyView(emptyView)
        mWatchRecordAdapter?.setNewInstance(datas)
        mWatchRecordAdapter?.setOnItemClickListener { adapter, view, position ->
            LocalVedioManager.startVedioDetailActivityForType(
                this@WatchRecordActivity,
                datas[position]
            )
        }
        mWatchRecordAdapter?.setOnItemCollectClickListener(object :
            WatchRecordAdapter.OnItemCollectClickListener {
            override fun onItemCollectClick(position: Int) {
                if (datas[position].collect == 0) {
                    datas[position].collect = 1
                    watchRecordPresenter.collect("" + datas[position].vedioId)
                } else {
                    datas[position].collect = 0
                    watchRecordPresenter.cancelCollect("" + datas[position].vedioId)
                }
                mWatchRecordAdapter?.notifyItemChanged(position)
            }
        })
        rv_watch_record.layoutManager = LinearLayoutManager(this)
        rv_watch_record.addItemDecoration(StickHeaderDecoration(this))
        rv_watch_record.adapter = mWatchRecordAdapter
    }

    override fun isApplyKitKatTranslucency() = true

    override fun onDestroy() {
        super.onDestroy()
        watchRecordPresenter.detachView()
    }

    override fun getHistorySuc(historyBean: BannerList) {
        datas.clear()
        val data = historyBean.list
        for (datum in data) {
            datum.topTabsReamrk = TimeHelp.format(datum.seeUpdateTime * 1000)
            datas.add(datum)
        }
//        mWatchRecordAdapter?.setNewInstance(datas)
        mWatchRecordAdapter?.notifyDataSetChanged()
    }

    override fun getHistoryFail() {

    }

    override fun collectSuc() {

    }

    override fun collectFail() {

    }

    override fun cancelCollectSuc() {

    }

    override fun cancelCollectFail() {

    }

}