package com.base.pdfreaderallpdfreader.ui.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.core.widget.addTextChangedListener
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.bean.DocumentBean
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_EXCEL
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfreaderallpdfreader.databinding.DialogDocumentDetailBinding
import com.base.pdfreaderallpdfreader.databinding.DialogDocumentHomeMoreBinding
import com.base.pdfreaderallpdfreader.databinding.DialogDocumentMoreBinding
import com.base.pdfreaderallpdfreader.databinding.DialogPageNumberBinding
import com.base.pdfreaderallpdfreader.ui.document.DocumentFragment
import com.base.pdfreaderallpdfreader.ui.view.DialogView.showDeleteDialog
import com.base.pdfreaderallpdfreader.ui.view.NameDialog.showDocumentRenameDialog
import com.base.pdfreaderallpdfreader.utils.IntentShareUtils.documentShare
import com.base.pdfreaderallpdfreader.utils.KotlinExt.toFormatSize
import com.base.pdfreaderallpdfreader.utils.KotlinExt.toFormatTime3
import com.base.pdfreaderallpdfreader.utils.KotlinExt.toFormatTime4
import com.base.pdfreaderallpdfreader.utils.NumberRangeFilter
import com.base.pdfreaderallpdfreader.utils.SpStringUtils
import com.base.pdfreaderallpdfreader.utils.SpStringUtils.LAST_VIEW_KEY
import com.base.pdfreaderallpdfreader.utils.ToastUtils.toast
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import java.io.File


interface DocumentHomeMoreCallBack {

    fun renameDocumentBean(file: File, newName: String) {

    }

    fun deleteDocument(item: DocumentBean) {

    }
}

object DocumentDialog {


    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    fun Context.showDocumentHomeMoreDialog(
        item: DocumentBean,
        documentFragment: DocumentFragment? = null,
        documentHomeMoreCallBack: DocumentHomeMoreCallBack? = null
    ): BottomSheetDialog {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogDocumentHomeMoreBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

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

        val file = File(item.path)
        binding.tvName.text = file.name
        binding.tvInfo.text = file.lastModified().toFormatTime4() + " " + file.length().toFormatSize()
        if (item.isBookmarked) {
            binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_s)
        } else {
            binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_n)
        }
        when (item.type) {
            TYPE_WORD -> {
                binding.ivDocument.setImageResource(R.mipmap.rv_word)
            }

            TYPE_EXCEL -> {
                binding.ivDocument.setImageResource(R.mipmap.rv_excel)
            }

            TYPE_PPT -> {
                binding.ivDocument.setImageResource(R.mipmap.r_ppt)
            }
        }
        binding.llRename.setOnClickListener {
            showDocumentRenameDialog(file.name, okAction = { newName ->
                dialog.dismiss()
                documentFragment?.renameDocumentBean(file, newName)
                documentHomeMoreCallBack?.renameDocumentBean(file, newName)
            })
        }
        binding.llDetail.setOnClickListener {
            showDocumentDetail(item.path)
        }
        binding.llShare.setOnClickListener {
            documentShare(item)
        }
        binding.llDelete.setOnClickListener {
            dialog.dismiss()
            showDeleteDialog {
                documentFragment?.deleteDocument(item)
                documentHomeMoreCallBack?.deleteDocument(item)
            }
        }
        return dialog
    }

    fun Context.showDocumentDetail(path: String) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogDocumentDetailBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

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

        val file = File(path)
        binding.tvName.text = file.name
        binding.tvPath.text = file.absolutePath
        val lastView = SpStringUtils.getSpStringList(LAST_VIEW_KEY).find { it.contains(file.absolutePath) }
        if (lastView != null) {
            val lastTime = lastView.split("_/_")[1]
            binding.tvLastView.text = lastTime.toLong().toFormatTime3()
        } else {
            binding.tvLastView.text = file.lastModified().toFormatTime3()
        }
        binding.tvLastChange.text = file.lastModified().toFormatTime3()
        binding.tvFileSize.text = file.length().toFormatSize()

        binding.tvBtnOk.setOnClickListener {
            dialog.dismiss()
        }

    }

    fun Activity.showDocumentMore(
        documentBean: DocumentBean,
        pageNumber: Int = 0,
        jumpAction: ((pageIndex: Int) -> Unit)? = null
    ) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogDocumentMoreBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

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

        if (documentBean.type == TYPE_EXCEL) {
            binding.llJump.visibility = View.GONE
        }

        if (documentBean.isBookmarked) {
            binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_s)
        } else {
            binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_n)
        }

        binding.ivBookmark.setOnClickListener {
            binding.ivBookmark.isSelected = !binding.ivBookmark.isSelected

            if (binding.ivBookmark.isSelected) {
                binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_s)
                SpStringUtils.addSpString(SpStringUtils.BOOKMARK_KEY, documentBean.path)
            } else {
                binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_n)
                SpStringUtils.deleteSpString(SpStringUtils.BOOKMARK_KEY, documentBean.path)
            }
        }
        binding.llDetail.setOnClickListener {
            showDocumentDetail(documentBean.path)
        }
        binding.llShare.setOnClickListener {
            documentShare(documentBean)
        }
        binding.llJump.setOnClickListener {
            showJumpPageNumberDialog(pageNumber) { pageIndex ->
                dialog.dismiss()
                jumpAction?.invoke(pageIndex)
            }
        }

    }

    fun Context.showJumpPageNumberDialog(pageNumber: Int, okAction: ((pageIndex: Int) -> Unit)?) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogPageNumberBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        val window = dialog.window
        window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

        dialog.show()

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

        binding.edit.filters = arrayOf(NumberRangeFilter(1, pageNumber + 1))
        binding.edit.hint = "1 - ${pageNumber + 1}"
        binding.edit.addTextChangedListener {
            binding.tvOk.isEnabled = !it.isNullOrEmpty()
        }
        binding.edit.requestFocus()
        binding.tvOk.setOnClickListener {
            val number = binding.edit.text.toString()
            if (number.isEmpty()) {
                toast("number can't be empty!")
                return@setOnClickListener
            }
            dialog.dismiss()
            okAction?.invoke(number.toInt() - 1)
        }
    }
}