package com.adealink.weparty.share.qrcode.page.special

import android.graphics.Color
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.copyToClipBoard
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.commonSelectedDrawable
import com.adealink.weparty.commonui.ext.dpf
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.share.R
import com.adealink.weparty.share.databinding.FragmentCommonQrCodeBinding
import com.adealink.weparty.share.qrcode.QRCodeShareDialog
import com.adealink.weparty.share.qrcode.page.BaseQRCodeFragment
import com.adealink.weparty.share.qrcode.page.special.viewmodel.SpecialQRCodeViewModel
import com.adealink.weparty.share.qrcode.viewmodel.QRCodeViewModel
import com.adealink.weparty.viewmodel.parentFragmentViewModels
import com.adealink.weparty.R as APP_R

class SpecialQRCodeStep2Fragment : BaseQRCodeFragment(R.layout.fragment_common_qr_code) {

    private val binding by viewBinding(FragmentCommonQrCodeBinding::bind)

    private val specialQRCodeViewModel by parentFragmentViewModels<SpecialQRCodeViewModel, SpecialQRCodeFragment>(
        SpecialQRCodeFragment::class.java
    )
    private val qrCodeViewModel by parentFragmentViewModels<QRCodeViewModel, QRCodeShareDialog>(
        QRCodeShareDialog::class.java
    )

    override fun initViews() {
        super.initViews()
        binding.btnSaveImage.background = commonSelectedDrawable(24.dpf(), solidColor = Color.WHITE)
        binding.btnSaveImage.onClick {
            checkStoragePermissionAndSaveQRCode()
        }
        binding.btnCopyLink.onClick {
            copyLink()
        }

        val category = qrCodeViewModel.currentCategoryLD.value
        val number = specialQRCodeViewModel.orderNumberLD.value ?: 1
        binding.vPrice.setPrice(
            number * (category?.price ?: 0f),
            "${category?.unit ?: ""} x$number"
        )
    }

    override fun loadData() {
        super.loadData()
        val category = qrCodeViewModel.currentCategoryLD.value ?: return
        qrCodeViewModel.getSpecialQRCodeOrder(
            category,
            specialQRCodeViewModel.orderNumberLD.value ?: 1
        ).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    val link = generateQRCode(rlt.data)
                    this@SpecialQRCodeStep2Fragment.qrCodeLink = link
                    this.orderDetail = rlt.data
                    qrCodeViewModel.generateQRCode(link).observe(viewLifecycleOwner) {
                        qrCodeBitmap = it
                        binding.ivQrCode.setImageBitmap(it)
                    }
                }
            }
        }
    }

    private fun copyLink() {
        val link = this@SpecialQRCodeStep2Fragment.qrCodeLink
        if (link.isNullOrEmpty()) {
            return
        }
        copyToClipBoard(link, link)
        showToast(APP_R.string.common_copy_success)
    }

    override fun orderPrice(): Float? {
        val category = qrCodeViewModel.currentCategoryLD.value ?: return null
        val number = specialQRCodeViewModel.orderNumberLD.value ?: 1
        return category.price * number
    }

    override fun orderCount(): Int {
        return specialQRCodeViewModel.orderNumberLD.value ?: 1
    }

}