package com.zxhy.solarmasterace.manager

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.navigation.fragment.findNavController
import com.zxhy.solarmasterace.MainActivity
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.databinding.FragmentManagerBinding
import com.zxhy.solarmasterace.internalstorage.InternalStorageActivity
import com.zxhy.solarmasterace.quicktools.getMountInfoList
import java.math.BigDecimal

class ManagerFragment : Fragment() {

    private lateinit var binding: FragmentManagerBinding
    private lateinit var context: Context

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

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

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        context = requireContext()
        val disk = requireContext().getMountInfoList().first()
        binding.tvUse.text = "Use:" + disk.getUsedContent(requireContext())
        binding.tvFree.text = "Free:" + disk.getFreeContent(requireContext())
        val percentF = BigDecimal(disk.used)
            .divide(BigDecimal(disk.total), 2, BigDecimal.ROUND_HALF_UP)
        val percent = percentF.multiply(BigDecimal(100)).toInt()
        binding.tvPercent.text = "$percent%"

        binding.cardCategory.setOnClickListener {
            categoryMode()
        }
        binding.cardDocuments.setOnClickListener {
            documentsMode()
        }
        binding.ivCircle.setOnClickListener {
            startActivity(Intent(context, InternalStorageActivity::class.java))
        }
        categoryMode()
    }

    private fun categoryMode() {
        binding.apply {
            val txtColo1 = ContextCompat.getColor(context, R.color.white)
            val txtColo2 = ContextCompat.getColor(context, R.color.color_FF5C67E5)

            val bgColor2 = ContextCompat.getColor(context, R.color.color_FF4CE0AB)
            val bgColor1 = ContextCompat.getColor(context, R.color.white)

            cardCategory.setCardBackgroundColor(bgColor2)
            ivCategory.setImageResource(R.mipmap.dsa_897)
            tvCategory.setTextColor(txtColo1)

            cardDocuments.setCardBackgroundColor(bgColor1)
            ivDocuments.setImageResource(R.mipmap.wr_5877)
            tvDocuments.setTextColor(txtColo2)

            clFile.visibility = View.VISIBLE
            clDocuments.visibility = View.GONE

            llImage.setOnClickListener {
                nav("Image")
            }
            llVideo.setOnClickListener {
                nav("Video")
            }
            llAudio.setOnClickListener {
                nav("Audio")
            }
            llApk.setOnClickListener {
                nav("Apk")
            }
            llZip.setOnClickListener {
                nav("Zip")
            }
            llLogFile.setOnClickListener {
                nav("Log file")
            }
        }


    }

    private fun nav(tittle: String) {
        val bundle = Bundle().apply {
            putString("tittle", tittle)
        }
        findNavController().navigate(R.id.fileBrowserActivity, bundle)
    }

    private fun documentsMode() {
        binding.apply {
            val txtColo1 = ContextCompat.getColor(context, R.color.white)
            val txtColo2 = ContextCompat.getColor(context, R.color.color_FF5C67E5)

            val bgColor2 = ContextCompat.getColor(context, R.color.color_FF4CE0AB)
            val bgColor1 = ContextCompat.getColor(context, R.color.white)

            cardCategory.setCardBackgroundColor(bgColor1)
            ivCategory.setImageResource(R.mipmap.dsa_8972)
            tvCategory.setTextColor(txtColo2)

            cardDocuments.setCardBackgroundColor(bgColor2)
            ivDocuments.setImageResource(R.mipmap.wr_587712)
            tvDocuments.setTextColor(txtColo1)

            clFile.visibility = View.GONE
            clDocuments.visibility = View.VISIBLE

            llWord.setOnClickListener {
                nav("Word")
            }
            llExcel.setOnClickListener {
                nav("Excel")
            }
            llPpt.setOnClickListener {
                nav("PPT")
            }
            llPdf.setOnClickListener {
                nav("PDF")
            }
            llTxt.setOnClickListener {
                nav("TXT")
            }
            llAllDoc.setOnClickListener {
                nav("Documents")
            }
        }
    }

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