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.requestStoreFollow
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 {

            requireContext().requestStoreFollow(
                (requireActivity() as OverviewActivity).permissionLauncher,
                (requireActivity() as OverviewActivity).intentLauncher
            ) {
                findNavController().navigate(R.id.filesFragment)
            }
        }
        binding.ivWord.setOnClickListener { }
        binding.ivExcel.setOnClickListener { }
        binding.ivPdf.setOnClickListener { }
        binding.ivPpt.setOnClickListener { }
        binding.cardView1.setOnClickListener {
            requireContext().requestStoreFollow(
                (requireActivity() as OverviewActivity).permissionLauncher,
                (requireActivity() as OverviewActivity).intentLauncher
            ) {
                findNavController().navigate(R.id.dupPictureActivity)
            }
        }
        binding.cardView2.setOnClickListener {

            requireContext().requestStoreFollow(
                (requireActivity() as OverviewActivity).permissionLauncher,
                (requireActivity() as OverviewActivity).intentLauncher
            ) {
                findNavController().navigate(R.id.largeFileActivity)
            }
        }
        binding.cardView3.setOnClickListener {
            requireContext().requestStoreFollow(
                (requireActivity() as OverviewActivity).permissionLauncher,
                (requireActivity() as OverviewActivity).intentLauncher
            ) {
                findNavController().navigate(R.id.emptyFileActivity)
            }
        }

        binding.tvManage.setOnClickListener {
            findNavController().navigate(R.id.internalStorageActivity)
        }


    }

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

}