package com.bc_chat.bc_base.utils

import android.content.Context
import android.provider.Settings
import android.telephony.TelephonyManager

import com.bc_chat.bc_base.entity.User
import com.bc_chat.bc_base.entity.dao.GroupMemberDao
import com.zhaohaoting.framework.application.ZhtApplication
import com.zhaohaoting.framework.utils.Logger
import com.zhaohaoting.framework.utils.Preferences
import org.litepal.LitePal
import org.litepal.crud.DataSupport
import java.util.*


/**
 * Created by zhaohaoting on 2018-4-17.
 */
class AppConfig {
    // 'notice_setting:{"msg_notice":false,"msg_notice_obj":{"voice_notice":true,"vibrator_notice":true},"voice_call_notice":false}'
    private val APP_CONFIG = "config"
    private var user: User? = null
    private val customerService: String? = null //客服电话

    companion object {
        //消息提示设置
        val NOTICE_SETTING = "notice_setting"
        //消息提示开关 关掉后同时要关掉声音和震动
        val MSG_NOTICE = "msg_notice"
        //声音
        val VOICE_NOTICE = "voice_notice"
        //震动
        val VIBRATOR_NOTICE = "vibrator_notice"
        //声音和震动
        val MSG_NOTICE_OBJ = "msg_notice_obj"
        //电话和视频提醒
        val VOICE_CALL_NOTICE = "voice_call_notice"
        val VERSION_KEY = "version_code"//版本记录
        val GUIDE_SHOW = "GUIDE_SHOW"//版本记录

        val instance: AppConfig by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            AppConfig()
        }
    }

    fun getUser(): User {
        if (user == null) {
            val lastUserId = Preferences.getString(Const.LAST_LOGIN_USER_ID, "0")
            val saveUser = GroupMemberDao.instance.finUserById(lastUserId)
            if (saveUser != null) {
                user = saveUser
                return user as User
            }
            user = User()
        }
        return user as User
    }

    fun deleteUser() {
        user = User()
    }

    // 获取设备唯一码
    fun geUUID(context: Context): String {
        val tm = ZhtApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val deviceId = tm.deviceId
        //        String simSerialNumber = tm.getSimSerialNumber();
        val androidId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        //        UUID  uuid = new UUID((long) androidId.hashCode(), (long) deviceId.hashCode() << 32 | (long) simSerialNumber.hashCode());
        val uuid = UUID(androidId.hashCode().toLong(), deviceId.hashCode().toLong() shl 32)
        return uuid.toString()
    }
}
