package com.test.mydemo3.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore.Audio.Radio
import android.provider.Settings
import android.view.View
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.callback.RequestCallback
import com.test.mydemo3.R
import com.test.mydemo3.fragment.FileManagerFragment
import com.test.mydemo3.fragment.HomeFragment
import com.test.mydemo3.fragment.InternalStorageFragment
import com.test.mydemo3.util.PermissionUtil
import java.security.Permission
import javax.security.auth.callback.Callback

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
        }
    }

}