package com.adealink.weparty.commonui.mediapicker.videoselect

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.DisplayUtil
import com.adealink.weparty.R
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.mediapicker.videoselect.adapter.VideoSelectItemViewBinder
import com.adealink.weparty.commonui.mediapicker.videoselect.data.VideoItem
import com.adealink.weparty.commonui.mediapicker.videoselect.listener.VideoSelectOpListener
import com.adealink.weparty.commonui.mediapicker.videoselect.viewmodel.VideoSelectViewModel
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.databinding.ActivityVideoSelectBinding
import com.adealink.weparty.permission.PermissionUtils
import com.adealink.weparty.util.QMUIStatusBarHelper

class VideoSelectActivity : BaseActivity(), VideoSelectOpListener {

    companion object {
        private const val SPAN_COUNT = 3
        const val KEY_SOURCE = "key_source"
        const val KEY_VIDEO_PATH = "key_video_path"
        const val KEY_VIDEO_URI = "key_video_uri"
    }

    private var source: String = ""

    private val binding by viewBinding(ActivityVideoSelectBinding::inflate)
    private val videoSelectViewModel by viewModels<VideoSelectViewModel>()
    private val videosAdapter by fastLazy { MultiTypeListAdapter<VideoItem>() }

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        source = intent.getStringExtra(KEY_SOURCE) ?: ""
    }

    override fun initViews() {
        super.initViews()
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        setContentView(binding.root)
        binding.videoRv.layoutManager =
            GridLayoutManager(this, SPAN_COUNT, RecyclerView.VERTICAL, false)
        binding.videoRv.addItemDecoration(
            GridSpacingItemDecoration(
                SPAN_COUNT,
                DisplayUtil.dp2px(4f),
                DisplayUtil.dp2px(4f),
                true
            )
        )
        binding.videoRv.adapter = videosAdapter
        videosAdapter.register(VideoSelectItemViewBinder(this))
    }

    override fun observeViewModel() {
        super.observeViewModel()
        videoSelectViewModel.videosLD.observe(this) {
            when (it) {
                is Rlt.Success -> {
                    videosAdapter.submitList(it.data)
                }
                is Rlt.Failed -> {
                    showFailedToast(it)
                }
            }
        }
    }


    @SuppressLint("CheckResult")
    override fun loadData() {
        super.loadData()
        PermissionUtils.getRxPermissions(this)
            .request(
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    Manifest.permission.READ_MEDIA_VIDEO
                } else {
                    Manifest.permission.READ_EXTERNAL_STORAGE
                }
            )
            .subscribe { granted ->
                if (granted) {
                    videoSelectViewModel.loadVideos()
                } else {
                    showPermissionNoGrantedDialog()
                }
            }
    }

    private fun showPermissionNoGrantedDialog() {
        CommonDialog.Builder()
            .message(getCompatString(R.string.video_select_permission_no_granted))
            .onPositive {
                PermissionUtils.startPermissionSettingActivity(this)
            }
            .onNegative {
                finish()
            }
            .setShowDefaultCancel(true)
            .build().show(supportFragmentManager)
    }

    override fun onVideoSelect(video: VideoItem) {
        val intent = Intent()
        intent.putExtra(KEY_SOURCE, source)
        intent.putExtra(KEY_VIDEO_PATH, video.path)
        intent.putExtra(KEY_VIDEO_URI, video.url)
        setResult(RESULT_OK, intent)
        finish()
    }

}