--[[
Copyright 2012-2014 João Cardoso
PetTracker is distributed under the terms of the GNU General Public License (Version 3).
As a special exception, the copyright holders of this addon do not give permission to
redistribute and/or modify it.

This addon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the addon. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.

This file is part of PetTracker.
--]]

local Addon = PetTracker
local Tabs = LibStub('SecureTabs-1.0')
local Journal = PetTrackerTamerJournal
local L = Addon.Locals

-- GET_ITEM_INFO_RECEIVED

--[[ Startup ]]--

function Journal:Startup()
	HybridScrollFrame_CreateButtons(self.List, 'PetTrackerTamerEntry', 44, 0)
	hooksecurefunc('PetJournalParent_UpdateSelectedTab', function(...)
		Tabs:Update(...)
	end)

	self.Startup = function() end
	self:SetScript('OnShow', nil)
	self.List.scrollBar.doNotHide = true
	self.Count.Number:SetText(#PetTracker_Tamers)
	self.SearchBox:SetText(PetTracker_Sets.TamerSearch or SEARCH)
	self.SearchBox:SetScript('OnTextChanged', self.Search)

	self.Tab1.tip = TEAM
	self.Tab1.Icon:SetTexture('Interface/Icons/ability_hunter_pet_goto')
	self.Tab1.Icon:SetPoint('BOTTOM', 1, 1)
	self.Tab1.Icon:SetSize(27, 29)

	self.Tab2.tip = LOCATION_COLON:sub(0, -2)
	self.Tab2.Icon:SetTexture('Interface/Icons/inv_misc_map03')
	self.Tab2.Icon:SetPoint('BOTTOM', 1, 4)
	self.Tab2.Icon:SetSize(23, 23)

	self.Tab3.tip = HISTORY
	self.Tab3.Icon:SetTexture('Interface/Icons/achievement_challengemode_platinum')
	self.Tab3.Icon:SetPoint('BOTTOM', 1, 2)
	self.Tab3.Icon:SetSize(25, 25)
	
	local map = Addon.Map:Create(123, -43.6, 0)
	local spot = map:CreateTexture(nil, 'OVERLAY')
	spot:SetTexture('Interface/Glues/LoadingScreens/DynamicElements')
	spot:SetTexCoord(0, .5, 0, .5)
	spot:SetSize(30, 30)

	self.Spot = spot
	self.Map:SetScrollChild(map)
	self.Slots = PetTracker.JournalSlot:CreateLine('TOP', self.Team, 0, 104)
	self.Team.Border.Text:SetText(L.EnemyTeam)

	for i = 1, 4 do
		local loot = CreateFrame('Button', '$parentLoot' .. i, self.Card, 'PetTrackerReward')
		loot:SetPoint('TOPRIGHT', -58 + 45 * (i % 2), - 45 * ceil(i/2))
		loot.count:Show()

		self.Card[i] = loot
	end

	--[[for i = 1, 9 do
		local record = CreateFrame('Button', nil, self.History, 'PetTrackerFightRecord')
		record:SetPoint('TOP', 0, 40-55*i)
	end]]--

	self:SetTamer(Addon.Tamer:Get(1))
	self:SetTab(1)
end


--[[ Events ]]--

function Journal:Search()
	PetTracker_Sets.TamerSearch = self:GetText() ~= SEARCH and self:GetText() or nil
	self:GetParent().List:update()
end

function Journal:SetTab(tab)
	for i = 1,3 do
		local selected = i == tab
		local tab = self['Tab' .. i]

		tab.Hider:SetShown(not selected)
		tab.Highlight:SetShown(not selected)
		tab.TabBg:SetTexCoord(0.01562500, 0.79687500,
			selected and 0.78906250 or 0.61328125, selected and 0.95703125 or 0.78125000)
	end

	self.Team:SetShown(tab == 1)
	self.Map:SetShown(tab == 2)
	self.History:SetShown(tab == 3)
	self.Card:SetShown(tab ~= 3)
	self:Update()
end

function Journal:SetTamer(tamer)
	self:Startup()
	self.List.selected = tamer
	self.List:update()
	self:Update()
end


--[[ Redraw ]]--

function Journal:Update()
	local tamer = self.List.selected

	if self.Card:IsShown() then
		self.Card:Display(tamer)
	end

	if self.Team:IsShown() then
		for i, slot in ipairs(self.Slots) do
			slot:Display(tamer[i])
		end
	end

	if self.Map:IsShown() then
		SetMapByID(tamer.zone)
		self.Map:GetScrollChild():UpdateTiles()
		self.Spot:Show()

		for i = 1, GetNumMapLandmarks() do
			local _,_,_, x,y, _,_,_,_, spot = GetMapLandmarkInfo(i)
			if PetTracker_TamerLocations[spot] == tamer.id then
				return self.Map:GetScrollChild():PlacePOI(self.Spot, x, y)
			end
		end

		self.Spot:Hide()
	end
end

function Journal.List:update()
	local self = Journal.List
	local off = HybridScrollFrame_GetOffset(self)
	local tamers = {}

	for id in pairs(PetTracker_Tamers) do
		local tamer = Addon.Tamer:Get(id)
		if Addon:Filter(tamer, PetTracker_Sets.TamerSearch) then
			tinsert(tamers, tamer)
		end
	end

	for i, button in ipairs(self.buttons) do
		local tamer = tamers[i + off]

		if tamer then
			button.name:SetText(tamer.name)
			button.model:SetDisplayInfo(tamer.model)
			button.model.level:SetText(tamer:GetLevel())
			button.petTypeIcon:SetTexture(Addon:GetTypeIcon(tamer:GetType()))
			button.model.quality:SetVertexColor(Addon:GetQualityColor(tamer:GetQuality()))
			button.selectedTexture:SetShown(tamer.id == self.selected.id)
		end

		button:SetShown(tamer)
		button.tamer = tamer
	end

	HybridScrollFrame_Update(self, #tamers * 46, self:GetHeight())
end

function Journal.Card:Display(tamer)
	self.name:SetText(tamer.name)
	self.zone:SetText(tamer:GetZoneTitle())
	self.quest:SetShown(tamer.quest)
	self.model:SetDisplayInfo(tamer.model)
	self.model:SetRotation(.3)

	for i, slot in ipairs(self) do
		slot:Hide()
	end

	if tamer.quest ~= 0 then
		local completed = tamer:IsCompleted()
		local rewards = tamer:GetRewards()

		self.quest.status:SetText(tamer:GetCompleteState())
		self.quest.icon:SetDesaturated(completed)
		self.quest.ring:SetDesaturated(completed)
		self.quest.id = tamer.quest

		for i, loot in pairs(rewards) do
			local slot = self[i]
			slot.icon:SetTexture(loot.icon)
			slot.icon:SetDesaturated(completed)
			slot.count:SetText(loot.count)
			slot.link = loot.link
			slot:Show()
		end

		if tamer.gold > 0 then
			local slot = self[#rewards+1]
			slot.icon:SetTexture('Interface/icons/inv_misc_coin_01')
			slot.icon:SetDesaturated(completed)
			slot.count:SetText(tamer.gold)
			slot.link = nil
			slot:Show()
		end
	end
end


--[[ Start this Baby ]]--

Tabs:Startup(PetJournalParent, MountJournal, PetJournal)
Tabs:Add(PetJournalParent, Journal, 'Tamers')
Journal:SetScript('OnShow', Journal.Startup)