package com.dlc.chemical.sp

import android.content.Context
import com.dlc.chemical.common.Constants
import java.lang.Exception

/**
 * info
 *
 * @author ReiChin_
 */
object CameraSp {

    private const val SP_NAME = "camera_config_sp"
    private const val KEY_CAMERA_ID = "_camera_id"

    fun saveConfig(
        context: Context,
        cameraId: Int,
        type: CameraType
    ) {
        val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
        sp.edit()
            .putInt("${type.name}${KEY_CAMERA_ID}", cameraId)
            .apply()
    }

    fun loadConfig(
        context: Context,
        type: CameraType
    ): Int {
        val defCameraId = when (type) {
            CameraType.FACE -> Constants.CameraConfig.CAMERA_ID_FACE
            CameraType.VIDEO -> Constants.CameraConfig.CAMERA_ID_VIDEO
        }
        return try {
            val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
            sp.getInt("${type.name}${KEY_CAMERA_ID}", defCameraId)
        } catch (e: Exception) {
            e.printStackTrace()
            defCameraId
        }
    }

}

enum class CameraType {
    FACE, VIDEO
}