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

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.module.playmate.data.UserPlaymateCategoryData
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.viewmodel.QRCodeViewModel
import com.adealink.weparty.viewmodel.parentFragmentViewModels

/**
 * 通用码生成规则
 * https://mimolive.sg.larksuite.com/wiki/Dic1wugYQiuSXQk6jWgl9OzNglc?from=from_copylink
 *
 * 测试环境:
 * https://test-web.gami.vip/user/category?id=xxxx&code=xxxx
 * 正式环境:
 * https://web.gami.vip/user/category?id=xxxx&code=xxxx
 *
 * 参数说明:
 * - id: 用户ID
 * - code: 后端二维码
 */
class CommonQRCodeFragment : BaseQRCodeFragment(R.layout.fragment_common_qr_code) {

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

    private val binding by viewBinding(FragmentCommonQrCodeBinding::bind)

    private var currentCategory: UserPlaymateCategoryData? = null

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

    override fun observeViewModel() {
        super.observeViewModel()
        qrCodeViewModel.currentCategoryLD.observe(this) { data ->
            currentCategory = data
            data ?: return@observe
            updatePanel(data)
        }
    }

    private fun updatePanel(data: UserPlaymateCategoryData) {
        currentCategory = data
        binding.vPrice.setPrice(data.price, data.unit)
        qrCodeViewModel.getCommonQRCodeOrder(data).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

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

    private fun copyLink() {
        val link = this@CommonQRCodeFragment.qrCodeLink
        if (link.isNullOrEmpty()) {
            return
        }
        copyToClipBoard(link, link)
        showToast(com.adealink.weparty.R.string.common_copy_success)
    }

    override fun orderPrice(): Float? {
        return orderDetail?.category?.price
    }

    override fun orderCount(): Int {
        return 1
    }

}