package rowedto.jockeyedto.sheetedto.atticedto.harboredto

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.ChaosedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showRemoveOriginalTip
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Cunningedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.rememberOption
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.rememberRemove
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Affairedto.updateMediaStore
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Suitcaseedto.toFormatSize
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.slideedto.Branchedto
import java.io.File

class Princeedto : Crossingedto<ChaosedtoBinding>() {

    private val TAG = "PrivacyImportActivity"

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

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


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


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

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

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

    override fun initListener() {
        super.initListener()

        binding.slotedto.setOnClickListener {
            binding.guiltedto.currentItem = 0
        }
        binding.scatteredto.setOnClickListener {
            binding.guiltedto.currentItem = 1
        }
        binding.conditionedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finish()
        }
    }

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

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

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


}