package com.cr.common

import android.content.Context
import android.content.SharedPreferences
import com.cr.cruav.CrApplication

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/11 09:37
 * 描述：一般存储的管理
 */
class CrSaveManager {
    // define: 2023/4/11 key对应变量
    val KEY_MAP_CENTER:String = "mapCenter"
    val KEY_LIVE_URL:String = "liveUrl"

    // define: 2023/4/11 本地化存储实例
    var preferences:SharedPreferences ?=null

    /**
     * 初始化
     * @constructor
     */
    constructor(){
        // todo: 2023/4/11 初始化本地存储实例
        preferences = CrApplication.getInstance()!!.getSharedPreferences("config", Context.MODE_PRIVATE)
    }

    // todo: 2023/4/11 静态方法
    companion object{
        /**
         * 获取单例
         * @return CrSaveManager
         */
        fun getInstance() = InstanceHelper.self
    }
    /**
     * 用于单例
     */
    object InstanceHelper{
        var self = CrSaveManager()
    }


    /**
     * 获取地图是否自动居中
     * @return Boolean
     */
    fun getMapCenter(): Boolean? {
        return preferences?.getBoolean(KEY_MAP_CENTER,true)
    }

    /**
     * 保存boolean类型key
     * @param key String
     * @param value Boolean
     */
    fun saveBooleanKey(key:String,value:Boolean){
        var editor:SharedPreferences.Editor = preferences!!.edit();
        editor.putBoolean(key,value);
        editor.commit()
    }

    /**
     * 获取boolean类型key
     * @param key String
     * @return Boolean
     */
    fun getBooleanKey(key:String):Boolean{
        return preferences!!.getBoolean(key,false)
    }

    /**
     * 保存string类型key
     * @param key String
     * @param value Boolean
     */
    fun saveStringKey(key:String,value:String){
        var editor:SharedPreferences.Editor = preferences!!.edit();
        editor.putString(key,value);
        editor.commit()
    }

    /**
     * 获取string类型key
     * @param key String
     * @return Boolean
     */
    fun getStringKey(key:String):String?{
        return preferences!!.getString(key,null)
    }

    /**
     * 设置地图是否自动居中
     * @param isCenter Boolean
     */
    fun setMapCenter(isCenter:Boolean){
        var editor:SharedPreferences.Editor = preferences!!.edit()
        editor.putBoolean(KEY_MAP_CENTER,isCenter)
        editor.commit()
    }

    /**
     * 获取存储的直播地址列表
     * @return List<String>
     */
    fun getLiveUrlList():MutableList<String>{
        var urls = preferences?.getString(KEY_LIVE_URL,"")
        var urlSplit = urls?.split(";")
        var resList:MutableList<String> = mutableListOf()
        for(i in 0 until urlSplit!!.size){
            resList.add(urlSplit[i])
        }
        return resList
    }

    /**
     * 存储url并返回最新集合
     * @param url String
     * @return MutableList<String>
     */
    fun saveLiveUrl(url:String):MutableList<String>{
        // todo: 2023/9/12 先获取
        var urlList = getLiveUrlList()
        if(urlList.indexOf(url) != -1) return urlList
        urlList.add(url)
        var editor:SharedPreferences.Editor = preferences!!.edit()
        editor.putString(KEY_LIVE_URL,urlList.joinToString(";"))
        editor.commit()
        return getLiveUrlList()
    }

}