package com.develop.setting.ui

import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.ext.navigateTo
import com.develop.base.mvvm.BaseBindingActivity
import com.develop.common.router.Screens
import com.develop.setting.databinding.ActivityPowerAnimationBinding

@Route(path = Screens.Setting.ANIMATION)
class PowerAnimationActivity : BaseBindingActivity<ActivityPowerAnimationBinding>() {

    override fun createViewBinding(inflater: LayoutInflater): ActivityPowerAnimationBinding {
        return ActivityPowerAnimationBinding.inflate(layoutInflater)
    }

    private val runnable: Runnable = object : Runnable {
        override fun run() {
            if (binding.fullVideoView.isPlaying) {
                val current: Int = binding.fullVideoView.currentPosition
                binding.seekbar.progress = current
            }
            mainHandler.postDelayed(this, 50)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.fullVideoView.apply {
            setVideoURI(Uri.parse("android.resource://" + packageName + "/" + com.develop.common.R.raw.video))
            start()
            mainHandler.post(runnable)
            setOnCompletionListener {
                binding.tvSkin.visibility = View.GONE
                binding.maskView.visibility = View.VISIBLE
                binding.functionLayout.visibility = View.VISIBLE
                binding.fullVideoView.isClickable = false
            }
            setOnPreparedListener {
                binding.seekbar.apply {
                    max = binding.fullVideoView.duration
                }
                it.setVolume(0f, 0f)
            }
        }

        binding.resumeLayout.setOnClickListener {
            binding.tvSkin.visibility = View.VISIBLE
            binding.maskView.visibility = View.GONE
            binding.functionLayout.visibility = View.GONE
            binding.fullVideoView.start()
            binding.fullVideoView.isClickable = true
        }

        binding.skinLayout.setOnClickListener {
            binding.fullVideoView.stopPlayback()
            navigateTo(Screens.Setting.LANGUAGE)
            finish()
        }

        binding.seekbar.setTouch(false)

        binding.tvSkin.setOnClickListener {
            binding.fullVideoView.stopPlayback()
            navigateTo(Screens.Setting.LANGUAGE)
            finish()
        }

        binding.fullVideoView.setOnClickListener {
            binding.ivPlay.visibility = View.VISIBLE
            binding.fullVideoView.pause()
        }

        binding.ivPlay.setOnClickListener {
            binding.ivPlay.visibility = View.GONE
            binding.fullVideoView.start()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mainHandler.removeCallbacks(runnable)
    }
}