package com.gamedog.vididin.main.fragments.home.fragment


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.ScaleAnimation
import androidx.annotation.NonNull
import androidx.core.view.isVisible
import com.ama.core.architecture.appBase.AppViewsEmptyViewModelFragment
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.common.widget.PopMenuIconView
import com.gamedog.vididin.R
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.beans.YoutubeVideo
import com.gamedog.vididin.main.WatchAdDialog
import com.gamedog.vididin.router.Router
import com.gamedog.vididin.widget.MyPlayerControlView
import com.gamedog.vididin.youtubestatistic.TickerTimer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.PlayerConstants
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.utils.loadOrCueVideo
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
import com.gamedog.vididin.databinding.VididinappFeatureHomeItemLayoutBinding as ViewBinding


class HomeItemFragment : AppViewsEmptyViewModelFragment<ViewBinding>() {
    private var mPlayerView: YouTubePlayerView? = null
    private var mIsStared = false
    private var mPlayer: YouTubePlayer? = null
    private var mVideoData: YoutubeVideo? = null
    private var mIsPlaying: Boolean = false
    private var mIsIntroExpand: Boolean = false
    private var mCurPlayedSecond: Float = 0F
    private var mTotalDuration: Float = 0F
    private val mTickerTimer = TickerTimer()


    override fun inflateViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ) = ViewBinding.inflate(inflater, container, false)


    override fun ViewBinding.initViews() {
        playIcon.setOnClickListener {
            if (mIsPlaying) mPlayer?.pause() else mPlayer?.play()
        }

        tvVideoFrom.text = mVideoData?.snippet?.localized?.title
        tvVideoIntro.text = mVideoData?.snippet?.localized?.description

        popMenu.setMenuList(
            mutableListOf(
                PopMenuIconView.MenuItem(R.mipmap.home_menu_1) {
                    Router.Benefit.startActivity(requireActivity())
                },
                PopMenuIconView.MenuItem(R.mipmap.home_menu_2) {
                    WatchAdDialog(requireActivity(), VididinEvents.Event_AD_TASK_TYPE_Watch_Ad_Earn_Gold, null).show()
                },
                PopMenuIconView.MenuItem(R.mipmap.home_menu_3) {

                },
                PopMenuIconView.MenuItem(R.mipmap.home_menu_4) {
                    Router.Benefit.startActivity(requireActivity())
                }
            ))

        setOnClickBatch(ivIntroExpand, maskView) {
            when (this) {
                maskView ->  {
                    if (mIsPlaying) mPlayer?.pause() else mPlayer?.play()
                }

                ivIntroExpand ->  {
                    mIsIntroExpand = !mIsIntroExpand
                    tvVideoIntro.maxLines = if (mIsIntroExpand) 8 else 2
                    ivIntroExpand.setImageResource(if (mIsIntroExpand) R.mipmap.arrow_down else R.mipmap.arrow_up)
                }
            }
        }
    }


    override fun ViewBinding.initListeners() {
        ivStar.setOnClickListener {
            mIsStared = !mIsStared
            ivStar.setImageResource(if (mIsStared) R.mipmap.home_star else R.mipmap.home_star_undo)
        }

    }

    override fun ViewBinding.initObservers() {

    }




    fun loadVideo() {
        if (null == mPlayerView) {
            mPlayerView = YouTubePlayerView(requireContext())
            binding!!.playerContainer.addView(mPlayerView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            lifecycle.addObserver(mPlayerView!!)
            mPlayerView?.enableAutomaticInitialization = true
        }


        val playerView = mPlayerView
        playerView!!.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
            override fun onReady(@NonNull youTubePlayer: YouTubePlayer) {
                mPlayer = youTubePlayer

                val playerUiController = MyPlayerControlView(playerView, youTubePlayer)
                playerView.setCustomPlayerUi(playerUiController.rootView)

                if (mVideoData != null && !mVideoData?.id.isNullOrEmpty()) {
                    youTubePlayer.loadOrCueVideo(
                        lifecycle,
                        mVideoData!!.id,
                        0f
                    )

                    if (!playerView.isVisible) {
                        youTubePlayer.pause()
                    }
                }
            }

            override fun onCurrentSecond(youTubePlayer: YouTubePlayer, second: Float) {
                super.onCurrentSecond(youTubePlayer, second)
                mCurPlayedSecond = second
                updateProgressbar()
            }

            override fun onVideoDuration(youTubePlayer: YouTubePlayer, duration: Float) {
                super.onVideoDuration(youTubePlayer, duration)
                mTotalDuration = duration
                mTickerTimer.setVideoInfo(mVideoData!!.id, (1000L * mTotalDuration).toLong())
            }

            override fun onStateChange(
                youTubePlayer: YouTubePlayer,
                state: PlayerConstants.PlayerState
            ) {
                when (state) {
                    PlayerConstants.PlayerState.PLAYING  -> {
                        togglePlayingState(true)
                    }
                    PlayerConstants.PlayerState.PAUSED  -> {
                        togglePlayingState(false)
                    }
                    PlayerConstants.PlayerState.UNKNOWN  -> {
                        togglePlayingState(false)
                    }
                    PlayerConstants.PlayerState.UNSTARTED  -> {
                        togglePlayingState(false)
                    }
                    PlayerConstants.PlayerState.ENDED  -> {
                        togglePlayingState(false)
                    }
                    PlayerConstants.PlayerState.BUFFERING  -> {
                        togglePlayingState(false)
                    }
                    PlayerConstants.PlayerState.VIDEO_CUED  -> {
                        togglePlayingState(false)
                    }
                }
            }
        })
    }

    private fun togglePlayingState(isPlaying: Boolean) {
        if (mIsPlaying != isPlaying) {
            mIsPlaying = isPlaying
            if (mIsPlaying) {
                hidePlayIconAnim()
            } else {
                showPlayIconAnim()
            }

            if (mIsPlaying) mTickerTimer.start() else mTickerTimer.pause()
        }
    }

    override fun onResume() {
        super.onResume()
        mPlayer?.play()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onPause() {
        super.onPause()
        mPlayer?.pause()
    }

    private fun updateProgressbar() {
        with(binding?.progressBarPlayer!!) {
            setMax(100)
            if (mTotalDuration > 0) {
                setProgress((100*mCurPlayedSecond/mTotalDuration).toInt())
            }
        }
    }


    private fun hidePlayIconAnim() {
        if (!binding?.playIcon!!.isVisible) {
            return
        }


        with (binding?.playIcon!!) {
            val animationSet = AnimationSet(true)
            animationSet.duration = 250
            val alphaAnimation = AlphaAnimation(0.6f, 0.3f)
            val scaleAnimation = ScaleAnimation(
                1.0f, 0.5f,
                1.0f, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
            )
            animationSet.addAnimation(alphaAnimation)
            animationSet.addAnimation(scaleAnimation)

            animationSet.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {
                }

                override fun onAnimationEnd(animation: Animation) {
                    visibility = View.GONE
                }

                override fun onAnimationRepeat(animation: Animation) {
                }
            })

            startAnimation(animationSet)
        }
    }

    private fun showPlayIconAnim() {
        with (binding?.playIcon!!) {
            visibility = View.VISIBLE

            val animationSet = AnimationSet(true)
            animationSet.duration = 250
            val alphaAnimation = AlphaAnimation(0.3f, 0.6f)
            val scaleAnimation = ScaleAnimation(
                0.5f, 1.0f,
                0.5f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
            )
            animationSet.addAnimation(alphaAnimation)
            animationSet.addAnimation(scaleAnimation)

            animationSet.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {
                }

                override fun onAnimationEnd(animation: Animation) {

                }

                override fun onAnimationRepeat(animation: Animation) {
                }
            })

            startAnimation(animationSet)
        }
    }




    companion object {
        private const val KEY_ID = "id"
        internal fun newInstance(id: Long, videoData: YoutubeVideo) = HomeItemFragment().apply {
            mVideoData = videoData
            arguments = Bundle().apply {
                putLong(KEY_ID, id)
            }
        }
    }
}