package importantedto.appliededto.surgeonedto.promiseedto.respectableedto.prisonedto.comfortableedto

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.datarecovery.databinding.DonoredtoBinding
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.afteredto.constitutionaledto.Memberedto.updateMediaStore
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.candidateedto.Flawededto
import importantedto.concreteedto.cameedto.Hateedto.rememberOption
import importantedto.concreteedto.cameedto.Hateedto.rememberRemove
import importantedto.concreteedto.cameedto.Vigorousedto
import importantedto.gallonedto.Transcendedto
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showRemoveOriginalTip
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class Hopeedto : Murderedto<DonoredtoBinding>() {

    private val TAG = "PrivacyImportActivity"

    private val photosImportFragment: Flawededto by lazy {
        Flawededto("Photos")
    }
    private val videosImportFragment: Flawededto by lazy {
        Flawededto("Videos")
    }

    private val fragments by lazy {
        mutableListOf(photosImportFragment, videosImportFragment)
    }


    override val binding: DonoredtoBinding by lazy {
        DonoredtoBinding.inflate(layoutInflater)
    }


    override fun initView() {
        Transcendedto.setStatusBarLightMode(this, true)
        Transcendedto.setStatusBarColor(this, Color.TRANSPARENT)

        binding.relatededto.run {
            adapter = object : FragmentStateAdapter(this@Hopeedto) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.relatededto.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    binding.headededto.isSelected = true
                    binding.seasonaledto.isSelected = false
                } else {
                    binding.seasonaledto.isSelected = true
                    binding.headededto.isSelected = false
                }
            }
        })
    }

    override fun initListener() {
        super.initListener()

        binding.headededto.setOnClickListener {
            binding.relatededto.currentItem = 0
        }
        binding.seasonaledto.setOnClickListener {
            binding.relatededto.currentItem = 1
        }
        binding.bangedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finish()
        }
    }

    private val photosPath = arrayListOf<Vigorousedto>()
    private val videosPath = arrayListOf<Vigorousedto>()

    @SuppressLint("SetTextI18n")
    fun showCardImport(type: String, list: List<Vigorousedto>) {
        if (type == "Photos") {
            photosPath.clear()
            photosPath.addAll(list)
        } else {
            videosPath.clear()
            videosPath.addAll(list)
        }
        val allSelect = arrayListOf<Vigorousedto>()
        allSelect.addAll(photosPath)
        allSelect.addAll(videosPath)
        if (allSelect.isNotEmpty()) {
            binding.messedto.isVisible = true
            binding.belongedto.text = "${allSelect.size} Selected"
            binding.ignoranceedto.text = allSelect.sumOf { File(it.path).length() }.toFormatSize()
            binding.districtedto.setOnClickListener {
                if (!rememberOption) {
                    showRemoveOriginalTip { isRememberOption, isRemove ->
                        rememberOption = isRememberOption
                        rememberRemove = isRemove
                        copyMediaToPrivacySpace(allSelect)
                    }
                } else {
                    copyMediaToPrivacySpace(allSelect)
                }
            }
        } else {
            binding.districtedto.setOnClickListener {}
            binding.messedto.isVisible = false
        }
    }

    private fun copyMediaToPrivacySpace(allSelect: ArrayList<Vigorousedto>) {
        binding.hobbyedto.setOnClickListener { }
        binding.hobbyedto.visibility = View.VISIBLE
        lifecycleScope.launch(Dispatchers.IO) {
            allSelect.forEach {
                Tumbleedto.logDebug(TAG, it.mimeType)
                val dir = filesDir
                val file = File(it.path)

                runCatching {
                    if (it.mimeType.contains("image")) {
                        val imageDir = File(dir, "photos")
                        file.copyTo(File(imageDir, file.name), true)
                    } else {
                        val videoDir = File(dir, "videos")
                        file.copyTo(File(videoDir, file.name), true)
                    }
                    if (rememberRemove) {
                        file.delete()
                    }
                }
            }
            updateMediaStore()

            launch(Dispatchers.Main) {
                binding.hobbyedto.visibility = View.GONE
                setResult(0, Intent().apply { putExtra("Result", "Success") })
                finish()
            }
        }
    }


}