package com.test.mydemo3.activity

import android.annotation.SuppressLint
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.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.kongzue.dialogx.dialogs.WaitDialog
import com.test.mydemo3.R
import com.test.mydemo3.adapter.MediaAdapter
import com.test.mydemo3.model.FileBean
import com.test.mydemo3.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 mItemAdapter: MediaAdapter
    private var mDataList: ArrayList<String> = ArrayList<String>()
    private var mCheckedDataList: ArrayList<String> = ArrayList<String>()

    private var mCheckedNumber = 0

    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)
        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()
        val fileType = intent.getStringExtra("fileType").toString()
        mItemAdapter = MediaAdapter(mItemDataList, this, mItemCheckedArray, fileType, 118)
        mGridView.adapter = mItemAdapter
        Thread(Runnable {
            WaitDialog.show("please wait..")
            mItemDataList = FileUtil.getMediaFiles(this, fileType, 20)
            runOnUiThread {
                mItemAdapter.setData(mItemDataList)
                WaitDialog.dismiss()
                for (fileBean in mItemDataList){
                    mDataList.add(fileBean.filePath)
                }
            }
        }).start()


        mTitleTv.text = fileType

        mModel = ViewModelProvider(this)[BaseActivityViewModel::class.java]
        mModel.setNumber(0)
        mModel.myInt.observe(this) {
            mSelectTv.text = String.format(getString(R.string.select_0, mCheckedNumber.toString()))
        }
        setOnGridViewItemClickListener()
        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()
        }
    }

    private fun setOnGridViewItemClickListener(){
        mGridView.setOnItemClickListener { parent, view, position, id ->
            if (isClickEditBtn){
                updateCheckBoxStatus(view, position)
            }else{
                //TODO 点击进入图片或视频播放
            }
        }
    }

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