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

import android.content.Intent
import android.view.LayoutInflater
import androidx.activity.OnBackPressedCallback
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.photo.ResultPhotosFiles
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

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

    private var list: ArrayList<ResultPhotos>? = null

    companion object {
        val KEY_SCAN_RESULT = "scan_result"

    }

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

    override fun initView() {
        super.initView()
        list = intent.getParcelableArrayListExtraCompat(KEY_SCAN_RESULT)
        scanType = intent.getIntExtra(KEY_SCAN_TYPE, VALUE_SCAN_TYPE_photo)
        setSelectTypeTitle(scanType)


    }

    override fun initData() {
        super.initData()
        binding.imageViewBack.setOnClickListener { dealExit() }
        onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    dealExit()
                }
            })

        binding.run {
            val myAdapter = when (scanType) {
                VALUE_SCAN_TYPE_audio, VALUE_SCAN_TYPE_deleted_audio, VALUE_SCAN_TYPE_documents, VALUE_SCAN_TYPE_deleted_documents -> {
                    bottomLayout.setBackgroundResource(R.drawable.bg_rectangle_white_top_20)
                    ScanResultDocumentsAdapter(
                        this@ScanResultDisplayActivity,
                        scanType
                    ) { folderLists ->
                        goSort(folderLists)
                    }
                }

                else -> {
                    bottomLayout.setBackgroundResource(0)
                    ScanResultPhotoAdapter(
                        this@ScanResultDisplayActivity,
                        scanType
                    ) { folderLists ->
                        goSort(folderLists)
                    }

                }


            }.apply {
                list?.let {
                    textDirCount.text = it.size.toString()
                    val sumOf = it.sumOf { it.allFiles.size }
                    textAllCounts.text = sumOf.toString()
                    setData(it)
                }
            }
            recyclerResult.run {
                adapter = myAdapter
                layoutManager = LinearLayoutManager(this@ScanResultDisplayActivity)
            }


        }
    }


    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)
                    textFileType.text = getString(R.string.text_photos)
                }

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

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

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


    }


    private fun goSort(list: ArrayList<ResultPhotosFiles>) {
        startActivity(
            Intent(
                this@ScanResultDisplayActivity,
                PhotoSortingActivity::class.java
            ).apply {
                putExtra(KEY_SCAN_TYPE, scanType)
                putParcelableArrayListExtra(
                    PhotoSortingActivity.KEY_PHOTO_FOLDER_FILE,
                    list
                )
            })
    }
}