package rasel.lunar.launcher.feeds

import android.content.DialogInterface
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.gson.Gson
import io.realm.kotlin.ext.query
import rasel.lunar.launcher.BuildConfig
import rasel.lunar.launcher.apps.DismissCalback
import rasel.lunar.launcher.databinding.FeedsResultMenuBinding
import rasel.lunar.launcher.databinding.SearchMenuBinding
import rasel.lunar.launcher.model.LocationLog
import rasel.lunar.launcher.utils.BLog
import rasel.lunar.launcher.workers.WorkersDb
import java.text.SimpleDateFormat
import java.util.Date

internal class FeedsResult : BottomSheetDialogFragment() {

    private lateinit var binding: FeedsResultMenuBinding
    private lateinit var searchWord: String
    private lateinit var packageManager: PackageManager
    private lateinit var appInfo: ApplicationInfo

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FeedsResultMenuBinding.inflate(inflater, container, false)

        /* get package name from fragment's tag */
        searchWord = tag.toString()
        WorkersDb.getRealm().query<LocationLog>().find()?.let {
            if (it.size > 0) {
                binding.logs.text = it.map {
                    BLog.LOGE("LocLog >> ${it.toString()}")
                    SimpleDateFormat("yyy/MM/dd-HH:mm:ss").format(Date(it.time))
                    it.mAddressLines.joinToString(" , ")
                }.joinToString( " , ")
            }
        }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireDialog() as BottomSheetDialog).dismissWithAnimation = true


    }


    var mDismissCalback : DismissCalback? = null

    fun show(manager: FragmentManager, tag: String?, dismissCalback : DismissCalback?) {
        this.mDismissCalback = dismissCalback
        this.show(manager, tag)
    }

    override fun show(manager: FragmentManager, tag: String?) {
        super.show(manager, tag)
    }

    override fun dismiss() {
        BLog.LOGE("dismiss()")
        mDismissCalback?.invoke()
        super.dismiss()
    }

    override fun onDismiss(dialog: DialogInterface) {
        BLog.LOGE("onDismiss(dialog: DialogInterface)")
        mDismissCalback?.invoke()
        super.onDismiss(dialog)
    }
}