package com.all.pdfreader.pro.app.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import com.all.pdfreader.pro.app.R
import com.all.pdfreader.pro.app.databinding.DialogFileDetailsBinding
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
import com.all.pdfreader.pro.app.util.FileUtils.toFormatFileSize
import com.all.pdfreader.pro.app.util.FileUtils.toSlashDateTime
import com.all.pdfreader.pro.app.util.ToastUtils
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class FileDetailsDialogFragment() : BottomSheetDialogFragment() {

    private lateinit var binding: DialogFileDetailsBinding
    private val viewModel: PdfViewModel by activityViewModels()
    private lateinit var pdfDocument: PdfDocumentEntity


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = DialogFileDetailsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
            ?.setBackgroundResource(R.drawable.dr_rc_top_12_bg_white)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.pdfDocument.value?.let {
            pdfDocument = it
            initView()
        } ?: run {
            showToast(getString(R.string.file_not))
            dismiss()
        }
    }

    private fun initView() {
        binding.fileNameTv.text = pdfDocument.fileName
        binding.pathTv.text = pdfDocument.filePath
        binding.lastModifiedTv.text = pdfDocument.lastModified.toSlashDateTime()
        binding.lastViewedTv.text = pdfDocument.lastOpenedTime.toSlashDateTime()
        binding.sizeTv.text = pdfDocument.fileSize.toFormatFileSize()
    }

    private fun showToast(message: String) {
        ToastUtils.show(requireActivity(), message)
    }
}