package com.duben.microtribe.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
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.FollowPresenter
import com.duben.microtribe.mvp.views.FollowView
import com.duben.microtribe.ui.activitys.MainActivity
import com.duben.microtribe.ui.adapter.FollowAdapter
import com.duben.microtribe.ui.fragment.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_follow_video.*

/**
 *  @author Assen
 *  @date 2023/7/5
 *  @desc 主页 -> 推荐 —> 追剧
 */
class FollowVideoFragment : BaseFragment(), FollowView {

    private val followPresenter by lazy { FollowPresenter() }

    private val datas = arrayListOf<VedioBean>()
    private var followAdapter: FollowAdapter? = null

    companion object {
        fun newInstance(): Fragment {
            val args = Bundle()
            val fragment = FollowVideoFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onResume() {
        super.onResume()
        followPresenter.getCollectList()
    }

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

        initRecy()
    }

    override fun getContentViewLayoutID() = R.layout.fragment_follow_video

    private fun initRecy() {
        val emptyView =
            LayoutInflater.from(requireContext()).inflate(R.layout.item_follow_empty, null)
        emptyView.findViewById<View>(R.id.btn).setOnClickListener {
            // 去剧场
            (requireActivity() as MainActivity).changeRecommendTab(1)
        }
        rv_follow.layoutManager = GridLayoutManager(requireContext(), 3)
        followAdapter = FollowAdapter(requireActivity())
        followAdapter?.setEmptyView(emptyView)
        followAdapter?.setNewInstance(datas)
        followAdapter?.setOnItemLongClickListener { adapter, view, position ->
            datas[position].isChecked = true
            followAdapter?.setEditStyle(true)
            showEditView(false)
            return@setOnItemLongClickListener false
        }
        followAdapter?.setOnItemClickListener { adapter, view, position ->
            if (followAdapter?.getEditStyle() == true) return@setOnItemClickListener
            LocalVedioManager.startVedioDetailActivityForType(
                requireActivity(),
                datas[position]
            )
        }
        rv_follow.adapter = followAdapter
    }

    fun getAdapter(): FollowAdapter? {
        return followAdapter
    }

    override fun onDetach() {
        super.onDetach()
        followPresenter.detachView()
    }

    private fun showEditView(show: Boolean) {
        (requireActivity() as MainActivity).showEditView(show)
    }

    private fun hideEditView(hide: Boolean) {
        (requireActivity() as MainActivity).hideEditView(hide)
    }

    override fun getCollectListSuc(bannerList: BannerList) {
        datas.clear()
        if (bannerList.list.isNotEmpty()) {
            datas.addAll(bannerList.list)
            followAdapter?.setNewInstance(datas)
            showEditView(true)
        } else {
            hideEditView(true)
        }
        followAdapter?.setEditStyle(false)
    }

    override fun getCollectListFail() {
        showEditView(false)
    }

    override fun cancelCollectSuc() {
        followPresenter.getCollectList()
        (requireActivity() as MainActivity).showEditView(true)
    }

    override fun cancelCollectFail() {

    }

    fun delete() {
        followAdapter?.let {
            val strArr = arrayListOf<String>()
            for (datum in it.data) {
                if (datum.isChecked) {
                    strArr.add("" + datum.vedioId)
                }
            }
            followPresenter.cancelCollect(strArr)
        }
    }

}