package com.base.pdfreader2.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.base.pdfreader2.R
import com.base.pdfreader2.bean.DocumentBean
import com.base.pdfreader2.bean.DocumentBean.Companion.TYPE_EXCEL
import com.base.pdfreader2.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfreader2.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfreader2.databinding.DialogDocumentDetailBinding
import com.base.pdfreader2.databinding.DialogDocumentHomeMoreBinding
import com.base.pdfreader2.ui.main.DocumentFragment
import com.base.pdfreader2.ui.main.DocumentPresenter
import com.base.pdfreader2.ui.view.DialogView.showDeleteDialog
import com.base.pdfreader2.ui.view.NameDialog.showDocumentRenameDialog
import com.base.pdfreader2.utils.IntentShareUtils.documentShare
import com.base.pdfreader2.utils.KotlinExt.toFormatSize
import com.base.pdfreader2.utils.KotlinExt.toFormatTime3
import com.base.pdfreader2.utils.KotlinExt.toFormatTime4
import com.base.pdfreader2.utils.SpStringUtils
import com.base.pdfreader2.utils.SpStringUtils.LAST_VIEW_KEY
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import java.io.File

object DocumentDialog {


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

        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.h_soucang_s)
        } else {
            binding.ivBookmark.setImageResource(R.mipmap.h_soucang_n)
        }
        when (item.type) {
            TYPE_WORD -> {
                binding.ivDocument.setImageResource(R.mipmap.wordicon)
            }

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

            TYPE_PPT -> {
                binding.ivDocument.setImageResource(R.mipmap.ppticon)
            }
        }
        binding.llRename.setOnClickListener {
            showDocumentRenameDialog(file.name, okAction = { newName ->
                dialog.dismiss()
                documentPresenter.renameDocumentBean(file, newName, documentFragment)
            })
        }
        binding.llDetail.setOnClickListener {
            showDocumentDetail(item.path)
        }
        binding.llShare.setOnClickListener {
            documentShare(item)
        }
        binding.llDelete.setOnClickListener {
            dialog.dismiss()
            showDeleteDialog {
                documentPresenter.deleteDocument(item, documentFragment)
            }
        }
        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(false)

        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()
        }

    }
}