package com.lukouguoji.gjj.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ActivityGjjGoodsManifestBinding
import com.lukouguoji.gjj.viewModel.GjjGoodsManifestViewModel
import com.lukouguoji.module_base.adapter.loadImage
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.impl.observe
import com.lukouguoji.module_base.ktx.noNull
import com.yzq.zxinglibrary.encode.CodeCreator

class GjjGoodsManifestActivity :
    BaseBindingActivity<ActivityGjjGoodsManifestBinding, GjjGoodsManifestViewModel>() {

    override fun layoutId() = R.layout.activity_gjj_goods_manifest
    override fun viewModelClass() = GjjGoodsManifestViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        viewModel.id = intent.getStringExtra(Constant.Key.ID).noNull()
        binding.viewModel = viewModel

        viewModel.pageModel.rv = binding.rv
        viewModel.getData()

        CodeCreator.createQRCode("123456", 400, 400, null)?.let {
            loadImage(binding.iv, it)
        }

        FlowBus.with<String>(ConstantEvent.SIGNATURE_SUCCESS)
            .observe(this) {
                viewModel.signature.postValue(it)
                viewModel.submitSignature(requireContext(), it)
            }

        viewModel.resultSign.observe(this) {
            loadImage(binding.ivSign, it)
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context, id: String) {
            val starter = Intent(context, GjjGoodsManifestActivity::class.java)
                .putExtra(Constant.Key.ID, id)
            context.startActivity(starter)
        }
    }
}