local achievements = {
	["rooms1"]={
		t="rooms",
		amount=2000,
		steamStatName="rooms"
	},
	["enemies1"]={
		t="enemies",
		amount=500,
		steamStatName="enemies"
	},
	["boss1"]={
		t="level",
		st="1_5"
	},
	["boss2"]={
		t="level",
		st="2_5"
	},
	["boss3"]={
		t="level",
		st="3_5"
	},
	["boss4"]={
		t="level",
		st="4_5"
	},
	["boss5"]={
		t="level",
		st="5_5"
	},
	["tutorial1"]={
		t="level",
		st="1_0"
	},
	["complete1"]={
		t="level",
		st="5_5"
	},
	["height1"]={
		t="height",
		amount=60*10
	},
	["height2"]={
		t="height",
		amount=60*20
	},
	["cost1"]={
		t="profit",
		amount=2000
	},
	["bullets1"]={
		t="bulletLevel",
		amount=500,
		steamStatName="bullets_per_level"
	},
	["bullets2"]={
		t="bulletLife",
		steamStatName="bullets_fired",
		amount=100000
	},
}

-- tracks the progress of achievements and handles submitting them to apple
AchievementsModel = class(function(o)
	o.capsUnlocked = 0
	o.capsUpdate = false
	o.terrainsCrossed = {}
	for i=1,8 do
		o.terrainsCrossed[i] = 0
	end
	o.beatsHarvested = 0
	o.planetsPurchased = 0 -- I wonder if this should just be based off of the player.galaxy thing
	o.pugPlanet = 0 -- use the maximum at any point
	o.achievements = achievements
end)
function AchievementsModel:addBulletLevel(num)
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "bulletLevel" then
			if num >= v.amount and not v.complete then
				v.dirty = true
				v.progress = v.amount
				report = true
			end
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:addBulletLifetime(num)
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "bulletLife" then
			if not v.progress then v.progress = 0 end
			v.progress = v.progress+num
			v.dirty = true
			if not v.complete then
				report = true
			end
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:addRoom()
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "rooms" then
			if not v.progress then v.progress = 0 end
			v.progress = v.progress+1
			v.dirty = true
			if not v.complete then
				report = true
			end
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:completeLevel(levelName)
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "level" then
			if v.st == levelName then
				v.progress = 1
				v.amount = 1
				v.dirty = true
				report = true
			end
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:newHeight(height)
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "height" then
			if not v.progress then v.progress = 0 end
			v.progress = math.max(height, v.progress)
			v.dirty = true
			if not v.complete then
				report = true
			end
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:newMoney(money)
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "profit" then
			if not v.progress then v.progress = 0 end
			v.progress = math.max(money, v.progress)
			v.dirty = true
			report = true
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:useDraft()
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "draft" then
			v.dirty = true
			v.amount = 100
			v.progress = 100
			report = true
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:killedEnemy()
	local report = false
	for i,v in pairs(self.achievements) do
		if v.t == "enemies" then
			if not v.progress then v.progress = 0 end
			v.progress = v.progress+1
			v.dirty = true
			if not v.complete then
				report = true
			end
		end
	end
	if report then
		self:submitUpdates()
	end
end
function AchievementsModel:submitUpdates()
	-- only submit updates if we are connected to steam
	if not scf:isSteamConnected() then
		return
	end
	for i,v in pairs(self.achievements) do
		if v.dirty and not v.complete and not v.reporting then
			v.reporting = true
			v.dirty = false
			-- get a new reporting object
			local sa = SteamAchievements()
			-- attempt to report the stat
			if v.steamStatName then
				sa:SetStatInt(v.steamStatName, v.progress)
			end
			if v.progress/v.amount*100 >= 100 then
				sa:SetAchievementComplete(i)
				v.complete = true
			end
			v.reporting = false
		end
	end
end

function AchievementsModel:saveTo(data)
	data.achievements = self.achievements
	return data
end

function AchievementsModel:loadFrom(data)
	if data.achievements then
		for i,v in pairs(data.achievements) do
			if self.achievements[i] and self.achievements[i].steamStatName then
				v.steamStatName = self.achievements[i].steamStatName
			end
			self.achievements[i] = v
			self.achievements[i].reporting = false
		end
	end
end
