//package com.base.pdfviewerscannerwhite.ui.main
//
//import android.annotation.SuppressLint
//import android.net.Uri
//import android.view.View
//import android.view.inputmethod.EditorInfo
//import androidx.activity.OnBackPressedCallback
//import androidx.core.content.ContextCompat
//import androidx.core.widget.addTextChangedListener
//import androidx.fragment.app.Fragment
//import androidx.viewpager2.adapter.FragmentStateAdapter
//import androidx.viewpager2.widget.ViewPager2
//import com.angcyo.tablayout.DslTabIndicator
//import com.angcyo.tablayout.delegate2.ViewPager2Delegate
//import com.base.pdfviewerscannerwhite.BuildConfig
//import com.base.pdfviewerscannerwhite.R
//import com.base.pdfviewerscannerwhite.bean.DocumentBean
//import com.base.pdfviewerscannerwhite.databinding.ActivityMainBinding
//import com.base.pdfviewerscannerwhite.helper.BaseActivity
//import com.base.pdfviewerscannerwhite.ui.main.DocumentAdapter
//import com.base.pdfviewerscannerwhite.ui.main.DocumentPageFragment.Companion.jumpDocument
//import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDeleteDialog
//import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentRenameDialog
//import com.base.pdfviewerscannerwhite.ui.view.DialogView.showStoragePermission
//import com.base.pdfviewerscannerwhite.ui.view.RateDialog.showRateDialog
//import com.base.pdfviewerscannerwhite.utils.IntentShareUtils.shareMutDocuments
//import com.base.pdfviewerscannerwhite.utils.KeyBoardUtils.hideKeyboard
//import com.base.pdfviewerscannerwhite.utils.KeyBoardUtils.showKeyBoard
//import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime2
//import com.base.pdfviewerscannerwhite.utils.LogEx
//import com.base.pdfviewerscannerwhite.utils.PermissionUtils.checkStorePermission
//import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
//import com.base.pdfviewerscannerwhite.utils.updateMediaStore
//
//@SuppressLint("SetTextI18n")
//class MainActivity : BaseActivity<ActivityMainBinding>(), MainView {
//
//    private val TAG = "MainActivity"
//    private lateinit var mainPresenter: MainPresenter
//
//    override val binding: ActivityMainBinding by lazy {
//        ActivityMainBinding.inflate(layoutInflater)
//    }
//    private val pdfFragment: DocumentPageFragment by lazy {
//        DocumentPageFragment(DocumentBean.TYPE_PDF)
//    }
//    private val wordFragment: DocumentPageFragment by lazy {
//        DocumentPageFragment(DocumentBean.TYPE_WORD)
//    }
//    private val excelFragment: DocumentPageFragment by lazy {
//        DocumentPageFragment(DocumentBean.TYPE_EXCEL)
//    }
//    private val pptFragment: DocumentPageFragment by lazy {
//        DocumentPageFragment(DocumentBean.TYPE_PPT)
//    }
//    private val toolFragment: ToolFragment by lazy {
//        ToolFragment()
//    }
//    private var currentFragment: Fragment = pdfFragment
//    private val fragments by lazy {
//        mutableListOf(pdfFragment, wordFragment, excelFragment, pptFragment, toolFragment)
//    }
//    private lateinit var searchAdapter: DocumentAdapter
//
//    override fun initView() {
//
//        mainPresenter = MainPresenter(this, this)
//        mainPresenter.initScannerLauncher(this)
//
//        binding.viewPager2.offscreenPageLimit = 4
//        binding.viewPager2.run {
//            isUserInputEnabled = true
//            adapter = object : FragmentStateAdapter(this@MainActivity) {
//                override fun getItemCount(): Int {
//                    return fragments.size
//                }
//
//                override fun createFragment(position: Int): Fragment {
//                    return fragments[position]
//                }
//            }
//        }
//        binding.viewPager2.registerOnPageChangeCallback(object :
//            ViewPager2.OnPageChangeCallback() {
//
//            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
//                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
//                if (position == 3 && positionOffset > 0) {
//                    binding.viewPager2.setCurrentItem(position, false)
//                }
//            }
//
//            override fun onPageSelected(position: Int) {
//                currentFragment = fragments[position]
//            }
//        })
//
//        initTabLayout()
//        initSearchAdapter()
//
//        if (!checkStorePermission()) {
//            showStoragePermission(launcher)
//            updateMediaStore()
//        }
//    }
//
//    private fun initSearchAdapter() {
//        searchAdapter = DocumentAdapter()
//        binding.rvSearch.adapter = searchAdapter
//        searchAdapter.moreAction = { item ->
//            if (currentFragment is DocumentPageFragment) {
//                (currentFragment as DocumentPageFragment).documentMoreAction.invoke(item)
//            }
//        }
//        searchAdapter.itemClick = {
//            jumpDocument(it)
//        }
//
//    }
//
//    private fun initTabLayout() {
//        val colorPdf = ContextCompat.getColor(this, R.color.color_f02f2b)
//        val colorWord = ContextCompat.getColor(this, R.color.color_0592ee)
//        val colorExcel = ContextCompat.getColor(this, R.color.color_149b55)
//        val colorPpt = ContextCompat.getColor(this, R.color.color_ffa127)
//        binding.tabLayoutDocument.apply {
//            tabIndicator.indicatorWidth =
//                this.resources.getDimensionPixelOffset(R.dimen.dp_50)
//            tabIndicator.indicatorHeight =
//                this.resources.getDimensionPixelOffset(R.dimen.dp_4)
//            tabIndicator.indicatorStyle =
//                DslTabIndicator.INDICATOR_STYLE_BOTTOM
//
//            configTabLayoutConfig {
//                onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
//                    val toIndex = selectIndexList.first()
//                    LogEx.logDebug(
//                        TAG,
//                        "toIndex=$toIndex fromIndex=$fromIndex selectIndexList=$selectIndexList reselect=$reselect fromUser=$fromUser"
//                    )
//
//                    when (toIndex) {
//                        0 -> {
//                            tabSelectColor = colorPdf
//                            tabIndicator.indicatorColor = colorPdf
//                            binding.viewPager2.currentItem = 0
//                        }
//
//                        1 -> {
//                            tabSelectColor = colorWord
//                            tabIndicator.indicatorColor = colorWord
//                            binding.viewPager2.currentItem = 1
//                        }
//
//                        2 -> {
//                            tabSelectColor = colorExcel
//                            tabIndicator.indicatorColor = colorExcel
//                            binding.viewPager2.currentItem = 2
//                        }
//
//                        3 -> {
//                            tabSelectColor = colorPpt
//                            tabIndicator.indicatorColor = colorPpt
//                            binding.viewPager2.currentItem = 3
//                        }
//                    }
//                    dslSelector.updateStyle()
//                }
//            }
//        }
//        ViewPager2Delegate.install(binding.viewPager2, binding.tabLayoutDocument)
//    }
//
//    private var latViewPagerIndex = -1
//
//    private lateinit var callback: OnBackPressedCallback
//
//    private fun callTab() {
//        LogEx.logDebug(TAG, "callTab")
//        if (uiMode == HOME_UI_MODE_DOCUMENT) {
//            binding.llDocument.callOnClick()
//        }
//    }
//
//    @SuppressLint("ClickableViewAccessibility", "SetTextI18n")
//    override fun initListener() {
//        super.initListener()
//        callback = object : OnBackPressedCallback(true /* enabled */) {
//            override fun handleOnBackPressed() {
//                if (uiMode == HOME_UI_MODE_SELECT) {
//                    when (lastUIMode) {
//                        HOME_UI_MODE_DOCUMENT -> changeDocumentUI()
//                        HOME_UI_MODE_RECENT -> changeRecentUI()
//                        HOME_UI_MODE_BOOKMARK -> changeBookmarkUI()
//                    }
//                }
//                if (uiMode == HOME_UI_MODE_SEARCH) {
//                    cancelSearchUI()
//                    when (lastUIMode) {
//                        HOME_UI_MODE_DOCUMENT -> changeDocumentUI()
//                        HOME_UI_MODE_RECENT -> changeRecentUI()
//                        HOME_UI_MODE_BOOKMARK -> changeBookmarkUI()
//                    }
//                }
//            }
//
//        }
//        onBackPressedDispatcher.addCallback(callback)
//        binding.flFanhui.setOnClickListener {
//            onBackPressedDispatcher.onBackPressed()
//        }
//        binding.llDocument.setOnClickListener {
//            changeDocumentUI()
//        }
//        binding.llRecent.setOnClickListener {
//            changeRecentUI()
//        }
//        binding.llBookmark.setOnClickListener {
//            changeBookmarkUI()
//        }
//        binding.llTool.setOnClickListener {
//            changeToolUI()
//        }
//        binding.ivScan.setOnClickListener {
//            mainPresenter.starGmsScan(this)
//        }
//        binding.ivXuanze.setOnClickListener {
//            changeSelectUI()
//        }
//        binding.tvTittle.setOnClickListener {
//
//        }
//        binding.vTabLayoutPlace.setOnClickListener {
//
//        }
//        binding.llDelete.setOnClickListener {
//            if (currentFragment is DocumentPageFragment) {
//                val fragment = (currentFragment as DocumentPageFragment)
//                showDeleteDialog {
//                    val list = fragment.getSelectItems()
//                    mainPresenter.deleteList(list)
//                    fragment.removeList(list)
//                }
//            }
//        }
//        binding.ivAllSelector.setOnClickListener {
//            binding.ivAllSelector.isSelected = !binding.ivAllSelector.isSelected
//            changePageSelect(binding.ivAllSelector.isSelected)
//        }
//        binding.llShare.setOnClickListener {
//            shareDocument()
//        }
//        binding.ivSearch.setOnClickListener {
//            changeSearchUI()
//        }
//        binding.editSearch.addTextChangedListener {
//
//        }
//        binding.editSearch.setOnEditorActionListener { v, actionId, event ->
//            LogEx.logDebug(TAG, "$actionId")
//            if (actionId == EditorInfo.IME_ACTION_DONE) {
//                LogEx.logDebug(TAG, "IME_ACTION_DONE")
//                searchDocument()
//            }
//            false
//        }
//        callTab()
//    }
//
//
//    private fun searchDocument() {
//        if (binding.editSearch.text.isNotEmpty()) {
//            val search = binding.editSearch.text.toString()
//            if (currentFragment is DocumentPageFragment) {
//                val item = (currentFragment as DocumentPageFragment).getSearchItems(search)
//                if (item.isNotEmpty()) {
//                    item.map { it.uiType = 4 }
//                    searchAdapter.submitList(item)
//                } else {
//                    binding.llEmpty.visibility = View.VISIBLE
//                }
//            }
//        }
//    }
//
//
//    private fun shareDocument() {
//        if (currentFragment is DocumentPageFragment) {
//            val documentFragment = (currentFragment as DocumentPageFragment)
//            val items = documentFragment.getSelectItems()
//            if (items.isNotEmpty()) {
//                shareMutDocuments(documentFragment.type, items.map { it.uri } as ArrayList<Uri>)
//            }
//
//        }
//    }
//
//    private fun changePageSelect(select: Boolean) {
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).changItemSelect(select)
//        }
//    }
//
//    private fun changeTabSelect(selectView: View) {
//        binding.llDocument.isSelected = selectView == binding.llDocument
//        binding.llRecent.isSelected = selectView == binding.llRecent
//        binding.llBookmark.isSelected = selectView == binding.llBookmark
//        binding.llTool.isSelected = selectView == binding.llTool
//
//    }
//
//    fun changeRipple() = binding.root.postDelayed({
//        val selectRipple = ContextCompat.getDrawable(this@MainActivity, R.drawable.ripple_select)
//        val normalRipple = ContextCompat.getDrawable(this@MainActivity, R.drawable.ripple_normal)
//
//        if (binding.llDocument.isSelected) {
//            binding.llDocument.background = selectRipple
//        } else {
//            binding.llDocument.background = normalRipple
//        }
//
//        if (binding.llRecent.isSelected) {
//            binding.llRecent.background = selectRipple
//        } else {
//            binding.llRecent.background = normalRipple
//        }
//
//        if (binding.llBookmark.isSelected) {
//            binding.llBookmark.background = selectRipple
//        } else {
//            binding.llBookmark.background = normalRipple
//        }
//
//        if (binding.llTool.isSelected) {
//            binding.llTool.background = selectRipple
//        } else {
//            binding.llTool.background = normalRipple
//        }
//    }, 200)
//
//    @SuppressLint("SetTextI18n")
//    override fun handleActivityGmsScanResult(imageUri: Uri, pdfUri: Uri) {
//        toast("handleActivityGmsScanResult")
//        if (BuildConfig.DEBUG) {
//            binding.tvDebugLog.text = "imageUri=$imageUri\npdfUri=$pdfUri"
//        }
//        showDocumentRenameDialog(name = "PDF_${System.currentTimeMillis().toFormatTime2()}") {
//
//        }
//    }
//
//    @SuppressLint("NotifyDataSetChanged")
//    override fun refreshSearchRv() {
//        searchAdapter.notifyDataSetChanged()
//    }
//
//    override fun deleteDocument(item: DocumentBean) {
//        LogEx.logDebug(TAG, "deleteDocument $item")
//        searchAdapter.remove(item)
//    }
//
//    override fun onResume() {
//        super.onResume()
//
//        showRateDialog()
//    }
//
//    private var uiMode = HOME_UI_MODE_DOCUMENT
//
//    private fun cancelSelectUI() {
//        binding.viewPager2.isUserInputEnabled = true
//        callback.isEnabled = false
//
//        if (latViewPagerIndex != -1) {
//            binding.viewPager2.setCurrentItem(latViewPagerIndex, false)
//            latViewPagerIndex = -1
//        }
//
//        binding.vTabLayoutPlace.visibility = View.GONE
//        binding.flFanhui.visibility = View.GONE
//        binding.llDelete.visibility = View.GONE
//        binding.llShare.visibility = View.GONE
//        binding.ivAllSelector.visibility = View.GONE
//
//
//        binding.ivSearch.visibility = View.VISIBLE
//        binding.tvTittle.visibility = View.VISIBLE
//        binding.llDocument.visibility = View.VISIBLE
//        binding.llRecent.visibility = View.VISIBLE
//        binding.llBookmark.visibility = View.VISIBLE
//        binding.llTool.visibility = View.VISIBLE
//        binding.ivXuanze.visibility = View.VISIBLE
//
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).changeSelectUi(false)
//        }
//    }
//
//    fun changeDocumentUI() {
//        LogEx.logDebug(TAG, "changeDocumentUI")
//        uiMode = HOME_UI_MODE_DOCUMENT
//        binding.tvTittle.text = "Document"
//
//        cancelSelectUI()
//
//        binding.ivXuanze.visibility = View.VISIBLE
//        binding.ivScan.visibility = View.VISIBLE
//        binding.ivPaixu.visibility = View.VISIBLE
//        binding.ivScan.visibility = View.VISIBLE
//
//        changeTabSelect(binding.llDocument)
//
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).setAllList()
//        }
//    }
//
//    fun changeRecentUI() {
//        LogEx.logDebug(TAG, "changeRecentUI")
//        uiMode = HOME_UI_MODE_RECENT
//        binding.tvTittle.text = "Recent"
//
//        cancelSelectUI()
//
//        binding.clTabLayout.visibility = View.VISIBLE
//        binding.ivPaixu.visibility = View.VISIBLE
//        binding.ivScan.visibility = View.VISIBLE
//        binding.ivScan.visibility = View.VISIBLE
//
//        changeTabSelect(binding.llRecent)
//
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).setRecentList()
//        }
//    }
//
//    fun changeBookmarkUI() {
//        LogEx.logDebug(TAG, "changeBookmarkUI")
//        uiMode = HOME_UI_MODE_BOOKMARK
//
//        cancelSelectUI()
//
//        binding.tvTittle.text = "Bookmark"
//        binding.clTabLayout.visibility = View.VISIBLE
//        binding.ivScan.visibility = View.VISIBLE
//        binding.ivScan.visibility = View.VISIBLE
//        changeTabSelect(binding.llBookmark)
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).setBookmarkList()
//        }
//    }
//
//    private fun changeToolUI() {
//        LogEx.logDebug(TAG, "changeToolUI")
//        uiMode = HOME_UI_MODE_TOOL
//        callback.isEnabled = false
//        binding.viewPager2.isUserInputEnabled = true
//
//        binding.tvTittle.text = "Tool"
//        binding.ivScan.visibility = View.GONE
//        binding.clTabLayout.visibility = View.GONE
//        latViewPagerIndex = binding.viewPager2.currentItem
//        binding.viewPager2.setCurrentItem(5, false)
//        changeTabSelect(binding.llTool)
//    }
//
//    private var lastUIMode = HOME_UI_MODE_DOCUMENT
//    private fun changeSelectUI() {
//        LogEx.logDebug(TAG, "changeSelectUI")
//        callback.isEnabled = true
//        lastUIMode = uiMode
//        uiMode = HOME_UI_MODE_SELECT
//        binding.ivPaixu.visibility = View.INVISIBLE
//        binding.ivXuanze.visibility = View.INVISIBLE
//        binding.ivSearch.visibility = View.INVISIBLE
//        binding.tvTittle.visibility = View.INVISIBLE
//        binding.ivScan.visibility = View.INVISIBLE
//
//        binding.llDocument.visibility = View.INVISIBLE
//        binding.llRecent.visibility = View.INVISIBLE
//        binding.llBookmark.visibility = View.INVISIBLE
//        binding.llTool.visibility = View.INVISIBLE
//
//        binding.flFanhui.visibility = View.VISIBLE
//        binding.llDelete.visibility = View.VISIBLE
//        binding.llShare.visibility = View.VISIBLE
//
//        binding.ivAllSelector.visibility = View.VISIBLE
//        binding.viewPager2.isUserInputEnabled = false
//        binding.vTabLayoutPlace.visibility = View.VISIBLE
//
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).changeSelectUi(true)
//        }
//    }
//
//    private fun changeSearchUI() {
//        callback.isEnabled = true
//        lastUIMode = uiMode
//        uiMode = HOME_UI_MODE_SEARCH
//
//        binding.tvTittle.visibility = View.INVISIBLE
//        binding.ivPaixu.visibility = View.INVISIBLE
//        binding.ivXuanze.visibility = View.INVISIBLE
//        binding.llMainContent.visibility = View.INVISIBLE
//
//        binding.editSearch.visibility = View.VISIBLE
//        binding.flFanhui.visibility = View.VISIBLE
//        binding.flSearch.visibility = View.VISIBLE
//        binding.flSearch.setOnClickListener {}
//        binding.editSearch.requestFocus()
//        showKeyBoard(binding.editSearch)
//    }
//
//    private fun cancelSearchUI() {
//        binding.flFanhui.visibility = View.GONE
//        binding.flSearch.visibility = View.GONE
//        binding.editSearch.visibility = View.GONE
//        binding.llEmpty.visibility = View.GONE
//        searchAdapter.submitList(listOf())
//        binding.editSearch.setText("")
//        hideKeyboard(binding.editSearch)
//
//        binding.llMainContent.visibility = View.VISIBLE
//        binding.tvTittle.visibility = View.VISIBLE
//        binding.ivPaixu.visibility = View.VISIBLE
//        binding.ivXuanze.visibility = View.VISIBLE
//
//        if (currentFragment is DocumentPageFragment) {
//            (currentFragment as DocumentPageFragment).changeHomeUI()
//        }
//    }
//
//    companion object {
//        const val HOME_UI_MODE_DOCUMENT = "home_ui_mode_document"
//        const val HOME_UI_MODE_RECENT = "home_ui_mode_recent"
//        const val HOME_UI_MODE_BOOKMARK = "home_ui_mode_bookmark"
//        const val HOME_UI_MODE_TOOL = "home_ui_mode_tool"
//        const val HOME_UI_MODE_SELECT = "home_ui_mode_select"
//        const val HOME_UI_MODE_SEARCH = "home_ui_mode_search"
//    }
//}