package com.zxhyis.toolss.ui.home

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.ConfigHelper
import com.zxhyis.toolss.cx.FileHelps.toFormatSize
import com.zxhyis.toolss.databinding.FragmentToolsBinding
import com.zxhyis.toolss.ui.AFunOb
import com.zxhyis.toolss.ui.page.NetWorkActivity
import com.zxhyis.toolss.ui.page.PrepareScanActivity
import com.zxhyis.toolss.ui.page.RepeaterdPhotoActivity
import com.zxhyis.toolss.ui.page.SpeakerCleanerActivity
import com.zxhyis.toolss.ui.page.VirusActivity
import com.zxhyis.toolss.ui.page.appmanager.AppManager2Activity
import com.zxhyis.toolss.ui.page.bigfileclean.BigFileCleanActivity
import com.zxhyis.toolss.ui.page.notifaction.NotificationCleanActivity
import com.zxhyis.toolss.ui.page.notifaction.NotificationGuestActivity
import com.zxhyis.toolss.ui.page.photocompress.photo.StartCompressionPhotoActivity
import com.zxhyis.toolss.ui.page.recentusage.RecentAppActivity
import com.zxhyis.toolss.ui.page.speed.BatteryOptimizerActivity

/**
 *
 * */
class ToolsFragment : Fragment() {

    private lateinit var binding: FragmentToolsBinding
    private lateinit var adapter: ToolsAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        adapter = ToolsAdapter(requireActivity()) { kName ->
            when (kName) {
                AFunOb.APP_MANAGER -> {
                    startActivity(Intent(requireActivity(), AppManager2Activity::class.java))
                }

                AFunOb.JUNK_CLEANER -> {
                    startActivity(Intent(requireActivity(), PrepareScanActivity::class.java))
                }

                AFunOb.NOTIFICATION_CLEANER -> {
                    if (SPUtils.getInstance().getBoolean("notification_guest", false)) {
                        startActivity(
                            Intent(
                                requireActivity(),
                                NotificationCleanActivity::class.java
                            )
                        )
                    } else {
                        startActivity(
                            Intent(
                                requireActivity(),
                                NotificationGuestActivity::class.java
                            )
                        )
                    }
                }

                AFunOb.Antivirus -> {
                    startActivity(Intent(requireActivity(), VirusActivity::class.java))
                }

                AFunOb.RECENT_APP_USAGE -> {
                    startActivity(Intent(requireActivity(), RecentAppActivity::class.java))
                }

                AFunOb.NETWORK_TRAFFIC -> {
                    startActivity(Intent(requireActivity(), NetWorkActivity::class.java))
                }

                AFunOb.LARGE_FILE_CLEANER -> {
                    startActivity(Intent(requireActivity(), BigFileCleanActivity::class.java))
                }

                AFunOb.SIMILAR_PHOTOS -> {
                    startActivity(Intent(requireActivity(), RepeaterdPhotoActivity::class.java))
                }

                AFunOb.SPEAK_CLEANER -> {
                    startActivity(Intent(requireActivity(), SpeakerCleanerActivity::class.java))
                }

                AFunOb.PHOTO_COMPRESS -> {
                    startActivity(Intent(requireActivity(), StartCompressionPhotoActivity::class.java))
                }

                AFunOb.APPP_LOCK -> {
                    startActivity(Intent(requireActivity(), BatteryOptimizerActivity::class.java))
                }
            }
        }
        binding.rvTools.adapter = adapter
    }


    override fun onResume() {
        super.onResume()
        val split = ConfigHelper.junkSizeClean.toFormatSize(1).split(' ')
        binding.tvSize.text = split[0]
        binding.tvSizeUnit.text = split[1]
    }

}