package com.lukouguoji.gjc.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcGoodsManifestBinding
import com.lukouguoji.gjc.viewModel.GjcGoodsManifestViewModel
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 GjcGoodsManifestActivity :
    BaseBindingActivity<ActivityGjcGoodsManifestBinding, GjcGoodsManifestViewModel>() {

    override fun layoutId() = R.layout.activity_gjc_goods_manifest
    override fun viewModelClass() = GjcGoodsManifestViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        viewModel.movID = 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, movID: String) {
            val starter = Intent(context, GjcGoodsManifestActivity::class.java)
                .putExtra(Constant.Key.ID, movID)
            context.startActivity(starter)
        }
    }
}