package com.ydl.ydlcommon.data

import com.meituan.android.walle.WalleChannelReader
import com.ydl.ydlcommon.base.BaseApp
import com.ydl.ydlcommon.base.config.YDLConstants
import com.ydl.ydlcommon.bean.GlobalInfo
import com.ydl.ydlcommon.router.YdlCommonOut

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

    private var globalInfo: GlobalInfo? = null

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

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

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

    private val ydlChannel: String by lazy {
        val packageName = YdlCommonOut.getApp().packageName
        var channel = WalleChannelReader.getChannel(YdlCommonOut.getApp())
        if (channel.isNullOrEmpty()){
            if (BaseApp.instance.getGlobalConfig().appFrom == YDLConstants.FROM_YDL){
                channel="zhuzhan"
            }else{
                channel="ATK_7_android_zhuzhan"
            }
            return@lazy channel!!
        }
        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 = PlatformRamImpl()
    }
}