package com.za.base

import com.za.common.GlobalData
import com.za.net.RetrofitHelper

object AppConfig {
	var isRelease = false

	// API 相关地址
	lateinit var BASE_URL : String        // API 主地址
	lateinit var IMG_BASE_URL : String    // 图片服务器地址
	lateinit var Resource_URL : String    // 资源服务器地址


	// H5 相关地址
	lateinit var trainUrl : String       // 培训文档地址
	lateinit var documentUrl : String     // 中道资料地址
	lateinit var newDriverTrainUrl : String //  新司机培训地址


	fun init(isRelease : Boolean? = false) {
		val envType = GlobalData.networkEnv
		if (isRelease == true) {
			when (envType) {
				Const.NetEnv.Main -> release()
				Const.NetEnv.Review -> review()
			}
		} else {
			when (envType) {
				Const.NetEnv.CRM1 -> crm1()
				Const.NetEnv.CRM2 -> crm2()
				Const.NetEnv.UAT -> uat()
			}
		}
	}

	fun changeEnv(envType : Int) {
		GlobalData.networkEnv = envType
		when (envType) {
			Const.NetEnv.Main -> release()
			Const.NetEnv.Review -> review()
			Const.NetEnv.CRM1 -> crm1()
			Const.NetEnv.CRM2 -> crm2()
			Const.NetEnv.UAT -> uat()
		}
		RetrofitHelper.reset()
	}


	fun release() {
		isRelease = true        // API 配置
		GlobalData.networkEnv = Const.NetEnv.Main
		BASE_URL = "https://api.sinoassist.com"
		IMG_BASE_URL = "https://api.sinoassist.com"
		Resource_URL = "https://www.sinoassist.com/res"

		// H5 配置
		trainUrl = "https://www.sinoassist.com/h5/supplier/dispatch/diverTrainDocment"
		documentUrl = "https://www.sinoassist.com/h5/supplier/dispatch/docmentList"
		newDriverTrainUrl = "https://www.sinoassist.com/h5/supplier/dispatch/driverTrainingList";
	}


	/**
	 * 审核环境配置
	 */
	private fun review() {
		isRelease = true        // API 配置
		GlobalData.networkEnv = Const.NetEnv.Review

		// API 配置
		BASE_URL = "http://interface.review.sino-assist.com"
		IMG_BASE_URL = "http://interface.review.sino-assist.com"
		Resource_URL = "https://www.sinoassist.com/res"
		documentUrl = "http://interface.review.sino-assist.com/h5/supplier/dispatch/docmentList"
		trainUrl = "http://interface.review.sino-assist.com/h5/supplier/dispatch/diverTrainDocment"
		newDriverTrainUrl =
			"http://interface.review.sino-assist.com/h5/supplier/dispatch/driverTrainingList"
	}

	/**
	 * CRM1 环境配置
	 */
	fun crm1() {
		isRelease = false
		GlobalData.networkEnv = Const.NetEnv.CRM1

		// API 配置
		BASE_URL = "https://api1.sino-assist.com"
		IMG_BASE_URL = "https://api1.sino-assist.com"
		Resource_URL = "https://crm1.sino-assist.com/res"

		documentUrl = "https://crm1.sino-assist.com/h5/supplier/dispatch/docmentList";
		trainUrl = "https://crm1.sino-assist.com/h5/supplier/dispatch/diverTrainDocment";
		newDriverTrainUrl = "https://crm1.sino-assist.com/h5/supplier/dispatch/driverTrainingList";
	}

	/**
	 * CRM2 环境配置
	 */
	fun crm2() {
		isRelease = false
		GlobalData.networkEnv = Const.NetEnv.CRM2

		// API 配置
		BASE_URL = "https://api2.sino-assist.com"
		IMG_BASE_URL = "https://api2.sino-assist.com"
		Resource_URL = "https://crm2.sino-assist.com/res"
	}


	fun uat() {
		isRelease = false
		GlobalData.networkEnv = Const.NetEnv.UAT

		BASE_URL = "https://api-uat.sino-assist.com" //crm2
		IMG_BASE_URL = "https://api-uat.sino-assist.com"
		Resource_URL = "https://uat.sino-assist.com/res"
		documentUrl = "https://uat.sino-assist.com/h5/supplier/dispatch/docmentList"
		trainUrl = "https://uat.sino-assist.com/h5/supplier/dispatch/diverTrainDocment"
		newDriverTrainUrl = "https://uat.sino-assist.com/h5/supplier/dispatch/driverTrainingList"
		documentUrl = "https://uat.sino-assist.com/h5/supplier/dispatch/docmentList";
		trainUrl = "https://uat.sino-assist.com/h5/supplier/dispatch/diverTrainDocment";
		newDriverTrainUrl = "https://uat.sino-assist.com/h5/supplier/dispatch/driverTrainingList";
	}

	/**
	 * 获取培训文档完整地址
	 * @param driverId 司机ID
	 * @param keyword 关键字
	 * @return 完整的培训文档URL
	 */
	fun getTrainUrl(keyWord : String = "") : String {
		if (keyWord.isEmpty()) {
			return trainUrl + "?token=${GlobalData.token}&driverId=${GlobalData.driverInfoBean?.userId}"
		}
		return trainUrl + "?token=${GlobalData.token}&driverId=${GlobalData.driverInfoBean?.userId}&keyword=$keyWord"
	}

	/**
	 * 获取中道资料完整地址
	 * @param driverId 司机ID
	 * @param keyword 关键字
	 * @return 完整的中道资料URL
	 */
	fun getDocmentUrl(keyWord : String = "") : String {
		if (keyWord.isEmpty()) {
			return documentUrl + "?token=${GlobalData.token}&driverId=${GlobalData.driverInfoBean?.userId}"
		}
		return documentUrl + "?token=${GlobalData.token}&driverId=${GlobalData.driverInfoBean?.userId}&keyword=$keyWord"
	}
}
