local LocalData ={}

local PlayerPrefs = CS.UnityEngine.PlayerPrefs

local LOCAL_DATA_KEY = {
	SDK_UID = "SDK_UID",
	AUDIO_MUSIC_VOLUME = "AUDIO_MUSIC_VOLUME",
	AUDIO_EFFECT_VOLUME = "AUDIO_EFFECT_VOLUME",
	SELECTED_LANGUAGE = "SELECTED_LANGUAGE",
	GM_SHOW_FLOATING_ICON = "GM_SHOW_FLOATING_ICON",
	MESSAGE_BOX_SHOW_TODAY = "MESSAGE_BOX_SHOW_TODAY",
	GAME_QUALITY_LEVEL = "GAME_QUALITY_LEVEL",										-- 游戏设置品质等级
	LAST_LOGIN_URL = "LAST_LOGIN_URL",
	LAST_LOGIN_NAME = "LAST_LOGIN_NAME",
	LAST_LOGIN_IP = "LAST_LOGIN_IP",
	LAST_LOGIN_INFO = "LAST_LOGIN_INFO", -- 上一次登录成功的信息
	LAST_LOGIN_TYPE = "LAST_LOGIN_TYPE", -- 上次登录类型 token不记录
	ACCOUNT_INFO = "ACCOUNT_INFO",
	SEND_QUEUE = "SEND_QUEUE",
	SDK_LOGIN_TYPE = "SDK_LOGIN_TYPE",

	NEED_UPDATE = "NEED_UPDATE",								-- 需要更新
	IOS_ORDER_ID = "IOS_ORDER_ID",
	IOS_PAY_INFO = "IOS_PAY_INFO",
	SHAKE_MODE = "SHAKE_MODE", -- 震动模式
	SAVE_POWER_MODE = "SAVE_POWER_MODE", -- 省电模式
	LAST_MAIL_ID = "LAST_MAIL_ID",
	IS_NEW_PLAYER = "IS_NEW_PLAYER",
	DISTINCT_ID = "DISTINCT_ID",
	LAST_LOGIN_TIME = "LAST_LOGIN_TIME",
	GATE = "GATE",
	BOUNTY_POP_TIME = "BOUNTY_POP_TIME",
	CHALLENGE_TASK_POP_TIME = "CHALLENGE_TASK_POP_TIME",
	NEW_AVATAR_AND_FRAME = "NEW_AVATAR_AND_FRAME",
	SHOP_DISCOUNT_RED_POINT_TIME = "SHOP_DISCOUNT_RED_POINT_TIME",
	ARENA_BOUNTY_POP_TIME = "ARENA_BOUNTY_POP_TIME",
	LINK_TOUCH_CANCEL_COUNT = "LINK_TOUCH_CANCEL_COUNT",
	ARENA_TODAY_BATTLE_COUNT = "ARENA_TODAY_BATTLE_COUNT",
	TRIAL_HERO = "TRIAL_HERO",
	BATTLE_SNAPSHOT = "BATTLE_SNAPSHOT",
	ACTIVITY_SUMMER_TODAY_SKIN = "ACTIVITY_SUMMER_TODAY_SKIN",
	ACTIVITY_SUMMER_TODAY_HERO = "ACTIVITY_SUMMER_TODAY_HERO",
	NOT_POP_TODAY = "NOT_POP_TODAY",
	FOURTEEN_DAY_TODAY_EXCHANGE = "FOURTEEN_DAY_TODAY_EXCHANGE",
	ACT_BOSS_RUSH_EXCHANGE = "ACT_BOSS_RUSH_EXCHANGE",
	ACT_PVP_SHOW_HELP = "ACT_PVP_SHOW_HELP",
}

LocalData.KEYS = LOCAL_DATA_KEY

function LocalData:save()
	if EDITOR_MODE then
		Logger.logHighlight("save data")
	end
	if self.dirty then
		self.dirty = false
		PlayerPrefs.Save()
	end
end

function LocalData:setString(key, value)
	self.dirty = true
	PlayerPrefs.SetString(key, value)
end

function LocalData:getString(key, defaultValue)
	if defaultValue then
		return PlayerPrefs.GetString(key, defaultValue)
	else
		return PlayerPrefs.GetString(key)
	end
end

function LocalData:setInt(key, value)
	self.dirty = true
    PlayerPrefs.SetInt(key, value)
end

function LocalData:getInt(key, defaultValue)
	if defaultValue then
		return PlayerPrefs.GetInt(key, defaultValue)
	else
		return PlayerPrefs.GetInt(key)
	end
end

function LocalData:setFloat(key, value)
	self.dirty = true
    PlayerPrefs.SetFloat(key, value)
end

function LocalData:getFloat(key, defaultValue)
	if defaultValue then
		return PlayerPrefs.GetFloat(key, defaultValue)
	else
		return PlayerPrefs.GetFloat(key)
	end
end

function LocalData:hasKey(key)
    return PlayerPrefs.HasKey(key)
end

function LocalData:delKey(key)
	self.dirty = true
	PlayerPrefs.DeleteKey(key)
end

-- 跟角色挂钩的唯一id
function LocalData:getRoleKey(key)
	local uid = DataManager.PlayerData:getAcountId()
	if uid == "" then
		Logger.logError("check roleid error!")
	end
	return key .. uid
end

--------------Implement-----------------
function LocalData:getAudioMusicVolume()
	return self:getFloat(LOCAL_DATA_KEY.AUDIO_MUSIC_VOLUME, 1)
end

function LocalData:setAudioMusicVolume(value)
	self:setFloat(LOCAL_DATA_KEY.AUDIO_MUSIC_VOLUME, value)
end

function LocalData:getAudioEffectVolume()
	return self:getFloat(LOCAL_DATA_KEY.AUDIO_EFFECT_VOLUME, 1)
end

function LocalData:setAudioEffectVolume(value)
	self:setFloat(LOCAL_DATA_KEY.AUDIO_EFFECT_VOLUME, value)
end

function LocalData:getSelectedLanguage()
	return self:getString(LOCAL_DATA_KEY.SELECTED_LANGUAGE, "")
end

function LocalData:setSelectedLanguage(value)
	self:setString(LOCAL_DATA_KEY.SELECTED_LANGUAGE, value)
end

function LocalData:getGMShowFloatingIcon()
	return self:getInt(LOCAL_DATA_KEY.GM_SHOW_FLOATING_ICON, 0) == 1
end

function LocalData:setGMShowFloatingIcon(value)
	self:setInt(LOCAL_DATA_KEY.GM_SHOW_FLOATING_ICON, value)
end

function LocalData:getMessageBoxShowTodayTime(key)
	return self:getInt(LOCAL_DATA_KEY.MESSAGE_BOX_SHOW_TODAY .. key, 0)
end

function LocalData:setMessageBoxShowTodayTime(key, value)
	self:setInt(LOCAL_DATA_KEY.MESSAGE_BOX_SHOW_TODAY .. key, value)
end

function LocalData:getGameQualityLevel()
	return self:getInt(LOCAL_DATA_KEY.GAME_QUALITY_LEVEL, 0)
end

function LocalData:setGameQualityLevel(level)
	self:setInt(LOCAL_DATA_KEY.GAME_QUALITY_LEVEL, level)
end

function LocalData:setTodayFirst(new)
	self.isTodayFirst = new
end

function LocalData:getTodayFirst()
	return self.isTodayFirst or false
end

function LocalData:setNeedUpdate(value)
	value = value or "0.0.0"
	self:setString(LOCAL_DATA_KEY.NEED_UPDATE, value)
end

function LocalData:getNeedUpdate()
	self:getString(LOCAL_DATA_KEY.NEED_UPDATE, "0.0.0")
end

function LocalData:setIosPayInfo(iosPayInfo)
	iosPayInfo = iosPayInfo or {}
	local str = json.encode(iosPayInfo)
	self:setString(LOCAL_DATA_KEY.IOS_PAY_INFO, str)
end

function LocalData:getIosPayInfo()
	local str = self:getString(LOCAL_DATA_KEY.IOS_PAY_INFO, "")
	if str == nil or str == "" then
		return {}
	else
		local iosPayInfo = json.decode(str)
		if iosPayInfo then
			return iosPayInfo
		else
			return {}
		end
	end
end

function LocalData:setIosOrders(iosOrders)
	iosOrders = iosOrders or {}
	local str = json.encode(iosOrders)
	self:setString(LOCAL_DATA_KEY.IOS_ORDER_ID, str)
end

function LocalData:getIosOrders()
	local str = self:getString(LOCAL_DATA_KEY.IOS_ORDER_ID, "")
	if str == nil or str == "" then
		return {}
	else
		local iosOrders = json.decode(str)
		if iosOrders then
			return iosOrders
		else
			return {}
		end
	end
end

function LocalData:setLastLoginInfo(loginType, id, token)
	local str = json.encode({
		type = loginType,
		id = id,
		token = token
	})
	if not loginType then
		self:setString(LOCAL_DATA_KEY.LAST_LOGIN_TYPE, "")
	elseif loginType ~= "token" then
		self:setString(LOCAL_DATA_KEY.LAST_LOGIN_TYPE, loginType)
	end
	self:setString(LOCAL_DATA_KEY.LAST_LOGIN_INFO, str)
end

function LocalData:setLastLoginType(loginType)
	self:setString(LOCAL_DATA_KEY.LAST_LOGIN_TYPE, loginType)
end

function LocalData:getLastLoginInfo()
	local str = self:getString(LOCAL_DATA_KEY.LAST_LOGIN_INFO, "{}")
	local info = json.decode(str) or {}
	info.type = info.type or NetManager.LOGIN_TYPE.ANONYMOUS
	if type(info.type) == "number" then
		if info.type == SDKManager.BF_LOGIN_TYPE.GOOGLE then
			info.type = NetManager.LOGIN_TYPE.GOOGLE
		end
	end
	info.id = info.id or DeviceHelper:getDeviceId()
	info.token = info.token
	return info
end

function LocalData:getLastLoginType()
	local str = self:getString(LOCAL_DATA_KEY.LAST_LOGIN_TYPE, "")
	if str == "" then
		str = NetManager.LOGIN_TYPE.ANONYMOUS
	end
	if str ~= NetManager.LOGIN_TYPE.ANONYMOUS and
		str ~= NetManager.LOGIN_TYPE.APPLE and
		str ~= NetManager.LOGIN_TYPE.GOOGLE and
		str ~= NetManager.LOGIN_TYPE.FACEBOOK
	 then
		str = NetManager.LOGIN_TYPE.ANONYMOUS
	end
	return str
end

function LocalData:setLastLoginName(name)
	name = name or ""
	self:setString(LOCAL_DATA_KEY.LAST_LOGIN_NAME, name)
end

function LocalData:getLastLoginName()
	return self:getString(LOCAL_DATA_KEY.LAST_LOGIN_NAME, "")
end

function LocalData:saveSendQueue(sendQueue)
	local str = json.encode(sendQueue)
	self:setString(LOCAL_DATA_KEY.SEND_QUEUE, str)
end

function LocalData:saveEmptySendQueue()
	if self.emptyTableJsonStr == nil then
		self.emptyTableJsonStr = json.encode({})
	end
	self:setString(LOCAL_DATA_KEY.SEND_QUEUE, self.emptyTableJsonStr)
end

function LocalData:getSendQueue()
	local sendQueue = json.decode(self:getString(LOCAL_DATA_KEY.SEND_QUEUE, "{}"))
	return sendQueue
end

function LocalData:setShakeMode(value) -- 0-close 1-open
	self:setInt(self:getString(LOCAL_DATA_KEY.SHAKE_MODE), value)
end

function LocalData:getShakeMode()
	self:getInt(self:getString(LOCAL_DATA_KEY.SHAKE_MODE), 1)
end

function LocalData:setSavePowerMode(value) -- 0-close 1-open
	self:setInt(self:getString(LOCAL_DATA_KEY.SAVE_POWER_MODE), value)
end

function LocalData:getSavePowerMode()
	self:getInt(self:getString(LOCAL_DATA_KEY.SAVE_POWER_MODE), 1)
end

function LocalData:getLastMailId()
	return self:getInt(LOCAL_DATA_KEY.LAST_MAIL_ID, 0)
end

function LocalData:setLastMailId(id)
	if not id then
		return
	end
	self:setInt(LOCAL_DATA_KEY.LAST_MAIL_ID, id)
end

function LocalData:getAccountInfo()
	local info = json.decode(self:getString(LOCAL_DATA_KEY.ACCOUNT_INFO, "{}"))
	return info
end

function LocalData:setAccountInfo(info)
	if not info then
		return
	end
	local str = json.encode(info)
	self:setString(LOCAL_DATA_KEY.ACCOUNT_INFO, str)
end

function LocalData:setLastLoginIp(ip)
	ip = ip or ""
	self:setString(LOCAL_DATA_KEY.LAST_LOGIN_IP, ip)
end

function LocalData:getLastLoginIp()
	return self:getString(LOCAL_DATA_KEY.LAST_LOGIN_IP, "")
end

function LocalData:getIsNewPlayer()
	return self:getInt(LOCAL_DATA_KEY.IS_NEW_PLAYER, 0) == 0
end

function LocalData:setNotNewPlayer()
	return self:setInt(LOCAL_DATA_KEY.IS_NEW_PLAYER, 1)
end

function LocalData:setDistinctId(id)
	self:setString(LOCAL_DATA_KEY.DISTINCT_ID, id or "")
end

function LocalData:getDistinctId()
	return self:getString(LOCAL_DATA_KEY.DISTINCT_ID, "")
end

function LocalData:setLastLoginTime()

end

function LocalData:setBountyPopTime(time)
	self:setInt(LOCAL_DATA_KEY.BOUNTY_POP_TIME, time)
end

function LocalData:getBountyPopTime()
	return self:getInt(LOCAL_DATA_KEY.BOUNTY_POP_TIME, 0)
end

function LocalData:setChallengeTaskPopTime(time)
	self:setInt(LOCAL_DATA_KEY.CHALLENGE_TASK_POP_TIME, time)
end

function LocalData:getChallengeTaskPopTime()
	return self:getInt(LOCAL_DATA_KEY.CHALLENGE_TASK_POP_TIME, 0)
end

function LocalData:getShopDiscountRedPointTime()
	return self:getInt(LOCAL_DATA_KEY.SHOP_DISCOUNT_RED_POINT_TIME, 0)
end

function LocalData:setShopDiscountRedPointTime(time)
	self:setInt(LOCAL_DATA_KEY.SHOP_DISCOUNT_RED_POINT_TIME, time)
end

function LocalData:getNewAvatarAndFrame()
	local info = json.decode(self:getString(LOCAL_DATA_KEY.NEW_AVATAR_AND_FRAME, "{}"))
	-- Logger.logHighlight("获取新头像/头像框状态")
	-- Logger.printTable(info)
	return info
end

function LocalData:setNewAvatarAndFrame(info)
	-- Logger.logHighlight("保存新头像/头像框状态")
	-- Logger.printTable(info)
	if not info then
		return
	end
	local str = json.encode(info)
	self:setString(LOCAL_DATA_KEY.NEW_AVATAR_AND_FRAME, str)
end

function LocalData:setArenaBountyPopTime(time)
	self:setInt(LOCAL_DATA_KEY.ARENA_BOUNTY_POP_TIME, time)
end

function LocalData:getArenaBountyPopTime()
	return self:getInt(LOCAL_DATA_KEY.ARENA_BOUNTY_POP_TIME, 0)
end

function LocalData:setLinkTouchCancelCount(count)
	self:setInt(LOCAL_DATA_KEY.LINK_TOUCH_CANCEL_COUNT, count)
end

function LocalData:getLinkTouchCancelCount()
	return self:getInt(LOCAL_DATA_KEY.LINK_TOUCH_CANCEL_COUNT, 0)
end

function LocalData:getTodayArenaBattleCount()
	return self:getInt(LOCAL_DATA_KEY.ARENA_TODAY_BATTLE_COUNT .. Time:getBeginningOfServerToday(), 0)
end

function LocalData:recordTodayArenaBattle()
	local value = self:getTodayArenaBattleCount()
	self:setInt(LOCAL_DATA_KEY.ARENA_TODAY_BATTLE_COUNT .. Time:getBeginningOfServerToday(), value + 1)
end

function LocalData:MarkTrailHero()
	self:setInt(LocalData:getRoleKey(LOCAL_DATA_KEY.TRIAL_HERO), 1)
end

function LocalData:GetMarktrailHero()
	return self:getInt(LocalData:getRoleKey(LOCAL_DATA_KEY.TRIAL_HERO), 0)
end

function LocalData:saveBattleSnapshot(snapshot)
	if EDITOR_MODE then
		Logger.logHighlight(json.encode(snapshot))
	end
	self:setString(LocalData:getRoleKey(LOCAL_DATA_KEY.BATTLE_SNAPSHOT), json.encode(snapshot))
	LocalData:save()
end

function LocalData:getBattleSnapshot()
	local dealedSnapshot = self:getString(LocalData:getRoleKey(LOCAL_DATA_KEY.BATTLE_SNAPSHOT), "{}")
	if EDITOR_MODE then
		Logger.logHighlight(dealedSnapshot)
	end
	return json.decode(dealedSnapshot)
end

function LocalData:getTodayActSummerWatchedSkin()
	return self:getInt(LOCAL_DATA_KEY.ACTIVITY_SUMMER_TODAY_SKIN .. Time:getBeginningOfServerToday(), 0) == 1
end

function LocalData:recordTodayActSummerWatchedSkin()
	self:setInt(LOCAL_DATA_KEY.ACTIVITY_SUMMER_TODAY_SKIN .. Time:getBeginningOfServerToday(), 1)
end

function LocalData:getTodayActSummerWatchedHero()
	return self:getInt(LOCAL_DATA_KEY.ACTIVITY_SUMMER_TODAY_HERO .. Time:getBeginningOfServerToday(), 0) == 1
end

function LocalData:recordTodayActSummerWatchedHero()
	self:setInt(LOCAL_DATA_KEY.ACTIVITY_SUMMER_TODAY_HERO .. Time:getBeginningOfServerToday(), 1)
end

function LocalData:setNotPopToday(customKey)
	self:setInt(LOCAL_DATA_KEY.NOT_POP_TODAY .. customKey, Time:getBeginningOfServerToday())
end

function LocalData:getNotPopToday(customKey)
 	return self:getInt(LOCAL_DATA_KEY.NOT_POP_TODAY .. customKey, 0)
end

function LocalData:getTodayFourteenDayExchangeWatched()
	return self:getInt(LOCAL_DATA_KEY.FOURTEEN_DAY_TODAY_EXCHANGE .. Time:getBeginningOfServerToday(), 0) == 1
end

function LocalData:recordTodayFourteenDayExchangeWatched()
	self:setInt(LOCAL_DATA_KEY.FOURTEEN_DAY_TODAY_EXCHANGE .. Time:getBeginningOfServerToday(), 1)
end

function LocalData:getTodayBossRushExchangeWatched()
	return self:getInt(LOCAL_DATA_KEY.ACT_BOSS_RUSH_EXCHANGE, 0)
end

function LocalData:recordTodayBossRushExchangeWatched()
	self:setInt(LOCAL_DATA_KEY.ACT_BOSS_RUSH_EXCHANGE, Time:getBeginningOfServerToday())
end

function LocalData:getActPvpShowHelpTag(actId)
	actId = actId or 0
	return self:getInt(LOCAL_DATA_KEY.ACT_PVP_SHOW_HELP .. actId, 0)
end

function LocalData:recordActPvpShowHelpTag(actId)
	actId = actId or 0
	self:setInt(LOCAL_DATA_KEY.ACT_PVP_SHOW_HELP .. actId, 1)
end

return LocalData