package com.gh.gamecenter.video.videomanager

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.entity.VideoDraftEntity
import com.gh.gamecenter.qa.draft.CommunityDraftWrapperActivity
import com.gh.gamecenter.qa.video.publish.VideoPublishActivity
import com.gh.gamecenter.video.upload.view.UploadVideoActivity

class VideoDraftFragment : ListFragment<VideoDraftEntity, VideoDraftViewModel>() {

    private var mAdapter: VideoDraftAdapter? = null

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == VideoManagerActivity.REQUEST_CODE_DRAFT && resultCode == UploadVideoActivity.RESULT_CODE_VIDEO ||
            resultCode == UploadVideoActivity.RESULT_CODE_DRAFT
        ) {
            onLoadRefresh()
        }
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        val insetDivider = ContextCompat.getDrawable(requireContext(), R.drawable.divider_item_line_space_16)
        val itemDecoration = CustomDividerItemDecoration(requireContext(), notDecorateTheLastItem = true)
        itemDecoration.setDrawable(insetDivider!!)
        return itemDecoration
    }

    override fun provideListAdapter(): VideoDraftAdapter {
        if (mAdapter == null) {
            mAdapter = VideoDraftAdapter(requireContext(), mListViewModel, mEntrance) {
                if (requireActivity() is CommunityDraftWrapperActivity) {
                    startActivity(VideoPublishActivity.getDraftIntent(requireContext(), it, mEntrance, "视频草稿"))
                } else {
                    val intent = Intent()
                    intent.putExtra(VideoDraftEntity::class.java.simpleName, it)
                    requireActivity().setResult(Activity.RESULT_OK, intent)
                    requireActivity().finish()
                }
            }
        }
        return mAdapter!!
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mListRv.layoutManager = FixLinearLayoutManager(requireContext())
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if ((mListRv?.itemDecorationCount ?: 0) > 0) {
            mListRv?.removeItemDecorationAt(0)
            mListRv?.addItemDecoration(itemDecoration)
        }
    }
}