package com.zj365.mime.act

import android.content.pm.ActivityInfo
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.xty.base.act.IBaseAct
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.getImageUrl
import com.xty.common.picture.PictureUtils
import com.xty.common.recycleUtils.RecycleItem
import com.zj365.mime.BuildConfig
import com.zj365.mime.R
import com.zj365.mime.adapter.ImageShowAdapter
import com.zj365.mime.databinding.ActFeedBackDetailBinding
import com.xty.network.model.FeedBackBean
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager


/**
 *  反馈意见详情
 */
@Route(path = ARouterUrl.FEED_BACK_DETAIL)
class FeedBackDetail: IBaseAct() {
    val binding by lazy{ActFeedBackDetailBinding.inflate(layoutInflater)}
    var  orientationUtils : OrientationUtils?=null
    val mAdapter= ImageShowAdapter()
    var bean : FeedBackBean?=null
    var hasVideo=false
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        isDarkBar=true
        bean=intent.extras!!.getSerializable("bean") as FeedBackBean
    }
    override fun initView() {
        super.initView()
        //RouteManager.goAct( ARouterUrl.FEED_LIST)   往期意见反馈
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        binding.title.mTvTitle.text = getString(R.string.feed_back)
        bean?.let {
            binding.mEdit.text=it.content

            initAdapter()
            var listPhoto= mutableListOf<String>()
                it.imagesList?.forEach { url->
                if(url.endsWith("png")||url.endsWith("jpg")||url.endsWith("jpeg")){
                    listPhoto.add(url)
                }else if(url.endsWith("mp4")||url.endsWith("3GP")){
                    hasVideo=true
                    PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)
                    binding.mGsy.setUpLazy(getImageUrl(url),false,null,null,"")
                    LogUtils.d("视频地址:${getImageUrl(url)}")
                    // binding.mGsy.setUpLazy("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",false,null,null,"")
                    // var view=ImageView(this)
                    // PictureUtils.getVideoThumb(BuildConfig.image_prefix+url, view)
                    // binding.mGsy.thumbImageView=view
                    //
                    //设置旋转
                    orientationUtils = OrientationUtils(this,   binding.mGsy)
                    binding.mGsy.fullscreenButton.visibility=View.GONE
                }
            }
            if(!hasVideo){
                binding.mGsy.visibility=View.GONE
                binding.mv.visibility=View.GONE
            }
            if(listPhoto.isEmpty()){
                binding.mImageTitle.visibility=View.GONE
                binding.mRecycle.visibility=View.GONE
            }else{
                mAdapter.setNewInstance(listPhoto)
            }

        }


    }


    private fun initAdapter() {
        var item = RecycleItem(this, 40, 1)
        item.setSpace(40, 40)
        binding.mRecycle.addItemDecoration(item)
        binding.mRecycle.layoutManager = GridLayoutManager(this, 3)
        binding.mRecycle.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            PictureUtils.httpPreviewImage(this,position,mAdapter.data,BuildConfig.image_prefix)
        }

    }
    override fun onBackPressed() {
        //先返回正常状态
        if (orientationUtils!=null&&orientationUtils!!.getScreenType() === ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            binding.mGsy.fullscreenButton.performClick()
            return
        }
        //释放所有
        binding.mGsy.setVideoAllCallBack(null)
        super.onBackPressed()
    }
    override fun onPause() {
        super.onPause()
        binding.mGsy.onVideoPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos();
    }


}