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.ui.document.DocumentFragment
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 var currentFragment = pdfFragment
    private val fragments by lazy {
        mutableListOf(pdfFragment)
    }

    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")
    override fun initListener() {
        super.initListener()


        binding.llDocument.setOnClickListener {
            changeTabSelect(it)
            binding.ivPaixu.visibility = View.VISIBLE
            currentFragment.setAllList()
        }
        binding.llRecent.setOnClickListener {
            changeTabSelect(it)
            binding.ivPaixu.visibility = View.GONE
            currentFragment.setRecentList()
        }
        binding.llBookmark.setOnClickListener {
            changeTabSelect(it)
            currentFragment.setBookmarkList()
        }
        binding.llTool.setOnClickListener {
            changeTabSelect(it)
        }

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


}