package com.xm.test.myfilemaster.activity

import android.Manifest
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.callback.RequestCallback
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.fragment.FileManagerFragment
import com.xm.test.myfilemaster.fragment.HomeFragment
import com.xm.test.myfilemaster.fragment.InternalStorageFragment
import com.xm.test.myfilemaster.util.PermissionUtil

class HomeActivity : AppCompatActivity() {
    private val REQUEST_MANAGE_EXTERNAL_STORAGE_PERMISSION = 101

    var mViewPage: ViewPager2? = null
    companion object{
        var mRadioGroup: RadioGroup? = null
        var isPermission = false
        fun groupShow(isShow: Boolean){
            mRadioGroup?.visibility = if (isShow) View.VISIBLE else View.GONE
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_home)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initView()
    }

    private fun initView() {
        mViewPage = findViewById(R.id.view_page)
        mRadioGroup = findViewById(R.id.home_btn_group)
        mViewPage?.isUserInputEnabled = false
        var list = ArrayList<Fragment>()
        list.add(HomeFragment())
        list.add(InternalStorageFragment())
        list.add(FileManagerFragment())
        getPermission()

        this.mViewPage?.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return list.size
            }

            override fun createFragment(position: Int): Fragment {
                return list[position]
            }
        }

        this.mViewPage?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {

                super.onPageSelected(position)
                if (position ==0){
                    findViewById<RadioButton>(R.id.radio_home_btn).isChecked = true
                    findViewById<RadioButton>(R.id.radio_file_manage_btn).isChecked = false
                    findViewById<RadioButton>(R.id.radio_view_btn).isChecked = false
                }else if (position == 1){
                    findViewById<RadioButton>(R.id.radio_home_btn).isChecked = false
                    findViewById<RadioButton>(R.id.radio_view_btn).isChecked = true
                    findViewById<RadioButton>(R.id.radio_file_manage_btn).isChecked = false
                }else if (position == 2){
                    findViewById<RadioButton>(R.id.radio_home_btn).isChecked = false
                    findViewById<RadioButton>(R.id.radio_view_btn).isChecked = false
                    findViewById<RadioButton>(R.id.radio_file_manage_btn).isChecked = true

                }

            }
        })

        mRadioGroup?.setOnCheckedChangeListener{
                _group, _id ->
            when(_id){
                R.id.radio_home_btn->{
                    mViewPage?.currentItem = 0

                }
                R.id.radio_view_btn ->{
                    mViewPage?.currentItem = 1
                }
                R.id.radio_file_manage_btn->{
                    mViewPage?.currentItem = 2
                }
            }
        }
    }

    fun getPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){
            isPermission = false
        }else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R && !isPermission){
            isPermission = false
        } else {
            PermissionX.init(this).permissions(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
            ).request(RequestCallback { allGranted, grantedList, deniedList ->
                if (allGranted){
                    isPermission = true
                }else{
                    isPermission = false
                }
            })
        }
    }

    override fun onResume() {
        super.onResume()
        if (PermissionUtil.checkGrantedPermission(this)){
            isPermission = true
        }
    }

}