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

import android.content.Intent
import android.view.LayoutInflater
import androidx.activity.OnBackPressedCallback
import androidx.activity.viewModels
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.db.ResultData
import com.ux.video.file.filerecovery.db.ResultDataFiles
import com.ux.video.file.filerecovery.sort.SortingActivity
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.FileType
import com.ux.video.file.filerecovery.utils.ScanType

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

    private lateinit var list: List<ResultData>
    private val viewModel: ScanningResultDisplayViewModel by viewModels()


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

    override fun initView() {
        super.initView()
        val intExtra = intent.getIntExtra(Common.KEY_SCAN_TYPE, ScanType.ALL_PHOTO.value)
        scanType = ScanType.from(intExtra)!!
        setSelectTypeTitle(scanType.mediaType)
    }

    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.mediaType) {
                FileType.AUDIO,  FileType.DOCUMENT -> {
                    bottomLayout.setBackgroundResource(R.drawable.bg_rectangle_white_top_20)
                    ScanResultDocumentsAudioAdapter(
                        this@ScanResultDisplayActivity,
                        scanType.mediaType
                    ) { folderLists ->
                        goSort(folderLists)
                    }
                }

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

                }
            }
            viewModel.scanData.observe(this@ScanResultDisplayActivity){ data->
                list = data
                list.let { data ->
                    textDirCount.text = data.size.toString()
                    val sumOf = data.sumOf { it.allFiles.size }
                    textAllCounts.text = sumOf.toString()
                    myAdapter.setData(data)
                }
            }
            recyclerResult.run {
                adapter = myAdapter
                layoutManager = LinearLayoutManager(this@ScanResultDisplayActivity)
            }
        }
    }
    private fun dealExit() {
        exitDialog = exitDialog ?: ExitDialogFragment {
            finish()
        }
        exitDialog?.show(supportFragmentManager, "")
    }

    private fun setSelectTypeTitle(fileType: FileType) {
        binding.run {
            when (fileType) {
                FileType.PHOTO -> {
                    title.text = getString(R.string.photo_title)
                    textFileType.text = getString(R.string.text_photos)
                }

                FileType.VIDEO-> {
                    title.text = getString(R.string.video_title)
                    textFileType.text = getString(R.string.text_videos)
                }

                FileType.AUDIO -> {
                    title.text = getString(R.string.audio_title)
                    textFileType.text = getString(R.string.text_audios)
                }

                FileType.DOCUMENT -> {
                    title.text = getString(R.string.document_title)
                    textFileType.text = getString(R.string.text_documents)
                }
            }
        }


    }


    private fun goSort(list: ArrayList<ResultDataFiles>) {
        viewModel.updateSortData(list)
        startActivity(
            Intent(
                this@ScanResultDisplayActivity,
                SortingActivity::class.java
            ).apply {
                putExtra(Common.KEY_FILE_TYPE, scanType.mediaType.value)
            })
    }
}