/*
 * Lunar Launcher
 * Copyright (C) 2022 Md Rasel Hossain
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package rasel.lunar.launcher.apps

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 rasel.lunar.launcher.BuildConfig
import rasel.lunar.launcher.databinding.SearchMenuBinding
import rasel.lunar.launcher.utils.BLog


internal class SearchMenu : BottomSheetDialogFragment() {

    private lateinit var binding: SearchMenuBinding
    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 = SearchMenuBinding.inflate(inflater, container, false)

        /* get package name from fragment's tag */
        searchWord = tag.toString()

        return binding.root
    }

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

        /* copy package name */
//        binding.appPackage.setOnClickListener {
//            copyToClipboard(requireContext(), packageName)
//        }
//
        appName()
//        binding.detailedInfo.setOnClickListener { detailedInfo() }
//        binding.activityBrowser.setOnClickListener { activityBrowser() }
//        binding.appStore.setOnClickListener { appStore() }
//        binding.appFreeform.setOnClickListener { freeform() }
//        binding.appInfo.setOnClickListener { appInfo() }
//        binding.appShare.setOnClickListener { share() }
//        binding.appUninstall.setOnClickListener { uninstall() }
        binding.searchNmap.setOnClickListener {
            openSearchApps("nmap://search?query=${searchWord}&appname=${BuildConfig.APPLICATION_ID}","com.nhn.android.nmap")
        }
        binding.searchGoogleMap.setOnClickListener {
            openSearchApps("geo:0,0?q=${searchWord}","com.google.android.apps.maps")
        }
        binding.searchGoogle.setOnClickListener {
            openSearchApps("https://www.google.com/search?q=${searchWord}","com.android.chrome")
        }
        binding.searchTmap.setOnClickListener {
            openSearchApps("tmap://search?name=${searchWord}","com.skt.tmap.ku")

        }
        binding.searchNaver.setOnClickListener {
            openSearchApps("https://search.naver.com/search.naver?where=nexearch&query=${searchWord}", "com.nhn.android.search")
        }
        binding.searchDuckduckgo.setOnClickListener {
            openSearchApps("https://duckduckgo.com/?t=h_&q=${searchWord}","com.duckduckgo.mobile.android")
        }
        binding.searchNamuwiki.setOnClickListener {
            openSearchApps("https://namu.wiki/Search?q=${searchWord}")
        }

        binding.searchTranslate.setOnClickListener {
            openSearchApps("https://translate.google.com/?hl=ko&sl=ko&tl=en&text=${searchWord}&op=translate","com.android.chrome")
        }

        binding.searchCoupang.setOnClickListener {
            openSearchApps("coupang://search?q=${searchWord}","com.coupang.mobile")
        }
    }
    
    fun openSearchApps(schemeString : String, pakage : String? = null) {
        val gmmIntentUri = Uri.parse(schemeString)
        val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
        pakage?.let {
            mapIntent.setPackage(pakage)
        }
        startActivity(mapIntent)
        try {
            dismiss()
        } catch (e : Exception) {
            e.printStackTrace()
        }
    }


    private fun appName() {
        binding.keyworkd.text = searchWord
    }

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

typealias DismissCalback = ()->Unit
