package com.xm.test.myfilemaster.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.SparseBooleanArray
import android.view.View
import android.widget.CheckBox
import android.widget.GridView
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.kongzue.dialogx.dialogs.WaitDialog
import com.test.mydemo3.activity.ImageShowActivity
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.adapter.MediaAdapter
import com.xm.test.myfilemaster.adapter.MediaAdapter2
import com.xm.test.myfilemaster.databinding.ActivityMediaBinding
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.util.FileUtil

class MediaActivity : AppCompatActivity() {
    private lateinit var mGridView: GridView
    private lateinit var mTitleBar: View
    private lateinit var mSelectBar: View

    private lateinit var mQuitBtn: ImageView
    private lateinit var mCancelBtn: ImageView
    private lateinit var mDeleteBtn: ImageView
    private lateinit var mEditBtn: ImageView
    private lateinit var mShareBtn: ImageView
    private lateinit var mCheckedBtn: CheckBox
    private lateinit var mSelectTv: TextView
    private lateinit var mTitleTv: TextView

    lateinit var mItemCheckedArray: SparseBooleanArray
    private var mItemDataList = ArrayList<FileBean>()
//    private lateinit var binding: ActivityMediaBinding

        private var mItemAdapter: MediaAdapter? = null
//    private var mItemAdapter: MediaAdapter2? = null
    private var mDataList: ArrayList<String> = ArrayList<String>()
    private var mCheckedDataList: ArrayList<String> = ArrayList<String>()

    private var mCheckedNumber = 0
    private var mFileType: String? = null

    private lateinit var mModel: BaseActivityViewModel

    val mFileTypeBeanList: ArrayList<String> = ArrayList()

    var mShowBeanData: ArrayList<String> = ArrayList()
    var mStartNumber: Int = 0

    var mIsCheckClick = false

    companion object {
        var isClickEditBtn = false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        binding = ActivityMediaBinding.inflate(layoutInflater)
//        setContentView(binding.root)
        setContentView(R.layout.activity_media)

        initView()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initView() {
        mGridView = findViewById(R.id.image_item_grid)
        mTitleBar = findViewById(R.id.title_bar)
        mSelectBar = findViewById(R.id.switch_and_select_layout)
//        mScrollView = findViewById(R.id.grid_view_scroll)

        mQuitBtn = mTitleBar.findViewById<ImageView>(R.id.btn_quit)
        mCancelBtn = mTitleBar.findViewById<ImageView>(R.id.btn_cancel)
        mDeleteBtn = mTitleBar.findViewById<ImageView>(R.id.btn_delete)
        mEditBtn = mTitleBar.findViewById<ImageView>(R.id.btn_edit)
        mShareBtn = mTitleBar.findViewById<ImageView>(R.id.btn_share)
        mCheckedBtn = mTitleBar.findViewById<CheckBox>(R.id.checkbox_item_all)
        mSelectTv = mTitleBar.findViewById<TextView>(R.id.select_num)
        mTitleTv = mTitleBar.findViewById<TextView>(R.id.title_bar_text)


        mEditBtn.visibility = View.VISIBLE
        mShareBtn.visibility = View.GONE
        findViewById<TextView>(R.id.current_file_path).visibility = View.GONE

        mItemCheckedArray = SparseBooleanArray()
        mFileType = intent.getStringExtra("fileType").toString() ?: ""
        mItemAdapter = MediaAdapter(mItemDataList, this, mItemCheckedArray, mFileType!!, 115)
//        mItemAdapter = MediaAdapter2()
        mGridView.adapter = mItemAdapter
//        binding.imageItemGrid.adapter = mItemAdapter
        Thread(Runnable {
            WaitDialog.show("please wait..")
            mItemDataList = FileUtil.getMediaFiles(this, mFileType!!, 50)
            runOnUiThread {
                WaitDialog.dismiss()
                for (fileBean in mItemDataList) {
                    mDataList.add(fileBean.filePath)
                }
                mItemAdapter?.setData(mItemDataList)
            }
        }).start()


        mTitleTv.text = mFileType!!

        mModel = ViewModelProvider(this)[BaseActivityViewModel::class.java]
        mModel.setNumber(0)
        mModel.myInt.observe(this) {
            mSelectTv.text = String.format(getString(R.string.select_0, mCheckedNumber.toString()))
        }
        setOnclickListener()
    }


    private fun setOnclickListener() {
        mEditBtn.setOnClickListener {
            mQuitBtn.visibility = View.GONE
            mEditBtn.visibility = View.GONE
            mSelectBar.visibility = View.VISIBLE
            mDeleteBtn.visibility = View.VISIBLE
            mCancelBtn.visibility = View.VISIBLE

            isClickEditBtn = true
            mItemAdapter?.notifyDataSetChanged()
        }
        mCancelBtn.setOnClickListener {
            cancelBtn()
        }
        mQuitBtn.setOnClickListener {
            finish()
        }
        mCheckedBtn.setOnClickListener {
            mIsCheckClick = !mIsCheckClick
            selectAll()
        }
        mGridView.setOnItemClickListener { parent, view, position, id ->
            if (isClickEditBtn) {
                updateCheckBoxStatus(view, position)
            } else {
                //TODO 点击进入图片或视频播放
                Intent(this, ImageShowActivity::class.java)
                val bundle = Bundle()
                bundle.putSerializable("fileBean", mItemDataList[position])
                intent.putExtras(bundle)
                intent.putExtra("fileType", mFileType)
                startActivity(intent)
            }
        }
    }

    private fun setOnGridViewItemClickListener() {
//        mGridView.setOnItemClickListener { parent, view, position, id ->

//        }
    }

    private fun setStateCheckedMap(isChecked: Boolean) {
        for (i in mDataList.indices) {
            mItemCheckedArray.put(i, isChecked)
            mGridView.setItemChecked(i, isChecked)
        }
    }

    private fun updateCheckBoxStatus(view: View, position: Int) {
        val viewHolder = view.tag as MediaAdapter.ViewHolder
        viewHolder.checkedBox.toggle()
        mGridView.setItemChecked(position, viewHolder.checkedBox.isChecked)
        mItemCheckedArray.put(position, viewHolder.checkedBox.isChecked)
        if (viewHolder.checkedBox.isChecked) {
            mCheckedDataList.add(mDataList[position])
        } else {
            mCheckedDataList.remove(mDataList[position])
        }
        mItemAdapter?.notifyDataSetChanged()
        mCheckedNumber = mCheckedDataList.size
        mModel.setNumber(mCheckedNumber)
    }

    private fun selectAll() {
        mCheckedDataList.clear()
        mCheckedNumber = if (mIsCheckClick) {
            setStateCheckedMap(true)
            mCheckedDataList.addAll(mDataList)
            mCheckedDataList.size
        } else {
            setStateCheckedMap(false)
            0
        }
        mModel.setNumber(mCheckedNumber)
        mItemAdapter?.notifyDataSetChanged()
    }

    override fun onBackPressed() {
        mItemAdapter?.notifyDataSetChanged()
        if (isClickEditBtn) {
            cancelBtn()
        } else {
            super.onBackPressed()
        }
    }

    private fun cancelBtn() {
        mQuitBtn.visibility = View.VISIBLE
        mEditBtn.visibility = View.VISIBLE
        mSelectBar.visibility = View.GONE
        mDeleteBtn.visibility = View.GONE
        mCancelBtn.visibility = View.GONE

        isClickEditBtn = false
        mItemAdapter?.notifyDataSetChanged()
    }
}