package com.test.mydemo3.activity

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.util.SparseBooleanArray
import android.widget.ListView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import com.kongzue.dialogx.dialogs.WaitDialog
import com.test.mydemo3.R
import com.test.mydemo3.base.BaseActivity
import com.test.mydemo3.model.FileBean
import com.test.mydemo3.model.FilesItemBean
import com.test.mydemo3.util.FileUtil
import com.test.mydemo3.util.PermissionUtil
import java.io.File

class InternalStorageActivity : BaseActivity() {
    private lateinit var mFilesItemListView: ListView

    companion object{
        var mFileItemList = ArrayList<FileBean>()
    }
    override fun getLayout(): Int {
        return R.layout.activity_internal_storage
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        super.setTitle("Internal storage")
        mInitView()
    }
    fun mInitView(){
        mFilesItemListView = findViewById<ListView>(R.id.files_item_listview)
        if (HomeActivity.isPermission){
            requestDataList()
        }else{
            setAdapter(mFilesItemListView, mFileItemList)
        }
    }

    private fun requestDataList() {
        if (mFileItemList.size == 0){
            WaitDialog.show("Please Wait..")
            Thread(Runnable {
//                val allFilesAndDirectories = FileUtil.getAllFilesAndDirectories(this)
                val allFilesAndDirectories = FileUtil.getFileList(Environment.getExternalStorageDirectory())
                mFileItemList = FileUtil.fileListSort(allFilesAndDirectories)
                runOnUiThread(Runnable {
                    setAdapter(mFilesItemListView, mFileItemList)
                    WaitDialog.dismiss()
                })
            }).start()
        }else{
            setAdapter(mFilesItemListView, mFileItemList)
        }
    }

    override fun onResume() {
        super.onResume()
        if (PermissionUtil.checkGrantedPermission(this)){
            requestDataList()
        }
    }

}