package com.ux.video.file.filerecovery.success

import android.content.Intent
import android.os.Environment
import android.view.LayoutInflater
import androidx.core.view.isVisible
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.ActivityRecoverOrDeletedSuccessBinding
import com.ux.video.file.filerecovery.databinding.ActivityScanningBinding
import com.ux.video.file.filerecovery.main.MainActivity
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.result.ScanResultDisplayActivity
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.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 RecoverySuccessActivity : BaseActivity<ActivityRecoverOrDeletedSuccessBinding>() {

    companion object {
        val KEY_SUCCESS_TYPE = "success_type"
        val KEY_SUCCESS_COUNT = "success_count"
    }

    private var scanType: Int = VALUE_SCAN_TYPE_photo

    //0  恢复成功  1 删除成功
    private var successType = 0
    override fun inflateBinding(inflater: LayoutInflater): ActivityRecoverOrDeletedSuccessBinding =
        ActivityRecoverOrDeletedSuccessBinding.inflate(inflater)

    override fun initData() {
        super.initData()
        scanType = intent.getIntExtra(KEY_SCAN_TYPE, VALUE_SCAN_TYPE_photo)
        successType = intent.getIntExtra(KEY_SUCCESS_TYPE, 0)
        val counts = intent.getIntExtra(KEY_SUCCESS_COUNT, 0)

        binding.run {
            tvNumber.text = counts.toString()
            bottomBtnRecoverLayout.run {
                tvLeft.text = resources.getString(R.string.text_continue)
                tvRight.text = resources.getString(R.string.view)
            }
            when (scanType) {
                VALUE_SCAN_TYPE_photo, VALUE_SCAN_TYPE_deleted_photo -> tvFileType.text =
                    resources.getString(R.string.describe_photos)

                VALUE_SCAN_TYPE_video, VALUE_SCAN_TYPE_deleted_video -> tvFileType.text =
                    resources.getString(R.string.describe_videos)

                VALUE_SCAN_TYPE_audio, VALUE_SCAN_TYPE_deleted_audio -> tvFileType.text =
                    resources.getString(R.string.describe_audios)

                VALUE_SCAN_TYPE_documents, VALUE_SCAN_TYPE_deleted_documents -> tvFileType.text =
                    resources.getString(R.string.describe_documents)
            }
            when (successType) {
                0 -> {
                    imageCenter.setImageResource(R.drawable.image_recover_success)
                    tvNumber.setTextColor(
                        Common.getColorInt(
                            this@RecoverySuccessActivity,
                            R.color.main_text_blue
                        )
                    )
                    bottomBtnRecoverLayout.root.isVisible = true
                    tvContinue.isVisible = false
                    tvDescribe.text = resources.getString(R.string.recovered_success)

                    bottomBtnRecoverLayout.tvLeft.setOnClickListener { finish()}
                    bottomBtnRecoverLayout.tvRight.setOnClickListener {
                        //todo 跳转到所有恢复文件页面
                    }

                }

                1 -> {
                    imageCenter.setImageResource(R.drawable.image_deleted_success)
                    tvNumber.setTextColor(
                        Common.getColorInt(
                            this@RecoverySuccessActivity,
                            R.color.dialog_shape_delete
                        )
                    )
                    bottomBtnRecoverLayout.root.isVisible = false
                    tvContinue.isVisible = true
                    tvDescribe.text = resources.getString(R.string.deleted_success)
                    tvContinue.setOnClickListener { finish()}
                }
            }

            imageViewBack.setOnClickListener { finish() }
            imageBackHome.setOnClickListener {
                val intent = Intent(this@RecoverySuccessActivity, MainActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
                startActivity(intent)
                finish()

            }
        }


    }


}