require( "scripts/common/simple_object.lua" );

local	indicator_pos_x = 65;
local	indicator_pos_y = -15;

local ObjectTable = GetObjectTable("bookshelfputstudymaterialsback");

bookshelf2 = simple_object:new{
	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "holding_books",
	
	anchor_x=14,
	anchor_y=63,
	offset_x=-9,
	offset_y=-3,
	
	indicator_ui_x=indicator_pos_x,
	indicator_ui_y=indicator_pos_y,
	dad_ui_x=indicator_pos_x-35,
	dad_ui_y=indicator_pos_y,
	mom_ui_x=indicator_pos_x-35,
	mom_ui_y=indicator_pos_y,
	
	cleaning_state_time = 1000;
	
	texture="anim/objects/bookshelf/bookshelf",
	animation_script="anim/objects/bookshelf/bookshelf2_animation_script.lua",
	glow_texture="anim/objects/bookshelf/glow2/bookshelf_glow",
	glow_animation_script="anim/objects/default_glow_animation_script.lua",
	--flipped_texture = true,
	
	complex_object = true;
	dad_next_task_x = 25,
	dad_next_task_y = -100,
	mom_next_task_x = 25,
	mom_next_task_y = -100,

	messing_state_time = ObjectTable["messing_time"],
	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],
	
	is_a_mess = true;
}

function bookshelf2:new(o)
	o = o or {}   -- create object if user does not provide one
	setmetatable(o, self)
	self.__index = self
	return o
end
--[[
function bookshelf2:dirty_mouse_on()
	if not self.mouse_on then
		self.mouse_on = true;
		if GetCharacterState("dad") == self.parent_state_action then
			ShowDadIcon(self.object_name);
		end
		if GetCharacterState("mom") == self.parent_state_action then
			ShowMomIcon(self.object_name);
		end
		PlayObjectAnimation(self.object_name, self.object_dirty_mouse_on_animation);
	end
end
--]]

function bookshelf2:parent_action()
	self.parent_is_coming = false;
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	RemoveObjectFromClickedList(self.object_name);
	if GetCharacterState(self.parent_chosen) == self.parent_state_action and self.object_is_dirty then
		if self.parent_chosen == "dad" then
			PlaySound("working_father", true);
		else
			PlaySound("working_mother", true);
		end
		HideCharacterStuff(self.parent_chosen);
		PlayCharacterAnimation(self.parent_chosen, self.character_cleaning_animation);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		self:get_cleaning_state_time();
	else
		ShowMiss(self.object_name);
		--[[
		if self.stress_inc_enabled and not self.no_stress_power then
			AddCharacterStress(self.parent_chosen, self.stress_inc);
		end
		--]]
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		IncludeObjectMouseHandler(self.object_name);
		FinishCharacterTask(self.parent_chosen);
		self.parent_chosen = "";
		self.click_enable = self.set_click_enable;
		
		if self.object_is_dirty then
			--ChangeObjectState(self.object_name, self.dirty_state_name);			
			IncludeObjectMouseHandler(self.object_name);			
			self.click_enable = true;
			PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
			--PlayGlowAnimation(self.object_name, "dirty");
		end
	end
end

function bookshelf2:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		if self.parent_chosen == "dad" then
			StopSound("working_father");
			PlaySound("task_complete_father", false);
			HideDadNextTaskIcon(self.object_name);
		else
			StopSound("working_mother");
			PlaySound("task_complete_mother", false);
			HideMomNextTaskIcon(self.object_name);
		end
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);	
		SetCharacterState(self.parent_chosen, "normal");
		
		if self.stress_inc_enabled and not self.no_stress_power then
			AddCharacterStress(self.parent_chosen, self.stress_inc);
		end
		
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		FinishCharacterTask(self.parent_chosen);
				
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		
		PlayObjectAnimation("estante1", "clean");
		ChangeObjectState("estante1", "clean");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
		
		FinishTeamWorkTask(self.parent_chosen, "messes", 1);
	end
end