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

local	indicator_pos_x = -5;
local	indicator_pos_y = -20;

outsidetable3 = simple_object:new{
	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "normal",
	anchor_x=83,
	anchor_y=135,
		
	cleaning_state_time = 5000;
	
	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,
	
	kids_on_table = false;
	
	complex_object = true;
	dad_next_task_x = indicator_pos_x+67,
	dad_next_task_y = indicator_pos_y,
	mom_next_task_x = indicator_pos_x-7,
	mom_next_task_y = indicator_pos_y+29,
	
	child_members = {};
	dad_coming = false;
	mom_coming = false;
	
	stress_inc_enabled = false;
}

function outsidetable3:new(o)
	o = o or {}   -- create object if user does not provide one
	setmetatable(o, self)
	self.__index = self
	return o
end

function outsidetable3:sit_character(character_name)
	PlayCharacterAnimation(character_name, "sit_outside");
	SetCharacterState(character_name, "sit_outside");
	HideCharacter(character_name);
	ChangeObjectState(character_name.."eatingoutside", "idle");
	PutCharacterOnObject(character_name, "mesafora3");
end

function outsidetable3:dirty_update()
	local n = table.getn(self.child_members);
	
	if not self.kids_on_table then
		self.kids_on_table = true;	
		for i = 1 , n do
			if GetCharacterState(self.child_members[i]) ~= "sit_outside" then
				self.kids_on_table = false;			
			end
		end
	else		
		if GetCharacterState("dad") == "sit_outside" and
			GetCharacterState("mom") == "sit_outside" and
			self.kids_on_table then
			ChangeObjectState("momeatingoutside", "playing");
			ChangeObjectState("dadeatingoutside", "playing");
			
			for i = 1 , n do
				ChangeObjectState(self.child_members[i].."eatingoutside", "playing");
			end
			
			ChangeObjectState(self.object_name, self.cleaning_state_name);
			self.new_state_start_time = GetCurrentTime();
			
			StartResetStressMeter(self.cleaning_state_time / 1000.0);
		end
	end
end

function outsidetable3:click()
	if self.click_enable then
		PlaySound("check", false);
		self.parent_chosen = GetSelectedParent();
		if GetCharacterState(self.parent_chosen) ~= "sit_outside" then
			self.no_stress_power = IsNannyStressPowerActive();
			
			if self.no_stress_power then
				ShowNoStressIcon(self.object_name);
			end
			
			if self.parent_chosen == "dad" then
				ShowDadIcon(self.object_name);
			else	
				ShowMomIcon(self.object_name);
			end
			
			if self.object_is_dirty then
				PlayGlowAnimation(self.object_name, "dirty_click");
				if self.stress_inc_enabled and not self.no_stress_power then
					AddFutureCharacterStress(self.parent_chosen, self.stress_inc);
				end	
			end
			
			self:parent_command();
			self.click_enable = false;
			self.parent_is_coming = true;
			self.previous_state = GetObjectState(self.object_name);
			
			IncludeObjectInClickedList(self.object_name);
		end
	end
end

function outsidetable3:parent_action()	
	self.parent_is_coming = false;
	--DebugOut("simple_object:parent_action:"..self.object_name);
	
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
		HideDadNextTaskIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
		HideMomNextTaskIcon(self.object_name);
	end
	
	RemoveObjectFromClickedList(self.object_name);
	if GetCharacterState(self.parent_chosen) == self.parent_state_action and
		self.object_is_dirty and
		self.kids_on_table then
		
		self:sit_character(self.parent_chosen);
		FlipAnimation(self.parent_chosen, false);
		
		PlaySound("having_meal", true);
	else
		ShowMiss(self.object_name);
		FinishCharacterTask(self.parent_chosen);
		self.parent_chosen = "";
		self.click_enable = self.set_click_enable;
		if self.object_is_dirty then
			IncludeObjectMouseHandler(self.object_name);
			self.click_enable = true;
			PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
			--PlayGlowAnimation(self.object_name, "dirty");
		else
			if self.click_enable then
				IncludeObjectMouseHandler(self.object_name);
			end
		end
	end
end

function outsidetable3:parent_command()
	RemoveObjectMouseHandler(self.object_name);
	if self.parent_chosen == "dad" then
		AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.dad_chair);
	else
		AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.mom_chair);
	end
end

function outsidetable3:cleaning_update()
	local n = table.getn(self.child_members);
	
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		StopSound("having_meal");
		
		SetCharacterState("dad", "normal");
		SetCharacterState("mom", "normal");
		ShowCharacter("dad");
		ShowCharacter("mom");
		ChangeObjectState("dadeatingoutside", "clean");
		ChangeObjectState("momeatingoutside", "clean");
		
		for i = 1 , n do
			SetCharacterState(self.child_members[i], "normal");
			PlayCharacterAnimation(self.child_members[i], "idle");
			RemoveCharacterOnObject(self.child_members[i]);
			ShowCharacter(self.child_members[i]);
			ChangeObjectState(self.child_members[i].."eatingoutside", "clean");
			FinishCharacterTask(self.child_members[i]);
		end
				
		PlayObjectAnimation("mesafora4", "dirty");
		ChangeObjectState("mesafora4", "dirty");
		ShowNextTaskIcon("mesafora4");
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		
		PlayCharacterAnimation("mom", "idle");
		PlayCharacterAnimation("dad", "idle");
		
		RemoveCharacterOnObject("mom");
		RemoveCharacterOnObject("dad");		
		
		FinishCharacterTask("mom");
		FinishCharacterTask("dad");
		
		self.kids_on_table = false;
		
		RemoveObjectMouseHandler(self.object_name);
		self.dad_coming = false;
		self.mom_coming = false;
	end
end