package com.yidian.shenghuoquan.bmanagercenter.app

import android.content.Intent
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.sdk.ClientType
import com.yidian.bcommon.sdk.JudgeClientUtils
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.nightmode.util.NightModeUtil
import com.yidian.shenghuoquan.bmanagercenter.personnel.ui.*
import com.yidian.shenghuoquan.bmanagercenter.service.LifeNumberListService
import com.yidian.shenghuoquan.bmanagercenter.service.SelectStoreService
import com.yidian.shenghuoquan.bmanagercenter.ui.auth.*
import com.yidian.shenghuoquan.bmanagercenter.ui.center.*
import com.yidian.shenghuoquan.bmanagercenter.ui.store.ChooseStoreActivity
import com.yidian.shenghuoquan.bmanagercenter.ui.store.StoreManagementActivity
import com.yidian.shenghuoquan.bmanagercenter.wallet.ui.*
import com.yidian.utils.DensityUtil
import com.yidian.xpage.XPageManager
import com.yidian.xpage.node.XPageHandler
import com.yidian.xpage.node.XPageNode
import com.yidian.xpage.node.XPageNodePageType
import com.yidian.yac.core.zap.Zap

/**
 * author: yinjiacheng
 * date: 8/4/21 5:06 PM
 * description: 非组件化方案 替代Application
 */
object BManagerCenter {

  /**
   * 作用同application onCreate
   */
  fun init() {
    registerXPage()
    initService()
    NightModeUtil.setContext(YdBaseApplication.context)
    // 工具类的使用需要先初始化，后续才能用
    DensityUtil.init(YdBaseApplication.context)
  }

  private fun registerXPage() {
    XPageManager.registeredNodes(
      arrayListOf(
        XPageNode(XRouterPathConstants.STORE_MANAGEMENT, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, StoreManagementActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.CHOOSE_STORE_ACTIVITY, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, ChooseStoreActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_IDENTITY, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountIdentityActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_AUTH, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountAuthActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_PERSONAL_AUTH, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountPersonalAuthActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_ENTERPRISE_AUTH, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountEnterpriseAuthActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_PERSONAL_AUTH_COMPLETE, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountPersonalAuthCompleteActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_ENTERPRISE_AUTH_COMPLETE, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountEnterpriseAuthCompleteActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_CREATE, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountCreateActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_CREATE_COMPLETE, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountCreateCompleteActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.CHOOSE_CAREER, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, ChooseCareerActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.PERSONAL_MY_STAFF, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, MyStaffActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.PERSONAL_MANAGER_INFO, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, ManagerInfoActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.PERSONAL_MANAGER_CHANGE, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, ManagerChangeActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.PERSONAL_STAFF_INFO, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, StaffInfoActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.PERSONAL_STAFF_MANAGE, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, StaffManageActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.PERSONAL_ADD_STAFF, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, AddStaffActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.MERCHANT_CENTER, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, MerchantCenterActivity::class.java)
            // 兼容B&C端
            if (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName) == ClientType.MERCHANT_B) {
              intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
            } else {
              // 同SingleTask
              intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
            }
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_INFO, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, MerchantInfoActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_MODIFY_NAME, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, LifeAccountModifyNameActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.LIFE_ACCOUNT_AUTH_INFO, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, MerchantAuthInfoActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.MY_WALLET, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, MyWalletActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.VERIFY_IDENTITY, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, VerifyIdentityActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.WECHAT_AUTH, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, WeChatAuthActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.WITHDRAW, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, WithdrawActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.TRANSACTION_LIST, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, TransactionListActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.TRANSACTION_DETAILS, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, TransactionDetailsActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
        XPageNode(XRouterPathConstants.AUTH_MANAGEMENT, XPageNodePageType.NATIVE, object : XPageHandler {
          override fun handler(params: Map<String, Any?>?) {
            val intent = Intent()
            if (params != null) {
              intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
            }
            intent.setClass(YdBaseApplication.context, MerchantAuthManagementActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            YdBaseApplication.context.startActivity(intent)
          }
        }),
      )
    )
  }

  private fun initService() {
    Zap.addService(ZapServiceNameConstants.LifeNumberListService, LifeNumberListService())
    Zap.addService(SelectStoreService.serviceName, SelectStoreService())
  }
}
