package com.ux.video.file.filerecovery.recovery

import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.android.material.tabs.TabLayoutMediator
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.base.BaseActivity
import com.ux.video.file.filerecovery.databinding.ActivityRecoveryBinding
import com.ux.video.file.filerecovery.db.ObjectBoxManager
import com.ux.video.file.filerecovery.recovery.ui.MyViewPage2Adapter
import com.ux.video.file.filerecovery.recovery.ui.recoveryphoto.RecoveredFragment
import com.ux.video.file.filerecovery.recovery.ui.recoveryphoto.RecoveryPhotoViewModel
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.CustomTextView
import com.ux.video.file.filerecovery.utils.FileType
import com.ux.video.file.filerecovery.utils.ScanManager
import kotlinx.coroutines.launch

class RecoveryActivity : BaseActivity<ActivityRecoveryBinding>() {
    val sharedViewModel: RecoveryPhotoViewModel by viewModels()
    private lateinit var fileType: FileType

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


    override fun initView() {
        super.initView()
        val intExtra = intent.getIntExtra(Common.KEY_FILE_TYPE, FileType.PHOTO.value)
        fileType = FileType.from(intExtra)!!
        binding.run {
            val fragments = listOf(
                RecoveredFragment.newInstance(FileType.PHOTO.value),
                RecoveredFragment.newInstance(FileType.VIDEO.value),
                RecoveredFragment.newInstance(FileType.AUDIO.value),
                RecoveredFragment.newInstance(FileType.DOCUMENT.value)
            )
            viewPage2.adapter = MyViewPage2Adapter(this@RecoveryActivity, fragments)
            TabLayoutMediator(tabLayout, viewPage2) { tab, position ->
                val tabView = LayoutInflater.from(this@RecoveryActivity)
                    .inflate(R.layout.tab_layout_item, null)
                val tvTitle = tabView.findViewById<CustomTextView>(R.id.tab_item_title)
                val tvCount = tabView.findViewById<CustomTextView>(R.id.tab_item_count)
                tvCount.text = getString(R.string.text_counts, 0)
                when (position) {
                    0 -> {
                        tvTitle.text = getString(R.string.text_photos)
                    }

                    1 -> {
                        tvTitle.text = getString(R.string.text_videos)
                    }

                    2 -> {
                        tvTitle.text = getString(R.string.text_audios)
                    }

                    3 -> {
                        tvTitle.text = getString(R.string.text_documents)
                    }
                }

                tab.customView = tabView
            }.attach()
            tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {

                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {

                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })

            ScanManager.showLog("-----tt-------", "-------fileType=${fileType}")
            fileType.run {
                when (this) {
                    FileType.PHOTO -> viewPage2.currentItem = 0
                    FileType.VIDEO -> viewPage2.currentItem = 1
                    FileType.AUDIO -> viewPage2.currentItem = 2
                    FileType.DOCUMENT -> viewPage2.currentItem = 3
                }
            }


        }
    }

    override fun initData() {
        super.initData()
        binding.imageBack.setOnClickListener {
            finish()
        }
        sharedViewModel.photoFiles.observe(this) {
            setTabCount( FileType.PHOTO, it.size)
        }
        sharedViewModel.videoFiles.observe(this) {
            setTabCount( FileType.VIDEO, it.size)
        }
        sharedViewModel.audioFiles.observe(this) {
            setTabCount( FileType.AUDIO, it.size)
        }
        sharedViewModel.documentsFiles.observe(this) {
            setTabCount(FileType.DOCUMENT, it.size)
        }
        lifecycleScope.launch {
            FileType.entries.forEach{ fileType->
                val recoveryFilePhoto = ObjectBoxManager.queryRecoveryFileAsync(fileType)
                sharedViewModel.setRecoveredFiles(fileType, recoveryFilePhoto)
                setTabCount(fileType, recoveryFilePhoto.size)

            }
        }
    }


    private fun setTabCount(fileType: FileType, number: Int) {
        binding.tabLayout.run {
            getTabAt(fileType.tabIndex)?.customView?.findViewById<CustomTextView>(R.id.tab_item_count)?.text =
                getString(R.string.text_counts, number)
//            when (fileType) {
//                FileType.PHOTO -> getTabAt(0)?.customView?.findViewById<CustomTextView>(R.id.tab_item_count)?.text =
//                    getString(R.string.text_counts, number)
//
//                FileType.VIDEO -> getTabAt(1)?.customView?.findViewById<CustomTextView>(R.id.tab_item_count)?.text =
//                    getString(R.string.text_counts, number)
//
//                FileType.AUDIO -> getTabAt(2)?.customView?.findViewById<CustomTextView>(R.id.tab_item_count)?.text =
//                    getString(R.string.text_counts, number)
//
//                FileType.DOCUMENT -> getTabAt(3)?.customView?.findViewById<CustomTextView>(
//                    R.id.tab_item_count
//                )?.text =
//                    getString(R.string.text_counts, number)
//            }
        }
    }
}