package com.lukouguoji.gjj.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ActivityIntImpManifestSubEditBinding
import com.lukouguoji.gjj.viewModel.IntImpManifestSubEditViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.bean.GjjHaWb
import com.lukouguoji.module_base.bean.GjjManifest
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.DetailsPageType

class IntImpManifestSubEditActivity :
    BaseBindingActivity<ActivityIntImpManifestSubEditBinding, IntImpManifestSubEditViewModel>() {

    override fun layoutId() = R.layout.activity_int_imp_manifest_sub_edit

    override fun viewModelClass() = IntImpManifestSubEditViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        binding.viewModel = viewModel
        viewModel.initOnCreated(intent)

        val title = when (viewModel.pageType.value) {
            DetailsPageType.Modify -> "分单编辑"
            else -> "分单新增"
        }
        setBackArrow(title)
    }

    companion object {
        const val KEY_MAIN_PC = "mainPc"
        const val KEY_MAIN_WEIGHT = "mainWeight"
        const val KEY_OTHER_SUB_PC = "otherSubPc"
        const val KEY_OTHER_SUB_WEIGHT = "otherSubWeight"

        /**
         * 新增分单
         */
        @JvmStatic
        fun startForAdd(context: Context, manifest: GjjManifest) {
            // 新增模式：其他分单之和 = 所有已有分单之和
            val otherSubPc = manifest.haWbList?.sumOf { it.pc } ?: 0L
            val otherSubWeight = manifest.haWbList?.sumOf { it.weight } ?: 0.0

            context.startActivity(
                Intent(context, IntImpManifestSubEditActivity::class.java)
                    .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Add.name)
                    .putExtra(Constant.Key.BEAN, manifest)
                    .putExtra(KEY_MAIN_PC, manifest.totalPc)
                    .putExtra(KEY_MAIN_WEIGHT, manifest.weight)
                    .putExtra(KEY_OTHER_SUB_PC, otherSubPc)
                    .putExtra(KEY_OTHER_SUB_WEIGHT, otherSubWeight)
            )
        }

        /**
         * 修改分单
         */
        @JvmStatic
        fun startForModify(context: Context, manifest: GjjManifest, haWb: GjjHaWb) {
            // 修改模式：其他分单之和 = 所有分单之和 - 当前编辑的分单
            val otherSubPc = (manifest.haWbList?.sumOf { it.pc } ?: 0L) - haWb.pc
            val otherSubWeight = (manifest.haWbList?.sumOf { it.weight } ?: 0.0) - haWb.weight

            context.startActivity(
                Intent(context, IntImpManifestSubEditActivity::class.java)
                    .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Modify.name)
                    .putExtra(Constant.Key.BEAN, manifest)
                    .putExtra("haWb", haWb)
                    .putExtra(KEY_MAIN_PC, manifest.totalPc)
                    .putExtra(KEY_MAIN_WEIGHT, manifest.weight)
                    .putExtra(KEY_OTHER_SUB_PC, otherSubPc)
                    .putExtra(KEY_OTHER_SUB_WEIGHT, otherSubWeight)
            )
        }
    }
}
