package com.zxhy.hfilemanagermaster.manager

import android.Manifest
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.FragmentManagerBinding
import com.zxhy.hfilemanagermaster.OverviewActivity
import com.zxhy.hfilemanagermaster.knife.getMountInfoList
import com.zxhy.hfilemanagermaster.permission.IntentLauncher
import com.zxhy.hfilemanagermaster.permission.PermissionLauncher
import com.zxhy.hfilemanagermaster.permission.requestPermission
import java.math.BigDecimal

/**
 * A simple [Fragment] subclass.
 * Use the [ManagerFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ManagerFragment : Fragment() {
    private lateinit var binding: FragmentManagerBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {}
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_manager, container, false)
        binding = FragmentManagerBinding.bind(view.rootView)
        return view
    }

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

        val disk = requireContext().getMountInfoList().first()
        binding.tvFree.text = disk.getTotalContent(requireContext())
        val percentF = BigDecimal(disk.used)
            .divide(BigDecimal(disk.total), 2, BigDecimal.ROUND_HALF_UP)

        binding.indicatorView.setPercent(percentF.toFloat())
        val percent = percentF.multiply(BigDecimal(100)).toInt()
        binding.tvUse.text = "$percent%"

        (requireActivity() as OverviewActivity).showManager()

        binding.ivFile.setOnClickListener {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                val flag = requestPermission(
                    requireContext(),
                    (requireActivity() as OverviewActivity).permissionLauncher,
                    (requireActivity() as OverviewActivity).intentLauncher,
                    arrayOf(Manifest.permission.READ_MEDIA_IMAGES), "设置允许访问媒体图片权限"
                ) {
                    findNavController().navigate(R.id.filesFragment)
                }
                if (flag) findNavController().navigate(R.id.filesFragment)
            }
        }
        binding.ivWord.setOnClickListener { }
        binding.ivExcel.setOnClickListener { }
        binding.ivPdf.setOnClickListener { }
        binding.ivPpt.setOnClickListener { }
        binding.cardView1.setOnClickListener {
            findNavController().navigate(R.id.dupPictureActivity)
        }
        binding.cardView2.setOnClickListener {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                val flag = requestPermission(
                    requireContext(),
                    (requireActivity() as OverviewActivity).permissionLauncher,
                    (requireActivity() as OverviewActivity).intentLauncher,
                    arrayOf(
                        Manifest.permission.READ_MEDIA_IMAGES,
                        Manifest.permission.READ_MEDIA_VIDEO
                    ), "设置允许访问媒体图片视频权限"
                ) {
                    findNavController().navigate(R.id.largeFileActivity)
                }
                if (flag) findNavController().navigate(R.id.largeFileActivity)
            }
        }
        binding.cardView3.setOnClickListener {
//            findNavController().navigate(R.id.emptyFileActivity)
        }

    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ManagerFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

}