package com.test.mydemo3.activity

import android.os.Bundle
import android.util.Log
import android.util.SparseBooleanArray
import android.view.View
import android.widget.LinearLayout
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import com.test.mydemo3.R
import com.test.mydemo3.adapter.FilesItemViewAdapter
import com.test.mydemo3.base.BaseActivity
import com.test.mydemo3.fragment.InternalStorageFragment
import com.test.mydemo3.model.FileBean
import com.test.mydemo3.util.FileUtil

class MoveOrCopyActivity : ComponentActivity() {

    var mListView: ListView? = null
    var mItemAdapter: FilesItemViewAdapter? = null
    var mCurrentBean: FileBean? = null
    var mItemDatas: ArrayList<FileBean>? = null
    var mFilesItemLists = ArrayList<ArrayList<FileBean>>()
    lateinit var mCurrantFilePathTv: TextView
    lateinit var mNotFileFoundLayout: View
    var mCurrentBeans = ArrayList<FileBean>()
    var mCheckedItems = ArrayList<FileBean>()
    var mOperate = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_move_or_copy)
        initView()
        setTitle("$mOperate To..")

        setViewOnClickListener()
        setAdapter()
    }

    private fun setViewOnClickListener() {
        findViewById<View>(R.id.btn_quit).setOnClickListener {
            if(backQuit()) finish()
        }

        findViewById<View>(R.id.btn_cancel_bottom).setOnClickListener(View.OnClickListener {
            finish()
        })
        findViewById<View>(R.id.btn_move_bottom).setOnClickListener(View.OnClickListener {
            var moveFile = if (mCurrentBean != null) {
                FileUtil.moveFile(mCheckedItems[0].filePath, mCurrentBean!!.filePath)
            } else {
                FileUtil.moveFile(mCheckedItems[0].filePath, FileUtil.EXTERNAL_STORAGE)
            }
            if (moveFile) {
                Toast.makeText(this, "move success!", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "move failed!", Toast.LENGTH_LONG).show()
            }
            finish()
        })

        findViewById<View>(R.id.btn_copy_bottom).setOnClickListener(View.OnClickListener {
            var moveFile = if (mCurrentBean != null) {
                FileUtil.copyFile(mCheckedItems[0].filePath, mCurrentBean!!.filePath)
            } else {
                FileUtil.copyFile(mCheckedItems[0].filePath, FileUtil.EXTERNAL_STORAGE)
            }
            if (moveFile) {
                Toast.makeText(this, "copy success!", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "copy failed!", Toast.LENGTH_LONG).show()
            }
            finish()
        })
    }

    private fun initView() {
        mItemDatas = InternalStorageFragment.mFileDataBean

        mCheckedItems = intent.getSerializableExtra("checkedItemBeans") as ArrayList<FileBean>
        mOperate = intent.getStringExtra("operate").toString()

        mListView = findViewById(R.id.files_item_listview)
        mCurrantFilePathTv = findViewById(R.id.current_file_path)
        mNotFileFoundLayout = findViewById(R.id.not_file_found_layout)
        findViewById<View>(R.id.btn_share).visibility = View.GONE
        findViewById<View>(R.id.btn_new).visibility = View.GONE
        findViewById<View>(R.id.btn_edit).visibility = View.GONE
        findViewById<View>(R.id.btn_cancel).visibility = View.GONE
        findViewById<View>(R.id.btn_quit).visibility = View.VISIBLE
        findViewById<View>(R.id.bottom_btn_group).visibility = View.VISIBLE
        findViewById<View>(R.id.btn_rename_bottom).visibility = View.GONE
        findViewById<View>(R.id.btn_copy_bottom).visibility =
            if (mOperate.equals("copy")) View.VISIBLE else View.GONE
        findViewById<View>(R.id.btn_move_bottom).visibility =
            if (mOperate.equals("move")) View.VISIBLE else View.GONE
        findViewById<View>(R.id.btn_delete_bottom).visibility = View.GONE
        findViewById<View>(R.id.btn_cancel_bottom).visibility = View.VISIBLE
    }

    fun setAdapter(){
        if (mItemDatas?.size!! > 0){
            mNotFileFoundLayout.visibility = View.GONE
            mListView?.visibility = View.VISIBLE
        }
        val sparseBooleanArray = SparseBooleanArray()
        mItemAdapter = FilesItemViewAdapter(mItemDatas!!, this, sparseBooleanArray)
        mListView?.adapter = mItemAdapter
        mItemAdapter?.setEditClickState(false)
        setOnListViewItemClickListener()

    }
    fun setOnListViewItemClickListener(){
        findViewById<ListView>(R.id.files_item_listview).setOnItemClickListener { parent, view, position, id ->
            mCurrentBean = mItemDatas?.get(position)
            if (!mCurrentBean?.fileType.equals("dir")){
                return@setOnItemClickListener
            }
            mFilesItemLists.add(mItemDatas!!)
            mCurrentBeans.add(mCurrentBean!!)
            mCurrantFilePathTv.text = "Internal Storage/ ${mCurrentBean?.fileName}"
            if (mCurrentBean?.fileType.equals("dir") && mCurrentBean?.childList?.size!! > 0){
                Thread(Runnable {
                    runOnUiThread {
                        mItemDatas = mCurrentBean!!.childList
                        val fileListSort = FileUtil.fileListSort(mItemDatas!!)
                        mItemAdapter?.setData(fileListSort)
                    }
                }).start()
            }else if(mCurrentBean?.fileType.equals("dir") && mCurrentBean?.childList?.size == 0){
                mListView?.visibility = View.GONE
                mNotFileFoundLayout.visibility = View.VISIBLE
            }
        }
    }
    override fun onDestroy() {
        if(backQuit()) finish()
        mItemAdapter?.setEditClickState(true)
        super.onDestroy()
//        BaseActivity.isClickEditBtn = true
    }
    fun backQuit(): Boolean{
        val size = mFilesItemLists.size
        if (size-1 == 0){
            mCurrantFilePathTv.text = "Internal Storage/"
        }
        if (size < 1){
            if (mNotFileFoundLayout.isVisible && mItemDatas?.size!! > 0){
                mListView?.visibility = View.VISIBLE
                mNotFileFoundLayout.visibility = View.GONE
                mItemAdapter?.setData(mItemDatas!!)
                return false
            }
            mCurrentBeans.clear()
            return true
        }else{
            mItemAdapter?.setData(mFilesItemLists[size-1])
            mItemDatas = mFilesItemLists[size-1]
            mListView?.visibility = View.VISIBLE
            mNotFileFoundLayout.visibility = View.GONE
            mFilesItemLists.remove(mFilesItemLists[size-1])
            mCurrentBeans.remove(mCurrentBeans[size-1])
            if (size - 1 > 0){
                mCurrentBean = mCurrentBeans[size-2]
                mCurrantFilePathTv.text = "Internal Storage/ ${mCurrentBean?.fileName}"
            }else{
                mCurrantFilePathTv.text = "Internal Storage/"
            }
            return false
        }
    }

    override fun onBackPressed() {
        if (backQuit()){
            super.onBackPressed()
        }
    }
}