package com.yidianling.ydlcommon.custom.data

import com.meituan.android.walle.WalleChannelReader
import com.yidianling.ydlcommon.custom.GlobalInfo
import com.yidianling.ydlcommon.custom.router.YdlCommonOut

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/05/03
 */
class YdlRamImpl private constructor(): YdlRam {

    private var globalInfo: GlobalInfo? = null
    private val sessionNewYearKeywordMap: MutableMap<String, GlobalInfo.GlobalData.NewYearKeyword?> by lazy {
        HashMap<String, GlobalInfo.GlobalData.NewYearKeyword?>()
    }

    companion object {
        fun getInstance(): YdlRamImpl {
            return Holder.INSTANCE
        }
    }

    override fun getGlobalInfo(): GlobalInfo? {
        return globalInfo
    }

    override fun setGlobalInfo(globalInfo: GlobalInfo) {
        this.globalInfo = globalInfo
    }

    override fun putNewYearKeyword(key: String, value: GlobalInfo.GlobalData.NewYearKeyword?) {
        sessionNewYearKeywordMap[key] = value
    }

    override fun getNewYearKeyword(key: String): GlobalInfo.GlobalData.NewYearKeyword? {
        return sessionNewYearKeywordMap[key]
    }

    private val ydlChannel: String by lazy {
        val packageName = YdlCommonOut.getApp().packageName
        var channel = WalleChannelReader.getChannel(YdlCommonOut.getApp())
        if (channel.isNullOrEmpty()){
            channel="zhuzhan"
        }
        if (packageName.endsWith("yidianling")) {
            channel = "android_$channel"
        } else if (packageName.endsWith("yidianling_atk_3")) {
            channel = "ATK_3_android_$channel"
        } else if (packageName.endsWith("yidianling_atk4")) {
            channel = "ATK_4_android_$channel"
        } else if (packageName.endsWith("yidianling_atk5") || packageName.endsWith("yidianling_atk9")) {
            channel = "ATK_5_android_$channel"
        } else if (packageName.endsWith("yidianling_atk6")) {
            channel = "ATK_6_android_$channel"
        } else if (packageName.endsWith("yidianling_atk7")) {
            channel = "ATK_7_android_$channel"
        } else if (packageName.endsWith("xinlizixun")) {
            channel = "ATK_7_android_$channel"
        }
        channel ?: "android"
    }

    override fun getChannelName(): String {
        return ydlChannel
    }

    private object Holder {
        val INSTANCE = YdlRamImpl()
    }
}