package com.ux.video.file.filerecovery.result

import android.content.Intent
import android.os.Environment
import android.view.LayoutInflater
import androidx.lifecycle.lifecycleScope
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.base.BaseActivity
import com.ux.video.file.filerecovery.databinding.ActivityScanningBinding
import com.ux.video.file.filerecovery.main.ScanSelectTypeActivity.Companion.VALUE_AUDIO
import com.ux.video.file.filerecovery.main.ScanSelectTypeActivity.Companion.VALUE_DOCUMENT
import com.ux.video.file.filerecovery.main.ScanSelectTypeActivity.Companion.VALUE_PHOTO
import com.ux.video.file.filerecovery.main.ScanSelectTypeActivity.Companion.VALUE_VIDEO
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.ScanManager
import com.ux.video.file.filerecovery.utils.ScanRepository
import com.ux.video.file.filerecovery.utils.ScanState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class ScanningActivity : BaseActivity<ActivityScanningBinding>() {

    companion object {
//        val KEY_SCAN_TYPE = "scan_type"
//        val VALUE_SCAN_TYPE_photo = 0
//        val VALUE_SCAN_TYPE_deleted_photo = 1
//        val VALUE_SCAN_TYPE_video = 2
//        val VALUE_SCAN_TYPE_deleted_video = 3
//        val VALUE_SCAN_TYPE_audio = 4
//        val VALUE_SCAN_TYPE_deleted_audio = 5
//        val VALUE_SCAN_TYPE_documents = 6
//        val VALUE_SCAN_TYPE_deleted_documents = 7
    }

    private var scanType: Int = VALUE_SCAN_TYPE_photo
    override fun inflateBinding(inflater: LayoutInflater): ActivityScanningBinding =
        ActivityScanningBinding.inflate(inflater)

    override fun initData() {
        super.initData()
        scanType = intent.getIntExtra(KEY_SCAN_TYPE, VALUE_SCAN_TYPE_photo)
        setSelectType(scanType)
        when (scanType) {
            VALUE_SCAN_TYPE_photo, VALUE_SCAN_TYPE_video, VALUE_SCAN_TYPE_audio, VALUE_SCAN_TYPE_documents -> scanAll()
            VALUE_SCAN_TYPE_deleted_photo, VALUE_SCAN_TYPE_deleted_video, VALUE_SCAN_TYPE_deleted_audio, VALUE_SCAN_TYPE_deleted_documents -> scanDeleted()
        }
        binding.scanProgress.setCenterImage(R.drawable.im_photo_center_image)
        binding.imageViewBack.setOnClickListener { finish() }

    }

    private fun setSelectType(fileType: Int) {
        binding.run {

            when (fileType) {
                VALUE_SCAN_TYPE_photo -> {
                    title.text = getString(R.string.photo_title)
                    tvScanDescribe.text = getString(R.string.describe_photos)
                }

                VALUE_SCAN_TYPE_deleted_photo -> {
                    title.text = getString(R.string.photo_title)
                    tvScanDescribe.text = getString(R.string.describe_delete_photos)
                }

                VALUE_SCAN_TYPE_video -> {
                    title.text = getString(R.string.video_title)
                    tvScanDescribe.text = getString(R.string.describe_videos)
                }

                VALUE_SCAN_TYPE_deleted_video -> {
                    title.text = getString(R.string.video_title)
                    tvScanDescribe.text = getString(R.string.describe_delete_videos)
                }

                VALUE_SCAN_TYPE_audio -> {
                    title.text = getString(R.string.audio_title)
                    tvScanDescribe.text = getString(R.string.describe_audios)
                }

                VALUE_SCAN_TYPE_deleted_audio -> {
                    title.text = getString(R.string.audio_title)
                    tvScanDescribe.text = getString(R.string.describe_delete_audios)
                }

                VALUE_SCAN_TYPE_documents -> {
                    title.text = getString(R.string.document_title)
                    tvScanDescribe.text = getString(R.string.describe_documents)
                }

                VALUE_SCAN_TYPE_deleted_documents -> {
                    title.text = getString(R.string.document_title)
                    tvScanDescribe.text = getString(R.string.describe_delete_documents)
                }
            }
        }


    }

    private fun scanAll() {
        val total = 800
        lifecycleScope.launch {
            val root = Environment.getExternalStorageDirectory()
            ScanManager.scanAllDocuments(root, type = scanType).flowOn(Dispatchers.IO).collect {
                when (it) {
                    is ScanState.Progress -> {
                        updateProgress(it)
                    }

                    is ScanState.Complete -> {
                        updateComplete(it)
                    }
                }
            }


        }
    }

    private fun scanDeleted() {
        lifecycleScope.launch {

            val root = Environment.getExternalStorageDirectory()
            ScanManager.scanHiddenPhotoAsync(root, type = scanType).flowOn(Dispatchers.IO).collect {
                when (it) {
                    is ScanState.Progress -> {
                        updateProgress(it)

                    }

                    is ScanState.Complete -> {
                        updateComplete(it)
                    }
                }
            }
        }

    }


    private fun updateProgress(scanState: ScanState.Progress) {
        val total = 1000
        scanState.let {
            binding.run {
                val percent = (it.scannedCount * 100 / total).coerceAtMost(100)
                scanProgress.setProgress(percent)
                tvScanCurrentFilePath.text = it.filePath
                tvScanCurrentCounts.text = it.scannedCount.toString()
                ScanManager.showLog("Scan", it.filePath)
            }
            ScanManager.showLog(
                "HiddenScan",
                "进度: ${it.scannedCount}"
            )
        }

    }

    private fun updateComplete(scanState: ScanState.Complete) {
        binding.scanProgress.setProgress(100)
        scanState.let {
            startActivity(
                Intent(
                    this@ScanningActivity,
                    ScanResultDisplayActivity::class.java
                ).apply {
                    putParcelableArrayListExtra(
                        ScanResultDisplayActivity.KEY_SCAN_RESULT,
                        it.result
                    )
                })
            ScanManager.showLog(
                "HiddenScan",
                "完成: ${it.result.size}"
            )

        }
        finish()

    }

}