package com.base.pdfreader2.ui.view

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.base.pdfreader2.R
import com.base.pdfreader2.bean.ConstObject.UI_SORT_FINAL_MODIFIED
import com.base.pdfreader2.bean.ConstObject.UI_SORT_LAST_MODIFIED
import com.base.pdfreader2.bean.ConstObject.UI_SORT_NAME_A_Z
import com.base.pdfreader2.bean.ConstObject.UI_SORT_NAME_Z_A
import com.base.pdfreader2.databinding.DialogSortBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

object DialogView {

    var SortMode = UI_SORT_LAST_MODIFIED
    fun Context.showSortDialog(okAction: () -> Unit) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogSortBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        val showSortUI: (mode: String) -> Unit = { mode ->
            when (mode) {
                UI_SORT_LAST_MODIFIED -> {
                    binding.iv1.visibility = View.VISIBLE
                    binding.tv1.setTextColor(Color.parseColor("#00B8DE"))
                }

                UI_SORT_FINAL_MODIFIED -> {
                    binding.iv2.visibility = View.VISIBLE
                    binding.tv2.setTextColor(Color.parseColor("#00B8DE"))
                }

                UI_SORT_NAME_A_Z -> {
                    binding.iv3.visibility = View.VISIBLE
                    binding.tv3.setTextColor(Color.parseColor("#00B8DE"))
                }

                UI_SORT_NAME_Z_A -> {
                    binding.iv4.visibility = View.VISIBLE
                    binding.tv4.setTextColor(Color.parseColor("#00B8DE"))
                }
            }
        }
        val resetUI = {
            binding.iv1.visibility = View.INVISIBLE
            binding.iv2.visibility = View.INVISIBLE
            binding.iv3.visibility = View.INVISIBLE
            binding.iv4.visibility = View.INVISIBLE

            binding.tv1.setTextColor(Color.parseColor("#333333"))
            binding.tv2.setTextColor(Color.parseColor("#333333"))
            binding.tv3.setTextColor(Color.parseColor("#333333"))
            binding.tv4.setTextColor(Color.parseColor("#333333"))
        }

        showSortUI.invoke(SortMode)

        var temp: String = SortMode
        binding.llLastModified.setOnClickListener {
            temp = UI_SORT_LAST_MODIFIED
            resetUI.invoke()
            showSortUI.invoke(temp)
        }
        binding.llFinalModified.setOnClickListener {
            temp = UI_SORT_FINAL_MODIFIED
            resetUI.invoke()
            showSortUI.invoke(temp)
        }
        binding.llNameAZ.setOnClickListener {
            temp = UI_SORT_NAME_A_Z
            resetUI.invoke()
            showSortUI.invoke(temp)
        }
        binding.llNameZA.setOnClickListener {
            temp = UI_SORT_NAME_Z_A
            resetUI.invoke()
            showSortUI.invoke(temp)
        }
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvOk.setOnClickListener {
            dialog.dismiss()
            SortMode = temp
            okAction.invoke()
        }
    }
}