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

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.base.BaseActivity
import com.ux.video.file.filerecovery.databinding.ActivityScanResultDisplayBinding
import com.ux.video.file.filerecovery.photo.PhotoSortingActivity
import com.ux.video.file.filerecovery.photo.ResultPhotos
import com.ux.video.file.filerecovery.result.ScanningActivity
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.getParcelableArrayListExtraCompat
import com.ux.video.file.filerecovery.utils.ScanManager
import com.ux.video.file.filerecovery.utils.ScanRepository

/**
 * 扫描结果汇总展示
 */
class ScanResultDisplayActivity : BaseActivity<ActivityScanResultDisplayBinding>() {
    private var scanResultAdapter: ScanResultAdapter? = null
    private var scanType: Int = VALUE_SCAN_TYPE_photo
    private var exitDialog: ExitDialogFragment? = null

    companion object {
        val KEY_SCAN_RESULT = "scan_result"

    }

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

    override fun initView() {
        super.initView()
        val list: ArrayList<ResultPhotos>? =
            intent.getParcelableArrayListExtraCompat(KEY_SCAN_RESULT)
        scanResultAdapter = ScanResultAdapter(this@ScanResultDisplayActivity) { folderLists ->
            startActivity(
                Intent(
                    this@ScanResultDisplayActivity,
                    PhotoSortingActivity::class.java
                ).apply {
                    putParcelableArrayListExtra(
                        PhotoSortingActivity.KEY_PHOTO_FOLDER_FILE,
                        folderLists
                    )
                })
        }
        binding.recyclerResult.run {
            adapter = scanResultAdapter
            layoutManager = LinearLayoutManager(this@ScanResultDisplayActivity)
        }
        list?.let {
            binding.run {
                textDirCount.text = it.size.toString()
                val sumOf = it.sumOf { it.allFiles.size }
                textAllCounts.text = sumOf.toString()
            }
            scanResultAdapter?.setData(it)
        }


//        ScanRepository.instance.photoResults.observe(this@ScanResultDisplayActivity) {
//            binding.run {
//                textDirCount.text = it.size.toString()
//                val sumOf = it.sumOf { it.allFiles.size }
//                textAllCounts.text = sumOf.toString()
//            }
//            scanResultAdapter?.setData(it)
//        }
    }

    override fun initData() {
        super.initData()
        scanType = intent.getIntExtra(KEY_SCAN_TYPE, VALUE_SCAN_TYPE_photo)
        setSelectTypeTitle(scanType)
        binding.imageViewBack.setOnClickListener { dealExit() }

        onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    dealExit()
                }
            })
    }


    private fun dealExit(){
        exitDialog = exitDialog?:ExitDialogFragment(){
            finish()
        }
        exitDialog?.show(supportFragmentManager,"")
    }
    private fun setSelectTypeTitle(fileType: Int) {
        binding.run {
            when (fileType) {
                VALUE_SCAN_TYPE_photo, VALUE_SCAN_TYPE_deleted_photo -> {
                    title.text = getString(R.string.photo_title)
                }

                VALUE_SCAN_TYPE_video, VALUE_SCAN_TYPE_deleted_video -> {
                    title.text = getString(R.string.video_title)
                }

                VALUE_SCAN_TYPE_audio, VALUE_SCAN_TYPE_deleted_audio -> {
                    title.text = getString(R.string.audio_title)
                }

                VALUE_SCAN_TYPE_documents, VALUE_SCAN_TYPE_deleted_documents -> {
                    title.text = getString(R.string.document_title)
                }
            }
        }


    }
}