package com.zxhyis.toolss.ui.page.recyclebin


import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.zxhyis.toolss.R
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.ActivityRecycleBinBinding
import com.zxhyis.toolss.logic.Counts
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.logic.setStatusBarTextDark
import com.zxhyis.toolss.logic.showToast

class RecycleBinActivity : TopActivity<ActivityRecycleBinBinding>() {

    private lateinit var f1: PictureFragment
    private lateinit var f2: VideoFragment
    private lateinit var f3: DocFragment

    private val result =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {}

    override fun getViewBinding1(): ActivityRecycleBinBinding {
        return ActivityRecycleBinBinding.inflate(layoutInflater)
    }

    override fun init() {
        vb.inBin.tvTitle.text = getString(R.string.recycle_bin)
        vb.inBin.tvMenu.visibility = View.VISIBLE
        initVp2()
        vb.inBin.ivBack.setOnClick(javaClass.simpleName) {
            finish()
        }

        vb.tvSet.setOnClick(javaClass.simpleName) {
            vb.tvSet.setBackgroundResource(R.drawable.bg_shape_set_click)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                result.launch(Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION))
            } else {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ), Counts.REQUEST_ALL_FILE
                )
            }
        }

        vb.inBin.tvMenu.setOnClick(javaClass.simpleName) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {
                    vb.llBottom.visibility = View.VISIBLE
                } else {
                    "Please apply for permission".showToast()
                }
            } else {
                if (ContextCompat.checkSelfPermission(
                        this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    "Please apply for permission".showToast()
                } else {
                    vb.llBottom.visibility = View.VISIBLE
                }
            }
        }

        vb.btDelete.setOnClick(javaClass.simpleName) {
            f1.deleteImages()
        }

        vb.btRecovery.setOnClick(javaClass.simpleName) {
            f1.recoveryImages()
        }

    }

    override fun onResume() {
        super.onResume()
        setStatusBarTextDark(this)

        vb.tvSet.setBackgroundResource(R.drawable.bg_shape_set)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                closeUI()
            } else {
                showUI()
            }
        } else {
            if (ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                showUI()
            } else {
                closeUI()
            }
        }
    }

    private fun closeUI() {
        vb.apply {
            llTop.visibility = View.VISIBLE
            vp2.visibility = View.VISIBLE
            tvSet.visibility = View.GONE
            tvDes.visibility = View.GONE
        }
    }

    private fun showUI() {
        vb.apply {
            llTop.visibility = View.GONE
            llBottom.visibility = View.GONE
            vp2.visibility = View.GONE
            tvSet.visibility = View.VISIBLE
            tvDes.visibility = View.VISIBLE
        }
    }

    private fun initVp2() {
        f1 = PictureFragment()
        f2 = VideoFragment()
        f3 = DocFragment()
        val fList = listOf(f1, f2, f3)
        vb.vp2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = fList.size

            override fun createFragment(position: Int): Fragment = fList[position]

        }
        vb.vp2.apply {
            isUserInputEnabled = false
            offscreenPageLimit = fList.size
            currentItem = 0
            vb.btPicture.setBackgroundResource(R.drawable.bg_shape_result)
            vb.btPicture.setTextColor(Color.WHITE)
            vb.btVideo.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btVideo.setTextColor(getColor(R.color.bin))
            vb.btDoc.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btDoc.setTextColor(getColor(R.color.bin))
            vb.vp2.setCurrentItem(0, false)
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> {
                            vb.vp2.setCurrentItem(0, false)
                        }

                        1 -> {
                            vb.vp2.setCurrentItem(1, false)
                        }

                        2 -> {
                            vb.vp2.setCurrentItem(2, false)
                        }
                    }
                }
            })

        }

        vb.btPicture.setOnClick(javaClass.simpleName) {
            vb.btPicture.setBackgroundResource(R.drawable.bg_shape_result)
            vb.btPicture.setTextColor(Color.WHITE)


            vb.btVideo.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btVideo.setTextColor(getColor(R.color.bin))

            vb.btDoc.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btDoc.setTextColor(getColor(R.color.bin))
            vb.vp2.setCurrentItem(0, false)
        }

        vb.btVideo.setOnClick(javaClass.simpleName) {
            vb.btVideo.setBackgroundResource(R.drawable.bg_shape_result)
            vb.btVideo.setTextColor(Color.WHITE)

            vb.btPicture.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btPicture.setTextColor(getColor(R.color.bin))

            vb.btDoc.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btDoc.setTextColor(getColor(R.color.bin))
            vb.vp2.setCurrentItem(1, false)
        }

        vb.btDoc.setOnClick(javaClass.simpleName) {
            vb.btDoc.setBackgroundResource(R.drawable.bg_shape_result)
            vb.btDoc.setTextColor(Color.WHITE)

            vb.btPicture.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btPicture.setTextColor(getColor(R.color.bin))

            vb.btVideo.setBackgroundResource(R.drawable.bg_shape_bin_unselect)
            vb.btVideo.setTextColor(getColor(R.color.bin))
            vb.vp2.setCurrentItem(2, false)

        }


    }


}