package com.gh.gamecenter.video.poster.video

import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.common.base.fragment.BaseFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.throwExceptionInDebug
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.FragmentVideoPosterBinding
import com.gh.gamecenter.entity.VideoEntity
import com.halo.assistant.HaloApp

class VideoPosterFragment : BaseFragment<Any>() {

    private lateinit var mBinding: FragmentVideoPosterBinding
    private lateinit var mAdapter: VideoPosterReviewAdapter

    private lateinit var mViewModel: VideoPosterViewModel

    override fun getLayoutId(): Int {
        return R.layout.fragment_video_poster
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = FragmentVideoPosterBinding.bind(mCachedView)

        val videoPath = arguments?.getString(EntranceConsts.KEY_PATH_VIDEO)
        val videoEntity = arguments?.getParcelable<VideoEntity>(VideoEntity::class.java.simpleName)

        val factory = VideoPosterViewModel.Factory(HaloApp.getInstance().application, videoPath, videoEntity)
        mViewModel = ViewModelProviders.of(this, factory).get(VideoPosterViewModel::class.java)
        mViewModel.videoPreviewsLiveData.observeNonNull(this, callback = {
            mAdapter.submitList(it)
        })
        mViewModel.previewLiveData.observeNonNull(this, callback = {
            if (it.thumbs != null) {
                mBinding.receiveIv.setBitmap(it.thumbs)
            } else if (it.thumbsUrl != null) {
                ImageUtils.picasso
                    .load(Uri.parse(it.thumbsUrl))
                    .into(mBinding.receiveIv.cropImageZoomView)
            }
        })
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.receiveIv.setCropRatio(9 / 16F)
        mBinding.reviewList.layoutManager = GridLayoutManager(requireContext(), 10)
        mAdapter = VideoPosterReviewAdapter(requireContext(), mViewModel)
        mBinding.reviewList.adapter = mAdapter
    }

    fun savePicture(path: String) {
        if (isAdded) throwExceptionInDebug("save clip picture failure", !mBinding.receiveIv.savePicture(path))
    }

    companion object {

        fun newInstance(videoPath: String?, videoEntity: VideoEntity?): VideoPosterFragment {
            val fragment = VideoPosterFragment()
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_PATH_VIDEO, videoPath)
            bundle.putParcelable(VideoEntity::class.java.simpleName, videoEntity)
            fragment.arguments = bundle
            return fragment
        }
    }
}