package com.ux.video.file.filerecovery.photo

import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.LayoutInflater
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.base.BaseActivity
import com.ux.video.file.filerecovery.databinding.ActivityPhotoInfoBinding
import com.ux.video.file.filerecovery.success.RecoverySuccessActivity
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.Common.KEY_SCAN_TYPE
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_audio
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_deleted_audio
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_deleted_documents
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_deleted_photo
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_deleted_video
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_documents
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_photo
import com.ux.video.file.filerecovery.utils.Common.VALUE_SCAN_TYPE_video
import com.ux.video.file.filerecovery.utils.ExtendFunctions.dpToPx
import com.ux.video.file.filerecovery.video.VideoPlayActivity

class PhotoInfoActivity : BaseActivity<ActivityPhotoInfoBinding>() {

    companion object {
        val KEY_CLICK_ITEM = "click_item"
    }

    private var scanType: Int = VALUE_SCAN_TYPE_photo
    private var myData: ResultPhotosFiles? = null

    override fun inflateBinding(inflater: LayoutInflater): ActivityPhotoInfoBinding =
        ActivityPhotoInfoBinding.inflate(inflater)

    override fun initView() {
        super.initView()
        myData = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra(KEY_CLICK_ITEM, ResultPhotosFiles::class.java)
        } else {
            @Suppress("DEPRECATION")
            intent.getParcelableExtra(KEY_CLICK_ITEM)
        }
        scanType = intent.getIntExtra(KEY_SCAN_TYPE, VALUE_SCAN_TYPE_photo)
        setView()

    }


    override fun initData() {
        super.initData()
        binding.run {
            imageViewBack.setOnClickListener { finish() }
            myData?.let { resultPhotosFiles ->

                tvName.text = resultPhotosFiles.name
                tvPath.text = resultPhotosFiles.path
                tvDate.text = Common.getFormatDate(resultPhotosFiles.lastModified)
                tvResolution.text = resultPhotosFiles.resolution
                tvDuration.text = Common.formatDuration(resultPhotosFiles.duration)

                Glide.with(this@PhotoInfoActivity)
                    .load(resultPhotosFiles.targetFile)
                    .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(8.dpToPx(this@PhotoInfoActivity))))
                    .listener(object : RequestListener<Drawable> {
                        override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Drawable?>,
                            isFirstResource: Boolean
                        ): Boolean {
                            return false
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            model: Any,
                            target: Target<Drawable?>?,
                            dataSource: DataSource,
                            isFirstResource: Boolean
                        ): Boolean {

                            return false
                        }

                    })
                    .into(image)

                layoutBottom.tvLeft.run {
                    text = resources.getString(R.string.delete)
                    setOnClickListener {
                        RecoverOrDeleteManager.showConfirmDeleteDialog(
                            true,
                            supportFragmentManager,
                            lifecycleScope,
                            setOf(resultPhotosFiles)
                        ) { count ->
                            complete(count, 1)
                        }
                    }
                }

                layoutBottom.tvRight.run {
                    text = resources.getString(R.string.recover)
                    setOnClickListener {
                        RecoverOrDeleteManager.showRecoveringDialog(
                            supportFragmentManager,
                            lifecycleScope,
                            setOf(resultPhotosFiles)
                        ) { count ->
                            complete(count, 0)
                        }
                    }
                }
            }
        }
    }

    private fun setView() {
        binding.run {
            when (scanType) {
                VALUE_SCAN_TYPE_photo, VALUE_SCAN_TYPE_deleted_photo -> {
                    layoutName.isVisible = true
                    layoutPath.isVisible = true
                    layoutResolution.isVisible = true
                    layoutDate.isVisible = true

                    layoutType.isVisible = false
                    layoutSize.isVisible = false
                    layoutDuration.isVisible = false

                    imPlay.isVisible = false
                }

                VALUE_SCAN_TYPE_video, VALUE_SCAN_TYPE_deleted_video -> {
                    layoutName.isVisible = true
                    layoutPath.isVisible = true
                    layoutResolution.isVisible = true
                    layoutDate.isVisible = true
                    layoutDuration.isVisible = true

                    layoutType.isVisible = false
                    layoutSize.isVisible = false

                    imPlay.isVisible = true
                   myData?.let { data->
                       frameImage.setOnClickListener {
                           startActivity(Intent(this@PhotoInfoActivity, VideoPlayActivity::class.java).apply {
                               putExtra(VideoPlayActivity.KEY_DATA, data)
                           })
                       }
                   }
                }

                VALUE_SCAN_TYPE_audio, VALUE_SCAN_TYPE_deleted_audio -> {
                    layoutName.isVisible = true
                    layoutPath.isVisible = true
                    layoutSize.isVisible = true
                    layoutDate.isVisible = true
                    layoutDuration.isVisible = true

                    layoutResolution.isVisible = false
                    layoutType.isVisible = false
                }

                VALUE_SCAN_TYPE_documents, VALUE_SCAN_TYPE_deleted_documents -> {
                    layoutName.isVisible = true
                    layoutType.isVisible = true
                    layoutPath.isVisible = true
                    layoutSize.isVisible = true
                    layoutDate.isVisible = true

                    layoutDuration.isVisible = false
                    layoutDuration.isVisible = false
                }
            }

        }

    }

    private fun complete(number: Int, type: Int) {
        finish()
        startActivity(Intent(this@PhotoInfoActivity, RecoverySuccessActivity::class.java).apply {
            putExtra(RecoverySuccessActivity.KEY_SUCCESS_COUNT, number)
            putExtra(RecoverySuccessActivity.KEY_SUCCESS_TYPE, type)
        })
    }
}