package com.zxhy.hfilemanagermaster.manager

import android.annotation.SuppressLint
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.requestStoreFollow
import java.math.BigDecimal

/**
 */
@Suppress("DEPRECATION")
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 {
            val bundle = Bundle().apply {
                putString("tittle", "Document")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivWord.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "Word")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivExcel.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "Excel")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivPdf.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "PDF")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivPpt.setOnClickListener {

            val bundle = Bundle().apply {
                putString("tittle", "PPT")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.cardView1.setOnClickListener {
            findNavController().navigate(R.id.dupPictureActivity)
        }
        binding.cardView2.setOnClickListener {
            findNavController().navigate(R.id.largeFileActivity)
        }
        binding.cardView3.setOnClickListener {
            findNavController().navigate(R.id.emptyFileActivity)
        }
        binding.tvManage.setOnClickListener {
            findNavController().navigate(R.id.internalStorageActivity)
        }

        requireContext().requestStoreFollow(
            (requireActivity() as OverviewActivity).permissionLauncher,
            (requireActivity() as OverviewActivity).intentLauncher,
            disAgreeAction = {
            },
            agreeAction = {
            })
    }


    companion object {
        @JvmStatic
        fun newInstance() =
            ManagerFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

}