package com.base.pdfviewerscannerwhite.ui.document.ppt

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import androidx.activity.addCallback
import androidx.core.view.isVisible
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.ads.AdmobHelper
import com.base.pdfviewerscannerwhite.ads.admob.AdmobInterstitialUtils
import com.base.pdfviewerscannerwhite.ads.admob.AdmobNativeUtils
import com.base.pdfviewerscannerwhite.bean.DocumentBean
import com.base.pdfviewerscannerwhite.databinding.ActivityPptBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.helper.MyApplication
import com.base.pdfviewerscannerwhite.ui.view.DialogView.showDocumentMore
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.SpStringUtils
import com.cherry.lib.doc.bean.DocEngine
import com.cherry.lib.doc.office.pg.control.PGControl
import com.cherry.lib.doc.util.Constant

class PptActivity : BaseActivity<ActivityPptBinding>() {

    private val TAG = "PptActivity"

    override val binding: ActivityPptBinding by lazy {
        ActivityPptBinding.inflate(layoutInflater)
    }

    companion object {
        var pptDocumentBean: DocumentBean = DocumentBean()

        fun launchDocViewer(
            activity: Activity,
            docSourceType: Int,
            path: String?,
            fileType: Int? = null,
            engine: Int? = null
        ) {
            val intent = Intent(activity, PptActivity::class.java)
            intent.putExtra(Constant.INTENT_SOURCE_KEY, docSourceType)
            intent.putExtra(Constant.INTENT_DATA_KEY, path)
            intent.putExtra(Constant.INTENT_TYPE_KEY, fileType)
            intent.putExtra(Constant.INTENT_ENGINE_KEY, engine)
            activity.startActivity(intent)
        }

    }

    override fun onResume() {
        super.onResume()
        updateAppLanguage(MyApplication.pptLanguage) {
            MyApplication.pptLanguage = it
        }
    }

    private var totalPageNumber = 0

    @SuppressLint("SetTextI18n")
    override fun initView() {
        initSpData(intent)
        binding.mDocView.getPageNumberAction = { current, total ->
            if (!binding.tvPageCount.isVisible) {
                binding.tvPageCount.isVisible = true
            }
            totalPageNumber = total
            binding.tvPageCount.text = "$current/$total"
        }
        binding.mDocView.singleTapAction = {
            LogEx.logDebug(TAG, "actionDownCallBack")
            if (isShowTopLayout) {
                LogEx.logDebug(TAG, "hide")
                hideTopLayout()
            } else {
                showTopLayout()
                LogEx.logDebug(TAG, "show")
            }
        }
        AdmobNativeUtils.showNativeAd(this, binding.flAd, R.layout.layout_admob_document_in)
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.mDocView.onDestroy()
        AdmobNativeUtils.onDestroy()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (AdmobHelper.isShowCloseDocumentInter()) {
                AdmobInterstitialUtils.showInterstitialAd(this@PptActivity) {
                    if (it) {
                        AdmobHelper.lastCloseDocumentShowAd = System.currentTimeMillis()
                    }
                    binding.root.postDelayed({ finishToMain() }, 500)
                }
            } else {
                binding.root.postDelayed({ finishToMain() }, 500)
            }
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.ivXuanzhuan.setOnClickListener {
            switchOrientation()
        }
        binding.ivMore.setOnClickListener {
            showDocumentMore(pptDocumentBean, totalPageNumber - 1) { pageIndex ->
                ((binding.mDocView.iOffice?.control?.appControl as PGControl)
                    .pgView.pgPrintMode.listView.showPDFPageForIndex(pageIndex))
            }
        }
    }

    private fun switchOrientation() {
        requestedOrientation = if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        } else {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }

    private var docSourceType = 0
    private var fileType = -1
    private var engine: Int = DocEngine.INTERNAL.value
    private var pathOrUri: String? = null// 文件地址
    private fun initSpData(intent: Intent?) {
        pathOrUri = intent?.getStringExtra(Constant.INTENT_DATA_KEY)
        docSourceType = intent?.getIntExtra(Constant.INTENT_SOURCE_KEY, 0) ?: 0
        fileType = intent?.getIntExtra(Constant.INTENT_TYPE_KEY, -1) ?: -1
        engine = intent?.getIntExtra(Constant.INTENT_ENGINE_KEY, DocEngine.INTERNAL.value) ?: DocEngine.INTERNAL.value

        binding.mDocView.openDoc(this, pathOrUri, docSourceType, fileType, false)
        LogEx.logDebug(TAG, "initData-docUrl = $pathOrUri")
        LogEx.logDebug(TAG, "initData-docSourceType = $docSourceType")
        LogEx.logDebug(TAG, "initData-fileType = $fileType")

        SpStringUtils.addSpString(SpStringUtils.LAST_VIEW_KEY, (pathOrUri ?: "") + "_/_" + System.currentTimeMillis())

    }

    private fun showTopLayout() {
        if (!isShowTopLayout) {
            isShowTopLayout = true

            val topAnim: Animation = TranslateAnimation(0f, 0f, -binding.vAnimatorTop.height.toFloat(), 0f)
            topAnim.setDuration(200)
            topAnim.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {
                    binding.vAnimatorTop.visibility = View.VISIBLE
                }

                override fun onAnimationRepeat(animation: Animation) {}
                override fun onAnimationEnd(animation: Animation) {
                }
            })
            binding.vAnimatorTop.startAnimation(topAnim)
        }
    }

    private var isShowTopLayout = true
    private fun hideTopLayout() {
        if (isShowTopLayout) {
            isShowTopLayout = false

            val topAnim: Animation = TranslateAnimation(0f, 0f, 0f, -binding.vAnimatorTop.height.toFloat())
            topAnim.setDuration(200)
            topAnim.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {}
                override fun onAnimationRepeat(animation: Animation) {}
                override fun onAnimationEnd(animation: Animation) {
                    binding.vAnimatorTop.visibility = View.GONE
                }
            })
            binding.vAnimatorTop.startAnimation(topAnim)
        }
    }
}