package com.base.pdfreaderallpdfreader.ui.main.home

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.base.pdfreaderallpdfreader.bean.ConstObject.DO_LOCK_PDF
import com.base.pdfreaderallpdfreader.bean.ConstObject.DO_SPLIT_PDF
import com.base.pdfreaderallpdfreader.databinding.FragmentHomeBinding
import com.base.pdfreaderallpdfreader.ui.document.DocumentActivity
import com.base.pdfreaderallpdfreader.ui.main.MainActivity
import com.base.pdfreaderallpdfreader.ui.main.getPdfFastSize
import com.base.pdfreaderallpdfreader.ui.pdf.PdfActivity
import com.base.pdfreaderallpdfreader.ui.pdf.PdfSelectActivity
import com.base.pdfreaderallpdfreader.ui.view.MainDialog.showStoragePermission
import com.base.pdfreaderallpdfreader.utils.PermissionUtils.checkStorePermission
import com.base.pdfreaderallpdfreader.utils.PermissionUtils.requestStoragePermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        return root
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val launcher = (requireActivity() as MainActivity).launcher

        binding.clPdfReader.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), DocumentActivity::class.java))
            }
        }
        binding.llEditPdf.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), DocumentActivity::class.java).apply {
                    putExtra("initTab", 1)
                })
            }

        }
        binding.llMergePdf.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                PdfActivity.jumpMerge(requireActivity())
            }
        }
        binding.llSplitPdf.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
                    putExtra("doWhat", DO_SPLIT_PDF)
                })
            }
        }
        binding.llLockPdf.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), PdfSelectActivity::class.java).apply {
                    putExtra("doWhat", DO_LOCK_PDF)
                })
            }
        }
        binding.llWord.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), DocumentActivity::class.java).apply {
                    putExtra("initTab", 2)
                })
            }
        }
        binding.llExcel.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), DocumentActivity::class.java).apply {
                    putExtra("initTab", 3)
                })
            }
        }
        binding.llPpt.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), DocumentActivity::class.java).apply {
                    putExtra("initTab", 4)
                })
            }
        }

        binding.llOther.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                requireContext().showStoragePermission(launcher)
            } else {
                startActivity(Intent(requireContext(), DocumentActivity::class.java).apply {
                    putExtra("initTab", 0)
                })
            }
        }


        if (requireContext().checkStorePermission()) {
            lifecycleScope.launch(Dispatchers.IO) {
                val size = getPdfFastSize(requireContext())
                launch(Dispatchers.Main) {
                    binding.tvFileNumber.text = "$size Files"
                }
            }
        } else {
            binding.ivArrow.visibility = View.INVISIBLE
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}