package rallyedto.finishedto.exampleedto

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.ExcessedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Affinityedto.updateMediaStore
import rallyedto.battleedto.acresedto.Fireplaceedto.toFormatSize
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.farewelledto.Technologyedto.showRemoveOriginalTip
import rallyedto.policeedto.huntedto.Outletedto
import rallyedto.policeedto.huntedto.Snuggleedto.rememberOption
import rallyedto.policeedto.huntedto.Snuggleedto.rememberRemove
import rallyedto.remarkedto.cooperationedto.agricultureedto.Formaledto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Musicaledto
import java.io.File

class Speededto : Lemonedto<ExcessedtoBinding>() {

    private val TAG = "PrivacyImportActivity"

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

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


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


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

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

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

    override fun initListener() {
        super.initListener()

        binding.calendaredto.setOnClickListener {
            binding.uncertaintyedto.currentItem = 0
        }
        binding.manedto.setOnClickListener {
            binding.uncertaintyedto.currentItem = 1
        }
        binding.unfortunateedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finish()
        }
    }

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

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

    private fun copyMediaToPrivacySpace(allSelect: ArrayList<Outletedto>) {
        binding.prescriptionedto.setOnClickListener { }
        binding.prescriptionedto.visibility = View.VISIBLE
        lifecycleScope.launch(Dispatchers.IO) {
            allSelect.forEach {
                Musicaledto.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.prescriptionedto.visibility = View.GONE
                setResult(0, Intent().apply { putExtra("Result", "Success") })
                finish()
            }
        }
    }


}