package com.test.basd.onecleanmaster.display

import android.content.Context
import android.telephony.TelephonyManager
import com.blankj.utilcode.util.SPUtils
import com.test.basd.onecleanmaster.helps.BaseApplication

/**
 * 判断用户渠道的方法
 */
object UserChancelEx {

    fun getUserChancelSwitch(int: Int): Boolean {
        val isOrganic = isOrganicUser()
        val isHasSim = isSimCardAvailable()
        return l(int, isHasSim, isOrganic)
    }

    fun l(i2: Int, isHasSim: Boolean, isOrganic: Boolean): Boolean {
        return if (!isOrganic && !k(i2, 3)) {
            false
        } else if (isOrganic && !k(i2, 2)) {
            false
        } else if (!isHasSim && !k(i2, 1)) {
            false
        } else if (isHasSim && !k(i2, 0)) {
            false
        } else k(i2, 4)
    }

    fun k(i2: Int, i3: Int): Boolean {
        return i2 shr i3 and 1 == 1
    }

    /**
     * 是否是自然用户
     */
     fun isOrganicUser(): Boolean {
        val source: String = SPUtils.getInstance().getString("install_info_source","")
        return !(source.contains("gclid") || source.contains("facebook"))
    }

     fun isSimCardAvailable(): Boolean {
        val telephonyManager = BaseApplication.context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return telephonyManager.simState != TelephonyManager.SIM_STATE_ABSENT
    }
}