package com.base.pdfviewerscannerwhite.ui.main

import android.annotation.SuppressLint
import android.net.Uri
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.pdfviewerscannerwhite.BuildConfig
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.databinding.ActivityMainBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.utils.PermissionUtils.requestStorePermission
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast

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: DocumentFragment by lazy {
        DocumentFragment()
    }
    private val toolFragment: ToolFragment by lazy {
        ToolFragment()
    }

    private var currentFragment: Fragment = pdfFragment
    private val fragments by lazy {
        mutableListOf(pdfFragment, toolFragment)
    }

    override fun initView() {
//        startActivity(Intent(this, PdfActivity::class.java))

        mainPresenter = MainPresenter(this)
        mainPresenter.initScannerLauncher(this)


        binding.viewPager2.run {
            isUserInputEnabled = false
            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 onPageSelected(position: Int) {
                currentFragment = fragments[position]
            }
        })

    }


    @SuppressLint("ClickableViewAccessibility", "SetTextI18n")
    override fun initListener() {
        super.initListener()


        binding.llDocument.setOnClickListener {
            binding.tvTittle.text = "Document"
            changeTabSelect(it)
            binding.ivScan.visibility = View.VISIBLE
            binding.clAction.visibility = View.VISIBLE
            binding.ivPaixu.visibility = View.VISIBLE
            binding.viewPager2.currentItem = 0
            if (currentFragment is DocumentFragment) {
                (currentFragment as DocumentFragment).setAllList()
            }
        }
        binding.llRecent.setOnClickListener {
            binding.tvTittle.text = "Document"
            binding.ivScan.visibility = View.VISIBLE
            binding.clAction.visibility = View.VISIBLE
            binding.ivPaixu.visibility = View.GONE
            changeTabSelect(it)
            binding.viewPager2.currentItem = 0
            if (currentFragment is DocumentFragment) {
                (currentFragment as DocumentFragment).setRecentList()
            }
        }
        binding.llBookmark.setOnClickListener {
            binding.tvTittle.text = "Document"
            binding.ivScan.visibility = View.VISIBLE
            binding.clAction.visibility = View.VISIBLE
            changeTabSelect(it)
            binding.viewPager2.currentItem = 0
            if (currentFragment is DocumentFragment) {
                (currentFragment as DocumentFragment).setBookmarkList()
            }
        }
        binding.llTool.setOnClickListener {
            binding.tvTittle.text = "Tool"
            binding.ivScan.visibility = View.GONE
            changeTabSelect(it)
            binding.viewPager2.currentItem = 1
            binding.clAction.visibility = View.GONE
        }

        binding.ivScan.setOnClickListener {
            mainPresenter.starGmsScan(this)
        }
        binding.tvTittle.setOnClickListener {
            requestStorePermission(launcher) {}
        }

    }


    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"
        }
    }


}