package es.verdnatura.presentation.view.feature.restaurant

import android.view.View
import android.widget.Button
import com.google.gson.Gson
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import es.verdnatura.R
import es.verdnatura.databinding.ActivityRestaurantViewBinding
import es.verdnatura.domain.toast
import es.verdnatura.presentation.base.BaseActivity
import es.verdnatura.presentation.view.feature.restaurant.model.UserMenu

class RestaurantActivity : BaseActivity<ActivityRestaurantViewBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_restaurant_view

    override fun init() {
        binding.mainToolbar.toolbarTitle.text = getString(R.string.selectMenu)
        /*    try {
                binding.imgView.loadUrl(intent.getStringExtra(getString(R.string.url))!!)
            } catch (ex: Exception) {
                getString(R.string.errorImage).toast(this)
                finish()
            }*/

        binding.mainToolbar.backButton.setOnClickListener {
            finish()
        }
    }

    fun generateQr(view: View) {
        binding.imageQr.visibility = View.VISIBLE
        binding.txtOption.visibility = View.VISIBLE
        binding.txtOption.text = (view as Button).text.toString()
        val userMenu = UserMenu(
            user = mobileApplication.userId!!,
            menu_id = view.tag.toString().toInt(),
            menu = view.text.toString(),
            name = mobileApplication.userName!!
        )
        try {
            val barcodeEncoder = BarcodeEncoder()
            val bitmap = barcodeEncoder.encodeBitmap(
                Gson().toJson(userMenu),
                BarcodeFormat.QR_CODE,
                400,
                400
            )
            val imageViewQrCode = binding.imageQr
            imageViewQrCode.setImageBitmap(bitmap)
        } catch (e: Exception) {
            e.toString().toast(this)
        }

    }

    private fun showQr() {

    }
}