package com.xm.test.myfilemaster.activity

import android.os.Bundle
import android.os.Environment
import android.widget.ListView
import com.kongzue.dialogx.dialogs.WaitDialog
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.base.BaseActivity
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.util.FileUtil
import com.xm.test.myfilemaster.util.PermissionUtil

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

}